diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-11-03 18:27:49 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-11-03 18:29:56 +0100 |
commit | 9c60496bc59c69f3098435f607d5d86a91182f89 (patch) | |
tree | 15c2f02d092372baf4872f28f0490c4f0e0e4970 /src/db | |
parent | c3ef30dd543e805b73c005ea93f38b57ef0a7b4d (diff) |
Do the right thing when output is a pipe
This changes the display_as_table() function to automatically ignore the
headers and not printing a database, but simply the values as a
whitespace seperated list when stdout is a pipe.
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/db')
-rw-r--r-- | src/db/interface.rs | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/src/db/interface.rs b/src/db/interface.rs index 8df69f9..173bbf8 100644 --- a/src/db/interface.rs +++ b/src/db/interface.rs @@ -7,6 +7,7 @@ use anyhow::anyhow; use anyhow::Context; use anyhow::Error; use anyhow::Result; +use itertools::Itertools; use crate::config::Configuration; use crate::db::DbConnectionConfig; @@ -144,7 +145,7 @@ fn artifacts(conn_cfg: DbConnectionConfig) -> Result<()> { if data.is_empty() { info!("No artifacts in database"); } else { - display_as_table(hdrs, data); + display_as_table(hdrs, data)?; } Ok(()) @@ -188,25 +189,39 @@ fn envvars(conn_cfg: DbConnectionConfig) -> Result<()> { if data.is_empty() { info!("No environment variables in database"); } else { - display_as_table(hdrs, data); + display_as_table(hdrs, data)?; } Ok(()) } -fn display_as_table<D: Display>(headers: Vec<ascii_table::Column>, data: Vec<Vec<D>>) { - let mut ascii_table = ascii_table::AsciiTable::default(); +/// Display the passed data as nice ascii table, +/// or, if stdout is a pipe, print it nicely parseable +fn display_as_table<D: Display>(headers: Vec<ascii_table::Column>, data: Vec<Vec<D>>) -> Result<()> { + use std::io::Write; - ascii_table.max_width = terminal_size::terminal_size() - .map(|tpl| tpl.0.0 as usize) // an ugly interface indeed! - .unwrap_or(80); + if atty::is(atty::Stream::Stdout) { + let mut ascii_table = ascii_table::AsciiTable::default(); - headers.into_iter() - .enumerate() - .for_each(|(i, c)| { - ascii_table.columns.insert(i, c); - }); + ascii_table.max_width = terminal_size::terminal_size() + .map(|tpl| tpl.0.0 as usize) // an ugly interface indeed! + .unwrap_or(80); - ascii_table.print(data); + headers.into_iter() + .enumerate() + .for_each(|(i, c)| { + ascii_table.columns.insert(i, c); + }); + + ascii_table.print(data); + } else { + let mut out = std::io::stdout(); + let mut lock = out.lock(); + for list in data { + writeln!(lock, "{}", list.iter().map(|d| d.to_string()).join(" "))?; + } + } + + Ok(()) } |