|
3 | 3 | unused_import_braces, |
4 | 4 | unused_qualifications)] |
5 | 5 | //! A formatted and aligned table printer written in rust |
6 | | -//! |
| 6 | +
|
7 | 7 | #[macro_use] |
8 | 8 | extern crate lazy_static; |
9 | 9 |
|
@@ -148,7 +148,7 @@ impl<'a> TableSlice<'a> { |
148 | 148 | .print_line_separator(out, &col_width, LinePosition::Title)?; |
149 | 149 | } |
150 | 150 | // Print rows |
151 | | - let mut iter = self.rows.into_iter().peekable(); |
| 151 | + let mut iter = self.rows.iter().peekable(); |
152 | 152 | while let Some(r) = iter.next() { |
153 | 153 | height += f(r, out, self.format, &col_width)?; |
154 | 154 | if iter.peek().is_some() { |
@@ -181,31 +181,22 @@ impl<'a> TableSlice<'a> { |
181 | 181 | /// as not beeing tty, and ANSI escape characters won't be displayed unless `force colorize` |
182 | 182 | /// is set to `true`. |
183 | 183 | /// # 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) { |
189 | 187 | (Some(mut o), true) => self.print_term(&mut *o), |
190 | 188 | _ => self.print(&mut io::stdout()), |
191 | | - }; |
192 | | - match r { |
193 | | - Err(e) => panic!("Cannot print table to standard output : {}", e), |
194 | | - Ok(height) => height |
195 | 189 | } |
196 | 190 | } |
197 | 191 |
|
198 | 192 | /// Print the table to standard output. Colors won't be displayed unless |
199 | 193 | /// stdout is a tty terminal. This means that if stdout is redirected to a file, or piped |
200 | 194 | /// to another program, no color will be displayed. |
201 | 195 | /// 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 |
209 | 200 | } |
210 | 201 |
|
211 | 202 | /// Print table in HTML format to `out`. |
@@ -248,7 +239,7 @@ impl Table { |
248 | 239 | /// Create a table initialized with `rows` |
249 | 240 | pub fn init(rows: Vec<Row>) -> Table { |
250 | 241 | Table { |
251 | | - rows: rows, |
| 242 | + rows, |
252 | 243 | titles: Box::new(None), |
253 | 244 | format: Box::new(*consts::FORMAT_DEFAULT), |
254 | 245 | } |
@@ -377,23 +368,18 @@ impl Table { |
377 | 368 | /// as not beeing tty, and ANSI escape characters won't be displayed unless `force colorize` |
378 | 369 | /// is set to `true`. |
379 | 370 | /// # 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> { |
384 | 373 | self.as_ref().print_tty(force_colorize) |
385 | 374 | } |
386 | 375 |
|
387 | 376 | /// Print the table to standard output. Colors won't be displayed unless |
388 | 377 | /// stdout is a tty terminal. This means that if stdout is redirected to a file, or piped |
389 | 378 | /// to another program, no color will be displayed. |
390 | 379 | /// 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) { |
397 | 383 | self.as_ref().printstd() |
398 | 384 | } |
399 | 385 |
|
|
0 commit comments