summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs67
1 files changed, 41 insertions, 26 deletions
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<T: Write + ?Sized, F>(&self, out: &mut T, f: F) -> Result<(), Error>
- where F: Fn(&Row, &mut T, &TableFormat, &[usize]) -> Result<(), Error>
+ fn __print<T: Write + ?Sized, F>(&self, out: &mut T, f: F) -> Result<usize, Error>
+ where F: Fn(&Row, &mut T, &TableFormat, &[usize]) -> Result<usize, Error>
{
+ 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<T: Write + ?Sized>(&self, out: &mut T) -> Result<(), Error> {
+ /// Print the table to `out` and returns the number of
+ /// line printed, or an error
+ pub fn print<T: Write + ?Sized>(&self, out: &mut T) -> Result<usize, Error> {
self.__print(out, Row::print)
}
- /// Print the table to terminal `out`, applying styles when needed
- pub fn print_term<T: Terminal + ?Sized>(&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<T: Terminal + ?Sized>(&self, out: &mut T) -> Result<usize, Error> {
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<T: Write + ?Sized>(&self, out: &mut T) -> Result<(), Error> {
+ /// Print the table to `out` and returns the number
+ /// of lines printed, or an error
+ pub fn print<T: Write + ?Sized>(&self, out: &mut T) -> Result<usize, Error> {
self.as_ref().print(out)
}
- /// Print the table to terminal `out`, applying styles when needed
- pub fn print_term<T: Terminal + ?Sized>(&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<T: Terminal + ?Sized>(&self, out: &mut T) -> Result<usize, Error> {
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()
}
}