Skip to content

Commit acad699

Browse files
pwoolcocPaul Woolcock
authored and
Paul Woolcock
committed
Towards rust-lang#653
Change the `verbose` attribute of a shell from a `bool` to a `Verbosity` enum, which has `Verbose`, `Normal`, and `Quiet` variants.
1 parent e6ec71a commit acad699

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

src/cargo/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub use self::package_id::PackageId;
55
pub use self::package_id_spec::PackageIdSpec;
66
pub use self::registry::Registry;
77
pub use self::resolver::Resolve;
8-
pub use self::shell::{Shell, MultiShell, ShellConfig};
8+
pub use self::shell::{Shell, MultiShell, ShellConfig, Verbosity};
99
pub use self::source::{Source, SourceId, SourceMap, SourceSet};
1010
pub use self::summary::Summary;
1111

src/cargo/core/shell.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ use term::attr::{Attr, Bold};
44
use std::io::{IoResult, stderr};
55
use std::fmt::Show;
66

7+
pub enum Verbosity {
8+
Verbose,
9+
Normal,
10+
Quiet
11+
}
12+
713
pub struct ShellConfig {
814
pub color: bool,
9-
pub verbose: bool,
15+
pub verbosity: Verbosity,
1016
pub tty: bool
1117
}
1218

@@ -23,7 +29,7 @@ pub struct Shell {
2329
pub struct MultiShell {
2430
out: Shell,
2531
err: Shell,
26-
verbose: bool
32+
verbose: Verbosity
2733
}
2834

2935
pub type Callback<'a> = |&mut MultiShell|:'a -> IoResult<()>;
@@ -33,8 +39,8 @@ struct UghWhyIsThisNecessary {
3339
}
3440

3541
impl MultiShell {
36-
pub fn new(out: Shell, err: Shell, verbose: bool) -> MultiShell {
37-
MultiShell { out: out, err: err, verbose: verbose }
42+
pub fn new(out: Shell, err: Shell, verbosity: Verbosity) -> MultiShell {
43+
MultiShell { out: out, err: err, verbosity: verbosity }
3844
}
3945

4046
pub fn out(&mut self) -> &mut Shell {
@@ -54,13 +60,17 @@ impl MultiShell {
5460
}
5561

5662
pub fn verbose(&mut self, callback: Callback) -> IoResult<()> {
57-
if self.verbose { return callback(self) }
58-
Ok(())
63+
match self.verbosity {
64+
Verbose => return callback(self),
65+
_ => Ok(())
66+
}
5967
}
6068

6169
pub fn concise(&mut self, callback: Callback) -> IoResult<()> {
62-
if !self.verbose { return callback(self) }
63-
Ok(())
70+
match self.verbosity {
71+
Verbose => Ok(()),
72+
_ => return callback(self)
73+
}
6474
}
6575

6676
pub fn error<T: ToString>(&mut self, message: T) -> IoResult<()> {
@@ -71,8 +81,17 @@ impl MultiShell {
7181
self.err().say(message, YELLOW)
7282
}
7383

84+
pub fn set_verbosity(&mut self, verbosity: Verbosity) {
85+
self.verbosity = verbosity;
86+
}
87+
88+
/// shortcut for commands that don't have both --verbose and --quiet
7489
pub fn set_verbose(&mut self, verbose: bool) {
75-
self.verbose = verbose;
90+
if verbose {
91+
self.verbosity = Verbose;
92+
} else {
93+
self.verbosity = Normal;
94+
}
7695
}
7796
}
7897

@@ -95,13 +114,17 @@ impl Shell {
95114
}
96115

97116
pub fn verbose(&mut self, callback: ShellCallback) -> IoResult<()> {
98-
if self.config.verbose { return callback(self) }
99-
Ok(())
117+
match self.config.verbosity {
118+
Verbose => return callback(self),
119+
_ => Ok(())
120+
}
100121
}
101122

102123
pub fn concise(&mut self, callback: ShellCallback) -> IoResult<()> {
103-
if !self.config.verbose { return callback(self) }
104-
Ok(())
124+
match self.config.verbosity {
125+
Verbose => Ok(()),
126+
_ => return callback(self)
127+
}
105128
}
106129

107130
pub fn say<T: ToString>(&mut self, message: T, color: Color) -> IoResult<()> {

src/cargo/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ use std::io::{mod, stdout, stderr};
3232
use serialize::{Decoder, Encoder, Decodable, Encodable, json};
3333
use docopt::Docopt;
3434

35-
use core::{Shell, MultiShell, ShellConfig};
35+
use core::{Shell, MultiShell, ShellConfig, Verbosity};
36+
use core::shell::{Verbose};
3637
use term::color::{BLACK};
3738

3839
pub use util::{CargoError, CliError, CliResult, human};
@@ -131,7 +132,7 @@ pub fn call_main_without_stdin<'a,
131132

132133
fn process<'a, V: Encodable<json::Encoder<'a>, io::IoError>>(
133134
callback: |&[String], &mut MultiShell| -> CliResult<Option<V>>) {
134-
let mut shell = shell(true);
135+
let mut shell = shell(Verbose);
135136
process_executed(callback(os::args().as_slice(), &mut shell), &mut shell)
136137
}
137138

@@ -149,20 +150,20 @@ pub fn process_executed<'a,
149150
}
150151
}
151152

152-
pub fn shell(verbose: bool) -> MultiShell {
153+
pub fn shell(verbosity: Verbosity) -> MultiShell {
153154
let tty = stderr_raw().isatty();
154155
let stderr = box stderr() as Box<Writer + Send>;
155156

156-
let config = ShellConfig { color: true, verbose: verbose, tty: tty };
157+
let config = ShellConfig { color: true, verbosity: verbosity, tty: tty };
157158
let err = Shell::create(stderr, config);
158159

159160
let tty = stdout_raw().isatty();
160161
let stdout = box stdout() as Box<Writer + Send>;
161162

162-
let config = ShellConfig { color: true, verbose: verbose, tty: tty };
163+
let config = ShellConfig { color: true, verbosity: verbosity, tty: tty };
163164
let out = Shell::create(stdout, config);
164165

165-
MultiShell::new(out, err, verbose)
166+
MultiShell::new(out, err, verbosity)
166167
}
167168

168169
pub fn handle_error(err: CliError, shell: &mut MultiShell) {

0 commit comments

Comments
 (0)