Skip to content

Commit 6bb234c

Browse files
authored
Merge pull request #114 from phsym/printstd-nopanic
Remove panic in print_tty and return a Result
2 parents bdfb150 + 2040ec8 commit 6bb234c

File tree

8 files changed

+35
-51
lines changed

8 files changed

+35
-51
lines changed

examples/tictactoe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {
1414
[EMPTY, EMPTY, EMPTY],
1515
[EMPTY, EMPTY, EMPTY]
1616
];
17-
let mut height = table.printstd();
17+
let mut height = table.print_tty(false).unwrap();
1818
let stdin = io::stdin();
1919
let mut stdout = io::stdout();
2020
let mut current = CROSS;
@@ -53,7 +53,7 @@ fn main() {
5353
terminal.cursor_up().unwrap();
5454
terminal.delete_line().unwrap();
5555
}
56-
height = table.printstd();
56+
height = table.print_tty(false).unwrap();
5757
if check(&table) {
5858
return;
5959
}

src/cell.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ impl Cell {
230230
for a in &self.style {
231231
match out.attr(*a) {
232232
Ok(..) | Err(::term::Error::NotSupported) | Err(::term::Error::ColorOutOfRange) => {
233-
()
234-
} // Ignore unsupported atrributes
233+
} // Ignore unsupported attributes
235234
Err(e) => return Err(term_error_to_io_error(e)),
236235
};
237236
}

