diff options
author | Pierre-Henri Symoneaux <pierre.henri.symoneaux@gmail.com> | 2019-08-25 15:12:45 +0200 |
---|---|---|
committer | Pierre-Henri Symoneaux <pierre.henri.symoneaux@gmail.com> | 2019-08-25 15:12:45 +0200 |
commit | cb6a033d7a212674c356c1a8365511abe2b4fca9 (patch) | |
tree | 414e5927c2bc90245b7005a818f14399f6b05b82 | |
parent | 1e06ea72ccf03cd2129d104360e1e07679190a07 (diff) |
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.
-rw-r--r-- | examples/tictactoe.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 38 | ||||
-rw-r--r-- | src/main.rs | 3 |
3 files changed, 15 insertions, 30 deletions
diff --git a/examples/tictactoe.rs b/examples/tictactoe.rs index 0ff7207..0c299f9 100644 --- a/examples/tictactoe.rs +++ b/examples/tictactoe.rs @@ -14,7 +14,7 @@ fn main() { [EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY] ]; - let mut height = table.printstd(); + let mut height = table.print_tty(false).unwrap(); let stdin = io::stdin(); let mut stdout = io::stdout(); let mut current = CROSS; @@ -53,7 +53,7 @@ fn main() { terminal.cursor_up().unwrap(); terminal.delete_line().unwrap(); } - height = table.printstd(); + height = table.print_tty(false).unwrap(); if check(&table) { return; } @@ -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<usize, Error> { + 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<usize, Error> { 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() } diff --git a/src/main.rs b/src/main.rs index 28d7b78..f18461b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,7 +45,6 @@ fn main() { table.set_titles(row!["Title 1", "Title 2"]); table.set_format(*consts::FORMAT_DEFAULT); table.get_format().indent(8); - let size = table.printstd(); - println!("Table height = {}", size); + table.printstd(); // println!("{:#?}", table); } |