@@ -2,9 +2,8 @@ use std::fmt;
2
2
use std:: io:: prelude:: * ;
3
3
use std:: io:: IsTerminal ;
4
4
5
+ use anstream:: AutoStream ;
5
6
use anstyle:: Style ;
6
- use anstyle_termcolor:: to_termcolor_spec;
7
- use termcolor:: { self , BufferWriter , StandardStream , WriteColor } ;
8
7
9
8
use crate :: util:: errors:: CargoResult ;
10
9
use crate :: util:: style:: * ;
@@ -81,15 +80,9 @@ enum ShellOut {
81
80
/// A plain write object without color support
82
81
Write ( Box < dyn Write > ) ,
83
82
/// Color-enabled stdio, with information on whether color should be used
84
- ///
85
- /// The separate buffered fields are used for buffered writing to the
86
- /// corresponding stream. The non-buffered fields should be used when you
87
- /// do not want content to be buffered.
88
83
Stream {
89
- stdout : StandardStream ,
90
- buffered_stdout : BufferWriter ,
91
- stderr : StandardStream ,
92
- buffered_stderr : BufferWriter ,
84
+ stdout : AutoStream < std:: io:: Stdout > ,
85
+ stderr : AutoStream < std:: io:: Stderr > ,
93
86
stderr_tty : bool ,
94
87
color_choice : ColorChoice ,
95
88
} ,
@@ -111,15 +104,13 @@ impl Shell {
111
104
/// output.
112
105
pub fn new ( ) -> Shell {
113
106
let auto_clr = ColorChoice :: CargoAuto ;
114
- let stdout_choice = auto_clr. to_termcolor_color_choice ( Stream :: Stdout ) ;
115
- let stderr_choice = auto_clr. to_termcolor_color_choice ( Stream :: Stderr ) ;
107
+ let stdout_choice = auto_clr. to_anstream_color_choice ( ) ;
108
+ let stderr_choice = auto_clr. to_anstream_color_choice ( ) ;
116
109
Shell {
117
110
output : ShellOut :: Stream {
118
- stdout : StandardStream :: stdout ( stdout_choice) ,
119
- buffered_stdout : BufferWriter :: stdout ( stdout_choice) ,
120
- stderr : StandardStream :: stderr ( stderr_choice) ,
121
- buffered_stderr : BufferWriter :: stderr ( stderr_choice) ,
122
- color_choice : ColorChoice :: CargoAuto ,
111
+ stdout : AutoStream :: new ( std:: io:: stdout ( ) , stdout_choice) ,
112
+ stderr : AutoStream :: new ( std:: io:: stderr ( ) , stderr_choice) ,
113
+ color_choice : auto_clr,
123
114
stderr_tty : std:: io:: stderr ( ) . is_terminal ( ) ,
124
115
} ,
125
116
verbosity : Verbosity :: Verbose ,
@@ -297,9 +288,7 @@ impl Shell {
297
288
pub fn set_color_choice ( & mut self , color : Option < & str > ) -> CargoResult < ( ) > {
298
289
if let ShellOut :: Stream {
299
290
ref mut stdout,
300
- ref mut buffered_stdout,
301
291
ref mut stderr,
302
- ref mut buffered_stderr,
303
292
ref mut color_choice,
304
293
..
305
294
} = self . output
@@ -317,12 +306,10 @@ impl Shell {
317
306
) ,
318
307
} ;
319
308
* color_choice = cfg;
320
- let stdout_choice = cfg. to_termcolor_color_choice ( Stream :: Stdout ) ;
321
- let stderr_choice = cfg. to_termcolor_color_choice ( Stream :: Stderr ) ;
322
- * stdout = StandardStream :: stdout ( stdout_choice) ;
323
- * buffered_stdout = BufferWriter :: stdout ( stdout_choice) ;
324
- * stderr = StandardStream :: stderr ( stderr_choice) ;
325
- * buffered_stderr = BufferWriter :: stderr ( stderr_choice) ;
309
+ let stdout_choice = cfg. to_anstream_color_choice ( ) ;
310
+ let stderr_choice = cfg. to_anstream_color_choice ( ) ;
311
+ * stdout = AutoStream :: new ( std:: io:: stdout ( ) , stdout_choice) ;
312
+ * stderr = AutoStream :: new ( std:: io:: stderr ( ) , stderr_choice) ;
326
313
}
327
314
Ok ( ( ) )
328
315
}
@@ -342,14 +329,14 @@ impl Shell {
342
329
pub fn err_supports_color ( & self ) -> bool {
343
330
match & self . output {
344
331
ShellOut :: Write ( _) => false ,
345
- ShellOut :: Stream { stderr, .. } => stderr. supports_color ( ) ,
332
+ ShellOut :: Stream { stderr, .. } => supports_color ( stderr. current_choice ( ) ) ,
346
333
}
347
334
}
348
335
349
336
pub fn out_supports_color ( & self ) -> bool {
350
337
match & self . output {
351
338
ShellOut :: Write ( _) => false ,
352
- ShellOut :: Stream { stdout, .. } => stdout. supports_color ( ) ,
339
+ ShellOut :: Stream { stdout, .. } => supports_color ( stdout. current_choice ( ) ) ,
353
340
}
354
341
}
355
342
@@ -372,13 +359,6 @@ impl Shell {
372
359
if self . needs_clear {
373
360
self . err_erase_line ( ) ;
374
361
}
375
- #[ cfg( windows) ]
376
- {
377
- if let ShellOut :: Stream { stderr, .. } = & mut self . output {
378
- :: fwdansi:: write_ansi ( stderr, message) ?;
379
- return Ok ( ( ) ) ;
380
- }
381
- }
382
362
self . err ( ) . write_all ( message) ?;
383
363
Ok ( ( ) )
384
364
}
@@ -388,13 +368,6 @@ impl Shell {
388
368
if self . needs_clear {
389
369
self . err_erase_line ( ) ;
390
370
}
391
- #[ cfg( windows) ]
392
- {
393
- if let ShellOut :: Stream { stdout, .. } = & mut self . output {
394
- :: fwdansi:: write_ansi ( stdout, message) ?;
395
- return Ok ( ( ) ) ;
396
- }
397
- }
398
371
self . out ( ) . write_all ( message) ?;
399
372
Ok ( ( ) )
400
373
}
@@ -426,26 +399,22 @@ impl ShellOut {
426
399
justified : bool ,
427
400
) -> CargoResult < ( ) > {
428
401
match * self {
429
- ShellOut :: Stream {
430
- ref mut buffered_stderr,
431
- ..
432
- } => {
433
- let mut buffer = buffered_stderr. buffer ( ) ;
434
- buffer. reset ( ) ?;
435
- buffer. set_color ( & to_termcolor_spec ( * style) ) ?;
402
+ ShellOut :: Stream { ref mut stderr, .. } => {
403
+ let style = style. render ( ) ;
404
+ let bold = ( anstyle:: Style :: new ( ) | anstyle:: Effects :: BOLD ) . render ( ) ;
405
+ let reset = anstyle:: Reset . render ( ) ;
406
+
407
+ let mut buffer = Vec :: new ( ) ;
436
408
if justified {
437
- write ! ( buffer, "{:>12}" , status ) ?;
409
+ write ! ( & mut buffer, "{style}{status :>12}{reset}" ) ?;
438
410
} else {
439
- write ! ( buffer, "{}" , status) ?;
440
- buffer. set_color ( termcolor:: ColorSpec :: new ( ) . set_bold ( true ) ) ?;
441
- write ! ( buffer, ":" ) ?;
411
+ write ! ( & mut buffer, "{style}{status}{reset}{bold}:{reset}" ) ?;
442
412
}
443
- buffer. reset ( ) ?;
444
413
match message {
445
- Some ( message) => writeln ! ( buffer, " {}" , message ) ?,
414
+ Some ( message) => writeln ! ( buffer, " {message}" ) ?,
446
415
None => write ! ( buffer, " " ) ?,
447
416
}
448
- buffered_stderr . print ( & buffer) ?;
417
+ stderr . write_all ( & buffer) ?;
449
418
}
450
419
ShellOut :: Write ( ref mut w) => {
451
420
if justified {
@@ -463,18 +432,15 @@ impl ShellOut {
463
432
}
464
433
465
434
/// Write a styled fragment
466
- fn write_stdout ( & mut self , fragment : impl fmt:: Display , color : & Style ) -> CargoResult < ( ) > {
435
+ fn write_stdout ( & mut self , fragment : impl fmt:: Display , style : & Style ) -> CargoResult < ( ) > {
467
436
match * self {
468
- ShellOut :: Stream {
469
- ref mut buffered_stdout,
470
- ..
471
- } => {
472
- let mut buffer = buffered_stdout. buffer ( ) ;
473
- buffer. reset ( ) ?;
474
- buffer. set_color ( & to_termcolor_spec ( * color) ) ?;
475
- write ! ( buffer, "{}" , fragment) ?;
476
- buffer. reset ( ) ?;
477
- buffered_stdout. print ( & buffer) ?;
437
+ ShellOut :: Stream { ref mut stdout, .. } => {
438
+ let style = style. render ( ) ;
439
+ let reset = anstyle:: Reset . render ( ) ;
440
+
441
+ let mut buffer = Vec :: new ( ) ;
442
+ write ! ( buffer, "{style}{}{reset}" , fragment) ?;
443
+ stdout. write_all ( & buffer) ?;
478
444
}
479
445
ShellOut :: Write ( ref mut w) => {
480
446
write ! ( w, "{}" , fragment) ?;
@@ -484,18 +450,15 @@ impl ShellOut {
484
450
}
485
451
486
452
/// Write a styled fragment
487
- fn write_stderr ( & mut self , fragment : impl fmt:: Display , color : & Style ) -> CargoResult < ( ) > {
453
+ fn write_stderr ( & mut self , fragment : impl fmt:: Display , style : & Style ) -> CargoResult < ( ) > {
488
454
match * self {
489
- ShellOut :: Stream {
490
- ref mut buffered_stderr,
491
- ..
492
- } => {
493
- let mut buffer = buffered_stderr. buffer ( ) ;
494
- buffer. reset ( ) ?;
495
- buffer. set_color ( & to_termcolor_spec ( * color) ) ?;
496
- write ! ( buffer, "{}" , fragment) ?;
497
- buffer. reset ( ) ?;
498
- buffered_stderr. print ( & buffer) ?;
455
+ ShellOut :: Stream { ref mut stderr, .. } => {
456
+ let style = style. render ( ) ;
457
+ let reset = anstyle:: Reset . render ( ) ;
458
+
459
+ let mut buffer = Vec :: new ( ) ;
460
+ write ! ( buffer, "{style}{}{reset}" , fragment) ?;
461
+ stderr. write_all ( & buffer) ?;
499
462
}
500
463
ShellOut :: Write ( ref mut w) => {
501
464
write ! ( w, "{}" , fragment) ?;
@@ -522,33 +485,22 @@ impl ShellOut {
522
485
}
523
486
524
487
impl ColorChoice {
525
- /// Converts our color choice to termcolor 's version.
526
- fn to_termcolor_color_choice ( self , stream : Stream ) -> termcolor :: ColorChoice {
488
+ /// Converts our color choice to anstream 's version.
489
+ fn to_anstream_color_choice ( self ) -> anstream :: ColorChoice {
527
490
match self {
528
- ColorChoice :: Always => termcolor:: ColorChoice :: Always ,
529
- ColorChoice :: Never => termcolor:: ColorChoice :: Never ,
530
- ColorChoice :: CargoAuto => {
531
- if stream. is_terminal ( ) {
532
- termcolor:: ColorChoice :: Auto
533
- } else {
534
- termcolor:: ColorChoice :: Never
535
- }
536
- }
491
+ ColorChoice :: Always => anstream:: ColorChoice :: Always ,
492
+ ColorChoice :: Never => anstream:: ColorChoice :: Never ,
493
+ ColorChoice :: CargoAuto => anstream:: ColorChoice :: Auto ,
537
494
}
538
495
}
539
496
}
540
497
541
- enum Stream {
542
- Stdout ,
543
- Stderr ,
544
- }
545
-
546
- impl Stream {
547
- fn is_terminal ( self ) -> bool {
548
- match self {
549
- Self :: Stdout => std:: io:: stdout ( ) . is_terminal ( ) ,
550
- Self :: Stderr => std:: io:: stderr ( ) . is_terminal ( ) ,
551
- }
498
+ fn supports_color ( choice : anstream:: ColorChoice ) -> bool {
499
+ match choice {
500
+ anstream:: ColorChoice :: Always
501
+ | anstream:: ColorChoice :: AlwaysAnsi
502
+ | anstream:: ColorChoice :: Auto => true ,
503
+ anstream:: ColorChoice :: Never => false ,
552
504
}
553
505
}
554
506
0 commit comments