From 1803803c74ec764a488cd84cb59c145d17639537 Mon Sep 17 00:00:00 2001 From: Pierre-Henri Symoneaux Date: Sat, 22 Sep 2018 23:26:57 +0200 Subject: Printing a tables returns the number of printed lines --- src/format.rs | 30 ++++++++++---------------- src/lib.rs | 67 ++++++++++++++++++++++++++++++++++++----------------------- src/main.rs | 3 ++- src/row.rs | 15 ++++++------- 4 files changed, 62 insertions(+), 53 deletions(-) diff --git a/src/format.rs b/src/format.rs index 45405ca..7cf644e 100644 --- a/src/format.rs +++ b/src/format.rs @@ -67,26 +67,16 @@ impl LineSeparator { } } - /// Print a full line separator to `out`. `col_width` is a slice containing the width of each column - #[deprecated(since = "0.6.7", note = "function will become private. See [issue #57](https://github.com/phsym/prettytable-rs/pull/57) and [issue #87](https://github.com/phsym/prettytable-rs/issues/87).")] - pub fn print(&self, - out: &mut T, - col_width: &[usize], - colsep: bool, - lborder: bool, - rborder: bool) - -> Result<(), Error> { - self._print(out, col_width, (1, 1), colsep, lborder, rborder) - } - - fn _print(&self, + /// Print a full line separator to `out`. `col_width` is a slice containing the width of each column. + /// Returns the number of printed lines + fn print(&self, out: &mut T, col_width: &[usize], padding: (usize, usize), colsep: bool, lborder: bool, rborder: bool) - -> Result<(), Error> { + -> Result { if lborder { out.write_all(Utf8Char::from(self.ljunc).as_bytes())?; } @@ -102,7 +92,8 @@ impl LineSeparator { if rborder { out.write_all(Utf8Char::from(self.rjunc).as_bytes())?; } - out.write_all(NEWLINE) + out.write_all(NEWLINE)?; + Ok(1) } } @@ -227,25 +218,26 @@ impl TableFormat { self.indent } - /// Print a full line separator to `out`. `col_width` is a slice containing the width of each column + /// Print a full line separator to `out`. `col_width` is a slice containing the width of each column. + /// Returns the number of printed lines #[deprecated(since="0.8.0", note="Will become private in future release. See [issue #87](https://github.com/phsym/prettytable-rs/issues/87)")] pub fn print_line_separator(&self, out: &mut T, col_width: &[usize], pos: LinePosition) - -> Result<(), Error> { + -> Result { match *self.get_sep_for_line(pos) { Some(ref l) => { //TODO: Wrap this into dedicated function one day out.write_all(&vec![b' '; self.get_indent()])?; - l._print(out, + l.print(out, col_width, self.get_padding(), self.csep.is_some(), self.lborder.is_some(), self.rborder.is_some()) } - None => Ok(()), + None => Ok(0), } } diff --git a/src/lib.rs b/src/lib.rs index e539b5b..bbc7647 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -135,39 +135,43 @@ impl<'a> TableSlice<'a> { } /// Internal only - fn __print(&self, out: &mut T, f: F) -> Result<(), Error> - where F: Fn(&Row, &mut T, &TableFormat, &[usize]) -> Result<(), Error> + fn __print(&self, out: &mut T, f: F) -> Result + where F: Fn(&Row, &mut T, &TableFormat, &[usize]) -> Result { + let mut height = 0; // Compute columns width let col_width = self.get_all_column_width(); - self.format + height += self.format .print_line_separator(out, &col_width, LinePosition::Top)?; if let Some(ref t) = *self.titles { - f(t, out, self.format, &col_width)?; - self.format + height += f(t, out, self.format, &col_width)?; + height += self.format .print_line_separator(out, &col_width, LinePosition::Title)?; } // Print rows let mut iter = self.rows.into_iter().peekable(); while let Some(r) = iter.next() { - f(r, out, self.format, &col_width)?; + height += f(r, out, self.format, &col_width)?; if iter.peek().is_some() { - self.format + height += self.format .print_line_separator(out, &col_width, LinePosition::Intern)?; } } - self.format + height += self.format .print_line_separator(out, &col_width, LinePosition::Bottom)?; - out.flush() + out.flush()?; + Ok(height) } - /// Print the table to `out` - pub fn print(&self, out: &mut T) -> Result<(), Error> { + /// Print the table to `out` and returns the number of + /// line printed, or an error + pub fn print(&self, out: &mut T) -> Result { self.__print(out, Row::print) } - /// Print the table to terminal `out`, applying styles when needed - pub fn print_term(&self, out: &mut T) -> Result<(), Error> { + /// Print the table to terminal `out`, applying styles when needed and returns the number of + /// line printed, or an error + pub fn print_term(&self, out: &mut T) -> Result { self.__print(out, Row::print_term) } @@ -177,15 +181,18 @@ impl<'a> TableSlice<'a> { /// output is redirected to a file, or piped to another program, the output is considered /// 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) { + pub fn print_tty(&self, force_colorize: bool) -> usize { let r = match (stdout(), atty::is(atty::Stream::Stdout) || force_colorize) { (Some(mut o), true) => self.print_term(&mut *o), _ => self.print(&mut io::stdout()), }; - if let Err(e) = r { - panic!("Cannot print table to standard output : {}", e); + match r { + Err(e) => panic!("Cannot print table to standard output : {}", e), + Ok(height) => height } } @@ -194,10 +201,12 @@ impl<'a> TableSlice<'a> { /// 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) { - self.print_tty(false); + pub fn printstd(&self) -> usize { + self.print_tty(false) } } @@ -328,13 +337,15 @@ impl Table { self.rows.iter_mut() } - /// Print the table to `out` - pub fn print(&self, out: &mut T) -> Result<(), Error> { + /// Print the table to `out` and returns the number + /// of lines printed, or an error + pub fn print(&self, out: &mut T) -> Result { self.as_ref().print(out) } - /// Print the table to terminal `out`, applying styles when needed - pub fn print_term(&self, out: &mut T) -> Result<(), Error> { + /// Print the table to terminal `out`, applying styles when needed and returns the number + /// of lines printed, or an error + pub fn print_term(&self, out: &mut T) -> Result { self.as_ref().print_term(out) } @@ -344,10 +355,12 @@ impl Table { /// output is redirected to a file, or piped to another program, the output is considered /// 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) { - self.as_ref().print_tty(force_colorize); + pub fn print_tty(&self, force_colorize: bool) -> usize { + self.as_ref().print_tty(force_colorize) } /// Print the table to standard output. Colors won't be displayed unless @@ -355,10 +368,12 @@ impl Table { /// 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) { - self.as_ref().printstd(); + pub fn printstd(&self) -> usize { + self.as_ref().printstd() } } diff --git a/src/main.rs b/src/main.rs index af9b745..3cf20c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,7 @@ fn main() { table.set_titles(row!["Title 1", "Title 2"]); table.set_format(*consts::FORMAT_DEFAULT); table.get_format().indent(8); - table.printstd(); + let size = table.printstd(); + println!("Table height = {}", size); // println!("{:#?}", table); } diff --git a/src/row.rs b/src/row.rs index 775a3d8..181793d 100644 --- a/src/row.rs +++ b/src/row.rs @@ -147,10 +147,11 @@ impl Row { format: &TableFormat, col_width: &[usize], f: F) - -> Result<(), Error> + -> Result where F: Fn(&Cell, &mut T, usize, usize, bool) -> Result<(), Error> { - for i in 0..self.get_height() { + let height = self.get_height(); + for i in 0..height { //TODO: Wrap this into dedicated function one day out.write_all(&vec![b' '; format.get_indent()])?; format.print_column_separator(out, ColumnPosition::Left)?; @@ -184,28 +185,28 @@ impl Row { format.print_column_separator(out, ColumnPosition::Right)?; out.write_all(NEWLINE)?; } - Ok(()) + Ok(height) } /// Print the row to `out`, with `separator` as column separator, and `col_width` - /// specifying the width of each columns + /// specifying the width of each columns. Returns the number of printed lines #[deprecated(since="0.8.0", note="Will become private in future release. See [issue #87](https://github.com/phsym/prettytable-rs/issues/87)")] pub fn print(&self, out: &mut T, format: &TableFormat, col_width: &[usize]) - -> Result<(), Error> { + -> Result { self.__print(out, format, col_width, Cell::print) } /// Print the row to terminal `out`, with `separator` as column separator, and `col_width` - /// specifying the width of each columns. Apply style when needed + /// specifying the width of each columns. Apply style when needed. returns the number of printed lines #[deprecated(since="0.8.0", note="Will become private in future release. See [issue #87](https://github.com/phsym/prettytable-rs/issues/87)")] pub fn print_term(&self, out: &mut T, format: &TableFormat, col_width: &[usize]) - -> Result<(), Error> { + -> Result { self.__print(out, format, col_width, Cell::print_term) } } -- cgit v1.2.3