From: Yehuda Katz Date: Sun, 22 Jun 2014 06:02:38 +0000 (-0700) Subject: More shell refactoring X-Git-Tag: archive/raspbian/0.35.0-2+rpi1~3^2^2^2^2^2^2^2~981 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=ad2c2f876d076cf33f06b060343e4f2ce4aa3a49;p=cargo.git More shell refactoring --- diff --git a/src/bin/cargo.rs b/src/bin/cargo.rs index 562164e34..8873a9a03 100644 --- a/src/bin/cargo.rs +++ b/src/bin/cargo.rs @@ -35,7 +35,7 @@ fn execute() { let (cmd, args) = match process(os::args()) { Ok((cmd, args)) => (cmd, args), - Err(err) => return handle_error(err, &mut shell(), false) + Err(err) => return handle_error(err, &mut shell(false)) }; match cmd.as_slice() { @@ -69,9 +69,9 @@ fn execute() { match command { Ok(ExitStatus(0)) => (), Ok(ExitStatus(i)) | Ok(ExitSignal(i)) => { - handle_error(CliError::new("", i as uint), &mut shell(), false) + handle_error(CliError::new("", i as uint), &mut shell(false)) } - Err(_) => handle_error(CliError::new("No such subcommand", 127), &mut shell(), false) + Err(_) => handle_error(CliError::new("No such subcommand", 127), &mut shell(false)) } } } diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 8eaeb0332..34891fc96 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -23,12 +23,15 @@ pub struct Shell { pub struct MultiShell { out: Shell, - err: Shell + err: Shell, + verbose: bool } +pub type Callback<'a> = |&mut MultiShell|:'a -> IoResult<()>; + impl MultiShell { - pub fn new(out: Shell, err: Shell) -> MultiShell { - MultiShell { out: out, err: err } + pub fn new(out: Shell, err: Shell, verbose: bool) -> MultiShell { + MultiShell { out: out, err: err, verbose: verbose } } pub fn out<'a>(&'a mut self) -> &'a mut Shell { @@ -47,12 +50,22 @@ impl MultiShell { self.out().say_status(status, message, GREEN) } + pub fn verbose(&mut self, callback: Callback) -> IoResult<()> { + if self.verbose { return callback(self) } + Ok(()) + } + + pub fn concise(&mut self, callback: Callback) -> IoResult<()> { + if !self.verbose { return callback(self) } + Ok(()) + } + pub fn error(&mut self, message: T) -> IoResult<()> { self.err().say(message, RED) } } - +pub type ShellCallback<'a> = |&mut Shell|:'a -> IoResult<()>; impl Shell { pub fn create(out: Box, config: ShellConfig) -> Shell { @@ -69,12 +82,13 @@ impl Shell { } } - pub fn verbose(&mut self, - callback: |&mut Shell| -> IoResult<()>) -> IoResult<()> { - if self.config.verbose { - return callback(self) - } + pub fn verbose(&mut self, callback: ShellCallback) -> IoResult<()> { + if self.config.verbose { return callback(self) } + Ok(()) + } + pub fn concise(&mut self, callback: ShellCallback) -> IoResult<()> { + if !self.config.verbose { return callback(self) } Ok(()) } diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index 8879bd70f..ec9d8afa9 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -144,11 +144,12 @@ fn process<'a, V: Encodable, io::IoError>>( callback: |&[String], &mut MultiShell| -> CliResult>) { - let mut shell = shell(); match global_flags() { - Err(e) => handle_error(e, &mut shell, true), + Err(e) => handle_error(e, &mut shell(false)), Ok(val) => { + let mut shell = shell(val.verbose); + if val.help { let (desc, options) = hammer::usage::(true); @@ -161,7 +162,7 @@ fn process<'a, let (_, options) = hammer::usage::(false); print!("{}", options); } else { - process_executed(callback(val.rest.as_slice(), &mut shell), val, &mut shell) + process_executed(callback(val.rest.as_slice(), &mut shell), &mut shell) } } } @@ -170,10 +171,10 @@ fn process<'a, pub fn process_executed<'a, T: Encodable, io::IoError>>( result: CliResult>, - flags: GlobalFlags, shell: &mut MultiShell) + shell: &mut MultiShell) { match result { - Err(e) => handle_error(e, shell, flags.verbose), + Err(e) => handle_error(e, shell), Ok(encodable) => { encodable.map(|encodable| { let encoded = json::Encoder::str_encode(&encodable); @@ -183,23 +184,23 @@ pub fn process_executed<'a, } } -pub fn shell() -> MultiShell { +pub fn shell(verbose: bool) -> MultiShell { let tty = stderr_raw().isatty(); let stderr = box stderr() as Box; - let config = ShellConfig { color: true, verbose: false, tty: tty }; + let config = ShellConfig { color: true, verbose: verbose, tty: tty }; let err = Shell::create(stderr, config); let tty = stdout_raw().isatty(); let stdout = box stdout() as Box; - let config = ShellConfig { color: true, verbose: false, tty: tty }; + let config = ShellConfig { color: true, verbose: verbose, tty: tty }; let out = Shell::create(stdout, config); - MultiShell::new(out, err) + MultiShell::new(out, err, verbose) } -pub fn handle_error(err: CliError, shell: &mut MultiShell, verbose: bool) { +pub fn handle_error(err: CliError, shell: &mut MultiShell) { log!(4, "handle_error; err={}", err); let CliError { error, exit_code, unknown, .. } = err; @@ -210,13 +211,16 @@ pub fn handle_error(err: CliError, shell: &mut MultiShell, verbose: bool) { let _ = shell.error(error.to_str()); } - if unknown && !verbose { - let _ = shell.err().say("\nTo learn more, run the command again with --verbose.", BLACK); + if unknown { + let _ = shell.concise(|shell| { + shell.err().say("\nTo learn more, run the command again with --verbose.", BLACK) + }); } - if verbose { - handle_cause(error, shell); - } + let _ = shell.verbose(|shell| { + let _ = handle_cause(error, shell); + Ok(()) + }); std::os::set_exit_status(exit_code as int); }