diff options
author | Pierre-Henri Symoneaux <phsym@users.noreply.github.com> | 2016-02-15 09:53:40 +0100 |
---|---|---|
committer | Pierre-Henri Symoneaux <phsym@users.noreply.github.com> | 2016-02-15 09:53:40 +0100 |
commit | 9bb1b4add459ce08526792c1a9fd7c37a0553043 (patch) | |
tree | 892cda3e593e601c85b16934b37999388c2ef5c7 /src | |
parent | aad9884f9e8b34cf49d2bde042a978dff36bcb92 (diff) | |
parent | 2e97afa91ea1d790f00aa832f4471a4fb1bd53cc (diff) |
Merge pull request #23 from phsym/detect_tty
Detect if stdout is a tty, and don't colorize table if it's not
Diffstat (limited to 'src')
-rw-r--r-- | src/format.rs | 2 | ||||
-rw-r--r-- | src/lib.rs | 45 |
2 files changed, 40 insertions, 7 deletions
diff --git a/src/format.rs b/src/format.rs index 030242b..6a973aa 100644 --- a/src/format.rs +++ b/src/format.rs @@ -197,7 +197,7 @@ impl Default for TableFormat { }
}
-/// A builder to create a `Table Format`
+/// A builder to create a `TableFormat`
pub struct FormatBuilder {
format: Box<TableFormat>
}
@@ -1,6 +1,7 @@ //! A formatted and aligned table printer written in rust
extern crate unicode_width;
extern crate term;
+extern crate atty;
#[macro_use] extern crate lazy_static;
use std::io;
@@ -143,18 +144,34 @@ impl <'a> TableSlice<'a> { return self.__print(out, Row::print_term);
}
- /// Print the table to standard output
+ /// Print the table to standard output. Colors won't be displayed unless
+ /// stdout is a tty terminal, or `force_colorize` is set to `true`.
+ /// In ANSI terminals, colors are displayed using ANSI escape characters. When for example the
+ /// 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`.
/// # Panic
/// Panic if writing to standard output fails
- pub fn printstd(&self) {
- let r = match stdout() {
- Some(mut o) => self.print_term(&mut *o),
- None => self.print(&mut io::stdout()),
+ pub fn print_tty(&self, force_colorize: bool) {
+ let r = match (stdout(), atty::is() || 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);
}
}
+
+ /// Print the table to standard output. Colors won't be displayed unless
+ /// 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)`
+ /// # Panic
+ /// Panic if writing to standard output fails
+ pub fn printstd(&self) {
+ self.print_tty(false);
+ }
}
impl Table {
@@ -265,7 +282,23 @@ impl Table { return self.as_ref().print_term(out);
}
- /// Print the table to standard output
+ /// Print the table to standard output. Colors won't be displayed unless
+ /// stdout is a tty terminal, or `force_colorize` is set to `true`.
+ /// In ANSI terminals, colors are displayed using ANSI escape characters. When for example the
+ /// 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`.
+ /// # Panic
+ /// Panic if writing to standard output fails
+ pub fn print_tty(&self, force_colorize: bool) {
+ self.as_ref().print_tty(force_colorize);
+ }
+
+ /// Print the table to standard output. Colors won't be displayed unless
+ /// 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)`
/// # Panic
/// Panic if writing to standard output fails
pub fn printstd(&self) {
|