@@ -9,44 +9,6 @@ use crate::util::errors::CargoResult;
9
9
use crate :: util:: hostname;
10
10
use crate :: util:: style:: * ;
11
11
12
- pub enum TtyWidth {
13
- NoTty ,
14
- Known ( usize ) ,
15
- Guess ( usize ) ,
16
- }
17
-
18
- impl TtyWidth {
19
- /// Returns the width of the terminal to use for diagnostics (which is
20
- /// relayed to rustc via `--diagnostic-width`).
21
- pub fn diagnostic_terminal_width ( & self ) -> Option < usize > {
22
- // ALLOWED: For testing cargo itself only.
23
- #[ allow( clippy:: disallowed_methods) ]
24
- if let Ok ( width) = std:: env:: var ( "__CARGO_TEST_TTY_WIDTH_DO_NOT_USE_THIS" ) {
25
- return Some ( width. parse ( ) . unwrap ( ) ) ;
26
- }
27
- match * self {
28
- TtyWidth :: NoTty | TtyWidth :: Guess ( _) => None ,
29
- TtyWidth :: Known ( width) => Some ( width) ,
30
- }
31
- }
32
-
33
- /// Returns the width used by progress bars for the tty.
34
- pub fn progress_max_width ( & self ) -> Option < usize > {
35
- match * self {
36
- TtyWidth :: NoTty => None ,
37
- TtyWidth :: Known ( width) | TtyWidth :: Guess ( width) => Some ( width) ,
38
- }
39
- }
40
- }
41
-
42
- /// The requested verbosity of output.
43
- #[ derive( Debug , Clone , Copy , PartialEq ) ]
44
- pub enum Verbosity {
45
- Verbose ,
46
- Normal ,
47
- Quiet ,
48
- }
49
-
50
12
/// An abstraction around console output that remembers preferences for output
51
13
/// verbosity and color.
52
14
pub struct Shell {
@@ -77,31 +39,6 @@ impl fmt::Debug for Shell {
77
39
}
78
40
}
79
41
80
- /// A `Write`able object, either with or without color support
81
- enum ShellOut {
82
- /// A plain write object without color support
83
- Write ( AutoStream < Box < dyn Write > > ) ,
84
- /// Color-enabled stdio, with information on whether color should be used
85
- Stream {
86
- stdout : AutoStream < std:: io:: Stdout > ,
87
- stderr : AutoStream < std:: io:: Stderr > ,
88
- stderr_tty : bool ,
89
- color_choice : ColorChoice ,
90
- hyperlinks : bool ,
91
- } ,
92
- }
93
-
94
- /// Whether messages should use color output
95
- #[ derive( Debug , PartialEq , Clone , Copy ) ]
96
- pub enum ColorChoice {
97
- /// Force color output
98
- Always ,
99
- /// Force disable color output
100
- Never ,
101
- /// Intelligently guess whether to use color output
102
- CargoAuto ,
103
- }
104
-
105
42
impl Shell {
106
43
/// Creates a new shell (color choice and verbosity), defaulting to 'auto' color and verbose
107
44
/// output.
@@ -299,18 +236,10 @@ impl Shell {
299
236
..
300
237
} = self . output
301
238
{
302
- let cfg = match color {
303
- Some ( "always" ) => ColorChoice :: Always ,
304
- Some ( "never" ) => ColorChoice :: Never ,
305
-
306
- Some ( "auto" ) | None => ColorChoice :: CargoAuto ,
307
-
308
- Some ( arg) => anyhow:: bail!(
309
- "argument for --color must be auto, always, or \
310
- never, but found `{}`",
311
- arg
312
- ) ,
313
- } ;
239
+ let cfg = color
240
+ . map ( |c| c. parse ( ) )
241
+ . transpose ( ) ?
242
+ . unwrap_or ( ColorChoice :: CargoAuto ) ;
314
243
* color_choice = cfg;
315
244
let stdout_choice = cfg. to_anstream_color_choice ( ) ;
316
245
let stderr_choice = cfg. to_anstream_color_choice ( ) ;
@@ -444,6 +373,20 @@ impl Default for Shell {
444
373
}
445
374
}
446
375
376
+ /// A `Write`able object, either with or without color support
377
+ enum ShellOut {
378
+ /// A plain write object without color support
379
+ Write ( AutoStream < Box < dyn Write > > ) ,
380
+ /// Color-enabled stdio, with information on whether color should be used
381
+ Stream {
382
+ stdout : AutoStream < std:: io:: Stdout > ,
383
+ stderr : AutoStream < std:: io:: Stderr > ,
384
+ stderr_tty : bool ,
385
+ color_choice : ColorChoice ,
386
+ hyperlinks : bool ,
387
+ } ,
388
+ }
389
+
447
390
impl ShellOut {
448
391
/// Prints out a message with a status. The status comes first, and is bold plus the given
449
392
/// color. The status can be justified, in which case the max width that will right align is
@@ -488,6 +431,55 @@ impl ShellOut {
488
431
}
489
432
}
490
433
434
+ pub enum TtyWidth {
435
+ NoTty ,
436
+ Known ( usize ) ,
437
+ Guess ( usize ) ,
438
+ }
439
+
440
+ impl TtyWidth {
441
+ /// Returns the width of the terminal to use for diagnostics (which is
442
+ /// relayed to rustc via `--diagnostic-width`).
443
+ pub fn diagnostic_terminal_width ( & self ) -> Option < usize > {
444
+ // ALLOWED: For testing cargo itself only.
445
+ #[ allow( clippy:: disallowed_methods) ]
446
+ if let Ok ( width) = std:: env:: var ( "__CARGO_TEST_TTY_WIDTH_DO_NOT_USE_THIS" ) {
447
+ return Some ( width. parse ( ) . unwrap ( ) ) ;
448
+ }
449
+ match * self {
450
+ TtyWidth :: NoTty | TtyWidth :: Guess ( _) => None ,
451
+ TtyWidth :: Known ( width) => Some ( width) ,
452
+ }
453
+ }
454
+
455
+ /// Returns the width used by progress bars for the tty.
456
+ pub fn progress_max_width ( & self ) -> Option < usize > {
457
+ match * self {
458
+ TtyWidth :: NoTty => None ,
459
+ TtyWidth :: Known ( width) | TtyWidth :: Guess ( width) => Some ( width) ,
460
+ }
461
+ }
462
+ }
463
+
464
+ /// The requested verbosity of output.
465
+ #[ derive( Debug , Clone , Copy , PartialEq ) ]
466
+ pub enum Verbosity {
467
+ Verbose ,
468
+ Normal ,
469
+ Quiet ,
470
+ }
471
+
472
+ /// Whether messages should use color output
473
+ #[ derive( Debug , PartialEq , Clone , Copy ) ]
474
+ pub enum ColorChoice {
475
+ /// Force color output
476
+ Always ,
477
+ /// Force disable color output
478
+ Never ,
479
+ /// Intelligently guess whether to use color output
480
+ CargoAuto ,
481
+ }
482
+
491
483
impl ColorChoice {
492
484
/// Converts our color choice to anstream's version.
493
485
fn to_anstream_color_choice ( self ) -> anstream:: ColorChoice {
@@ -499,6 +491,25 @@ impl ColorChoice {
499
491
}
500
492
}
501
493
494
+ impl std:: str:: FromStr for ColorChoice {
495
+ type Err = anyhow:: Error ;
496
+ fn from_str ( color : & str ) -> Result < Self , Self :: Err > {
497
+ let cfg = match color {
498
+ "always" => ColorChoice :: Always ,
499
+ "never" => ColorChoice :: Never ,
500
+
501
+ "auto" => ColorChoice :: CargoAuto ,
502
+
503
+ arg => anyhow:: bail!(
504
+ "argument for --color must be auto, always, or \
505
+ never, but found `{}`",
506
+ arg
507
+ ) ,
508
+ } ;
509
+ Ok ( cfg)
510
+ }
511
+ }
512
+
502
513
fn supports_color ( choice : anstream:: ColorChoice ) -> bool {
503
514
match choice {
504
515
anstream:: ColorChoice :: Always
0 commit comments