More shell refactoring
authorYehuda Katz <wycats@gmail.com>
Sun, 22 Jun 2014 06:02:38 +0000 (23:02 -0700)
committerYehuda Katz <wycats@gmail.com>
Sun, 22 Jun 2014 06:02:38 +0000 (23:02 -0700)
src/bin/cargo.rs
src/cargo/core/shell.rs
src/cargo/lib.rs

index 562164e34c7846aab5f1632b5c00ecf85bab6920..8873a9a030b53d74bbe29167d7b78bd3fb53c068 100644 (file)
@@ -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))
             }
         }
     }
index 8eaeb033214b8f421db6d74957c720f8a75485ab..34891fc968b3b4755466f53b867c7c5c31287466 100644 (file)
@@ -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<T: ToStr>(&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<Writer>, 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(())
     }
 
index 8879bd70f58a3d713d37b8614faa44b4b1fa8faf..ec9d8afa93916a3c10f6f522d43237cc81af4617 100644 (file)
@@ -144,11 +144,12 @@ fn process<'a,
            V: Encodable<json::Encoder<'a>, io::IoError>>(
                callback: |&[String], &mut MultiShell| -> CliResult<Option<V>>) {
 
-    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::<T>(true);
 
@@ -161,7 +162,7 @@ fn process<'a,
                 let (_, options) = hammer::usage::<GlobalFlags>(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<json::Encoder<'a>, io::IoError>>(
                             result: CliResult<Option<T>>,
-                            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<Writer>;
 
-    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<Writer>;
 
-    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);
 }