src/csv.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ impl super::Table {
8383
mod tests {
8484
use crate::{Table, Row, Cell};
8585

86-
static CSV_S: &'static str = "ABC,DEFG,HIJKLMN\n\
87-
foobar,bar,foo\n\
88-
foobar2,bar2,foo2\n";
86+
static CSV_S: &str = "ABC,DEFG,HIJKLMN\n\
87+
foobar,bar,foo\n\
88+
foobar2,bar2,foo2\n";
8989

9090
fn test_table() -> Table {
9191
let mut table = Table::new();

src/format.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ impl LineSeparator {
6060
/// and `junc` is the one used for junctions between columns and lines
6161
pub fn new(line: char, junc: char, ljunc: char, rjunc: char) -> LineSeparator {
6262
LineSeparator {
63-
line: line,
64-
junc: junc,
65-
ljunc: ljunc,
66-
rjunc: rjunc,
63+
line,
64+
junc,
65+
ljunc,
66+
rjunc,
6767
}
6868
}
6969

@@ -80,7 +80,7 @@ impl LineSeparator {
8080
if lborder {
8181
out.write_all(Utf8Char::from(self.ljunc).as_bytes())?;
8282
}
83-
let mut iter = col_width.into_iter().peekable();
83+
let mut iter = col_width.iter().peekable();
8484
while let Some(width) = iter.next() {
8585
for _ in 0..width + padding.0 + padding.1 {
8686
out.write_all(Utf8Char::from(self.line).as_bytes())?;

src/lib.rs

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
unused_import_braces,
44
unused_qualifications)]
55
//! A formatted and aligned table printer written in rust
6-
//!
6+
77
#[macro_use]
88
extern crate lazy_static;
99

@@ -148,7 +148,7 @@ impl<'a> TableSlice<'a> {
148148
.print_line_separator(out, &col_width, LinePosition::Title)?;
149149
}
150150
// Print rows
151-
let mut iter = self.rows.into_iter().peekable();
151+
let mut iter = self.rows.iter().peekable();
152152
while let Some(r) = iter.next() {
153153
height += f(r, out, self.format, &col_width)?;
154154
if iter.peek().is_some() {
@@ -181,31 +181,22 @@ impl<'a> TableSlice<'a> {
181181
/// as not beeing tty, and ANSI escape characters won't be displayed unless `force colorize`
182182
/// is set to `true`.
183183
/// # Returns
184-
/// The number of lines printed
185-
/// # Panic
186-
/// Panic if writing to standard output fails
187-
pub fn print_tty(&self, force_colorize: bool) -> usize {
188-
let r = match (stdout(), atty::is(atty::Stream::Stdout) || force_colorize) {
184+
/// A `Result` holding the number of lines printed, or an `io::Error` if any failure happens
185+
pub fn print_tty(&self, force_colorize: bool) -> Result<usize, Error> {
186+
match (stdout(), atty::is(atty::Stream::Stdout) || force_colorize) {
189187
(Some(mut o), true) => self.print_term(&mut *o),
190188
_ => self.print(&mut io::stdout()),
191-
};
192-
match r {
193-
Err(e) => panic!("Cannot print table to standard output : {}", e),
194-
Ok(height) => height
195189
}
196190
}
197191

198192
/// Print the table to standard output. Colors won't be displayed unless
199193
/// stdout is a tty terminal. This means that if stdout is redirected to a file, or piped
200194
/// to another program, no color will be displayed.
201195
/// To force colors rendering, use `print_tty()` method.
202-
/// Calling `printstd()` is equivalent to calling `print_tty(false)`
203-
/// # Returns
204-
/// The number of lines printed
205-
/// # Panic
206-
/// Panic if writing to standard output fails
207-
pub fn printstd(&self) -> usize {
208-
self.print_tty(false)
196+
/// Any failure to print is ignored. For better control, use `print_tty()`.
197+
/// Calling `printstd()` is equivalent to calling `print_tty(false)` and ignoring the result.
198+
pub fn printstd(&self) {
199+
let _ = self.print_tty(false); // Ignore result
209200
}
210201

211202
/// Print table in HTML format to `out`.
@@ -248,7 +239,7 @@ impl Table {
248239
/// Create a table initialized with `rows`
249240
pub fn init(rows: Vec<Row>) -> Table {
250241
Table {
251-
rows: rows,
242+
rows,
252243
titles: Box::new(None),
253244
format: Box::new(*consts::FORMAT_DEFAULT),
254245
}
@@ -377,23 +368,18 @@ impl Table {
377368
/// as not beeing tty, and ANSI escape characters won't be displayed unless `force colorize`
378369
/// is set to `true`.
379370
/// # Returns
380-
/// The number of lines printed
381-
/// # Panic
382-
/// Panic if writing to standard output fails
383-
pub fn print_tty(&self, force_colorize: bool) -> usize {
371+
/// A `Result` holding the number of lines printed, or an `io::Error` if any failure happens
372+
pub fn print_tty(&self, force_colorize: bool) -> Result<usize, Error> {
384373
self.as_ref().print_tty(force_colorize)
385374
}
386375

387376
/// Print the table to standard output. Colors won't be displayed unless
388377
/// stdout is a tty terminal. This means that if stdout is redirected to a file, or piped
389378
/// to another program, no color will be displayed.
390379
/// To force colors rendering, use `print_tty()` method.
391-
/// Calling `printstd()` is equivalent to calling `print_tty(false)`
392-
/// # Returns
393-
/// The number of lines printed
394-
/// # Panic
395-
/// Panic if writing to standard output fails
396-
pub fn printstd(&self) -> usize {
380+
/// Any failure to print is ignored. For better control, use `print_tty()`.
381+
/// Calling `printstd()` is equivalent to calling `print_tty(false)` and ignoring the result.
382+
pub fn printstd(&self) {
397383
self.as_ref().printstd()
398384
}
399385

src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ fn main() {
4545
table.set_titles(row!["Title 1", "Title 2"]);
4646
table.set_format(*consts::FORMAT_DEFAULT);
4747
table.get_format().indent(8);
48-
let size = table.printstd();
49-
println!("Table height = {}", size);
48+
table.printstd();
5049
// println!("{:#?}", table);
5150
}

src/row.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct Row {
2020
impl Row {
2121
/// Create a new `Row` backed with `cells` vector
2222
pub fn new(cells: Vec<Cell>) -> Row {
23-
Row { cells: cells }
23+
Row { cells }
2424
}
2525

2626
/// Create an row of length `size`, with empty strings stored

src/utils.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use unicode_width::UnicodeWidthStr;
88
use super::format::Alignment;
99

1010
#[cfg(any(not(windows), not(feature="win_crlf")))]
11-
pub static NEWLINE: &'static [u8] = b"\n";
11+
pub static NEWLINE: &[u8] = b"\n";
1212
#[cfg(all(windows, feature="win_crlf"))]
13-
pub static NEWLINE: &'static [u8] = b"\r\n";
13+
pub static NEWLINE: &[u8] = b"\r\n";
1414

1515
/// Internal utility for writing data into a string
1616
pub struct StringWriter {
@@ -152,10 +152,10 @@ mod tests {
152152
#[test]
153153
fn string_writer() {
154154
let mut out = StringWriter::new();
155-
out.write("foo".as_bytes()).unwrap();
156-
out.write(" ".as_bytes()).unwrap();
157-
out.write("".as_bytes()).unwrap();
158-
out.write("bar".as_bytes()).unwrap();
155+
out.write_all(b"foo").unwrap();
156+
out.write_all(b" ").unwrap();
157+
out.write_all(b"").unwrap();
158+
out.write_all(b"bar").unwrap();
159159
assert_eq!(out.as_string(), "foo bar");
160160
}
161161

@@ -200,7 +200,7 @@ mod tests {
200200
#[test]
201201
fn utf8_error() {
202202
let mut out = StringWriter::new();
203-
let res = out.write_all(&vec![0, 255]);
203+
let res = out.write_all(&[0, 255]);
204204
assert!(res.is_err());
205205
}
206206
}

0 commit comments

Comments
 (0)