From cb6a033d7a212674c356c1a8365511abe2b4fca9 Mon Sep 17 00:00:00 2001 From: Pierre-Henri Symoneaux Date: Sun, 25 Aug 2019 15:12:45 +0200 Subject: As suggested in #103, `printstd` should avoid panicking. Thus, as an improvement, `printstd` now ignores errors, but don't return the printed size anymore. `print_tty` now returns a `Result` and can be used for better control. --- src/lib.rs | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 66daaf9..e60a076 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -178,17 +178,11 @@ impl<'a> TableSlice<'a> { /// as not beeing tty, and ANSI escape characters won't be displayed unless `force colorize` /// is set to `true`. /// # Returns - /// The number of lines printed - /// # Panic - /// Panic if writing to standard output fails - pub fn print_tty(&self, force_colorize: bool) -> usize { - let r = match (stdout(), atty::is(atty::Stream::Stdout) || force_colorize) { + /// A `Result` holding the number of lines printed, or an `io::Error` if any failure happens + pub fn print_tty(&self, force_colorize: bool) -> Result { + match (stdout(), atty::is(atty::Stream::Stdout) || force_colorize) { (Some(mut o), true) => self.print_term(&mut *o), _ => self.print(&mut io::stdout()), - }; - match r { - Err(e) => panic!("Cannot print table to standard output : {}", e), - Ok(height) => height } } @@ -196,13 +190,10 @@ impl<'a> TableSlice<'a> { /// stdout is a tty terminal. This means that if stdout is redirected to a file, or piped /// to another program, no color will be displayed. /// To force colors rendering, use `print_tty()` method. - /// Calling `printstd()` is equivalent to calling `print_tty(false)` - /// # Returns - /// The number of lines printed - /// # Panic - /// Panic if writing to standard output fails - pub fn printstd(&self) -> usize { - self.print_tty(false) + /// Any failure to print is ignored. For better control, use `print_tty()`. + /// Calling `printstd()` is equivalent to calling `print_tty(false)` and ignoring the result. + pub fn printstd(&self) { + let _ = self.print_tty(false); // Ignore result } } @@ -352,10 +343,8 @@ impl Table { /// as not beeing tty, and ANSI escape characters won't be displayed unless `force colorize` /// is set to `true`. /// # Returns - /// The number of lines printed - /// # Panic - /// Panic if writing to standard output fails - pub fn print_tty(&self, force_colorize: bool) -> usize { + /// A `Result` holding the number of lines printed, or an `io::Error` if any failure happens + pub fn print_tty(&self, force_colorize: bool) -> Result { self.as_ref().print_tty(force_colorize) } @@ -363,12 +352,9 @@ impl Table { /// stdout is a tty terminal. This means that if stdout is redirected to a file, or piped /// to another program, no color will be displayed. /// To force colors rendering, use `print_tty()` method. - /// Calling `printstd()` is equivalent to calling `print_tty(false)` - /// # Returns - /// The number of lines printed - /// # Panic - /// Panic if writing to standard output fails - pub fn printstd(&self) -> usize { + /// Any failure to print is ignored. For better control, use `print_tty()`. + /// Calling `printstd()` is equivalent to calling `print_tty(false)` and ignoring the result. + pub fn printstd(&self) { self.as_ref().printstd() } -- cgit v1.2.3