Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove panic in print_tty and return a Result #114

Merged
merged 2 commits into from Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/tictactoe.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
38 changes: 12 additions & 26 deletions src/lib.rs
Expand Up @@ -178,31 +178,22 @@ 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
}
}

/// Print the table to standard output. Colors won't be displayed unless
/// 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
}
}

Expand Down Expand Up @@ -352,23 +343,18 @@ 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)
}

/// Print the table to standard output. Colors won't be displayed unless
/// 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()
}

Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Expand Up @@ -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);
}