summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPierre-Henri Symoneaux <phsym@users.noreply.github.com>2019-08-26 22:00:25 +0200
committerGitHub <noreply@github.com>2019-08-26 22:00:25 +0200
commit6bb234c979f8153f8ee8eede5cbb9a1ae2658f23 (patch)
tree3240b3ffdd53f569627d63484030bba6f15b3dd2
parentbdfb15081cf5e44934a08941f18a6eaba8275e1e (diff)
parent2040ec8190518db84d1651c596950488750ab567 (diff)
Merge pull request #114 from phsym/printstd-nopanic
Remove panic in print_tty and return a Result
-rw-r--r--examples/tictactoe.rs4
-rw-r--r--src/cell.rs3
-rw-r--r--src/csv.rs6
-rw-r--r--src/format.rs10
-rw-r--r--src/lib.rs44
-rw-r--r--src/main.rs3
-rw-r--r--src/row.rs2
-rw-r--r--src/utils.rs14
8 files changed, 35 insertions, 51 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;
}
diff --git a/src/cell.rs b/src/cell.rs
index 410ebd1..4b54f19 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -230,8 +230,7 @@ impl Cell {
for a in &self.style {
match out.attr(*a) {
Ok(..) | Err(::term::Error::NotSupported) | Err(::term::Error::ColorOutOfRange) => {
- ()
- } // Ignore unsupported atrributes
+ } // Ignore unsupported attributes
Err(e) => return Err(term_error_to_io_error(e)),
};
}
diff --git a/src/csv.rs b/src/csv.rs
index 417e8e6..9c5cd9f 100644
--- a/src/csv.rs
+++ b/src/csv.rs
@@ -83,9 +83,9 @@ impl super::Table {
mod tests {
use crate::{Table, Row, Cell};
- static CSV_S: &'static str = "ABC,DEFG,HIJKLMN\n\
- foobar,bar,foo\n\
- foobar2,bar2,foo2\n";
+ static CSV_S: &str = "ABC,DEFG,HIJKLMN\n\
+ foobar,bar,foo\n\
+ foobar2,bar2,foo2\n";
fn test_table() -> Table {
let mut table = Table::new();
diff --git a/src/format.rs b/src/format.rs
index ddf6713..9339348 100644
--- a/src/format.rs
+++ b/src/format.rs
@@ -60,10 +60,10 @@ impl LineSeparator {
/// and `junc` is the one used for junctions between columns and lines
pub fn new(line: char, junc: char, ljunc: char, rjunc: char) -> LineSeparator {
LineSeparator {
- line: line,
- junc: junc,
- ljunc: ljunc,
- rjunc: rjunc,
+ line,
+ junc,
+ ljunc,
+ rjunc,
}
}
@@ -80,7 +80,7 @@ impl LineSeparator {
if lborder {
out.write_all(Utf8Char::from(self.ljunc).as_bytes())?;
}
- let mut iter = col_width.into_iter().peekable();
+ let mut iter = col_width.iter().peekable();
while let Some(width) = iter.next() {
for _ in 0..width + padding.0 + padding.1 {
out.write_all(Utf8Char::from(self.line).as_bytes())?;
diff --git a/src/lib.rs b/src/lib.rs
index 9ccb008..11b4ed4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,7 +3,7 @@
unused_import_braces,
unused_qualifications)]
//! A formatted and aligned table printer written in rust
-//!
+
#[macro_use]
extern crate lazy_static;
@@ -148,7 +148,7 @@ impl<'a> TableSlice<'a> {
.print_line_separator(out, &col_width, LinePosition::Title)?;
}
// Print rows
- let mut iter = self.rows.into_iter().peekable();
+ let mut iter = self.rows.iter().peekable();
while let Some(r) = iter.next() {
height += f(r, out, self.format, &col_width)?;
if iter.peek().is_some() {
@@ -181,17 +181,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
}
}
@@ -199,13 +193,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
}
/// Print table in HTML format to `out`.
@@ -248,7 +239,7 @@ impl Table {
/// Create a table initialized with `rows`
pub fn init(rows: Vec<Row>) -> Table {
Table {
- rows: rows,
+ rows,
titles: Box::new(None),
format: Box::new(*consts::FORMAT_DEFAULT),
}
@@ -377,10 +368,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)
}
@@ -388,12 +377,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);
}
diff --git a/src/row.rs b/src/row.rs
index dd3efcf..d3a9d86 100644
--- a/src/row.rs
+++ b/src/row.rs
@@ -20,7 +20,7 @@ pub struct Row {
impl Row {
/// Create a new `Row` backed with `cells` vector
pub fn new(cells: Vec<Cell>) -> Row {
- Row { cells: cells }
+ Row { cells }
}
/// Create an row of length `size`, with empty strings stored
diff --git a/src/utils.rs b/src/utils.rs
index dbd8db1..a3ce2ef 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -8,9 +8,9 @@ use unicode_width::UnicodeWidthStr;
use super::format::Alignment;
#[cfg(any(not(windows), not(feature="win_crlf")))]
-pub static NEWLINE: &'static [u8] = b"\n";
+pub static NEWLINE: &[u8] = b"\n";
#[cfg(all(windows, feature="win_crlf"))]
-pub static NEWLINE: &'static [u8] = b"\r\n";
+pub static NEWLINE: &[u8] = b"\r\n";
/// Internal utility for writing data into a string
pub struct StringWriter {
@@ -152,10 +152,10 @@ mod tests {
#[test]
fn string_writer() {
let mut out = StringWriter::new();
- out.write("foo".as_bytes()).unwrap();
- out.write(" ".as_bytes()).unwrap();
- out.write("".as_bytes()).unwrap();
- out.write("bar".as_bytes()).unwrap();
+ out.write_all(b"foo").unwrap();
+ out.write_all(b" ").unwrap();
+ out.write_all(b"").unwrap();
+ out.write_all(b"bar").unwrap();
assert_eq!(out.as_string(), "foo bar");
}
@@ -200,7 +200,7 @@ mod tests {
#[test]
fn utf8_error() {
let mut out = StringWriter::new();
- let res = out.write_all(&vec![0, 255]);
+ let res = out.write_all(&[0, 255]);
assert!(res.is_err());
}
}