From 9c13c276fb0f556f159ea90d632c4c9cd830cab1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 23 Mar 2018 15:58:45 +0100 Subject: [PATCH] Do table output by default --- bin/core/imag-link/Cargo.toml | 1 + bin/core/imag-link/src/main.rs | 39 ++++++++++++++++++++++++---------- bin/core/imag-link/src/ui.rs | 7 ++++++ 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/bin/core/imag-link/Cargo.toml b/bin/core/imag-link/Cargo.toml index 607f5a08..3c2d3a37 100644 --- a/bin/core/imag-link/Cargo.toml +++ b/bin/core/imag-link/Cargo.toml @@ -26,6 +26,7 @@ log = "0.4.0" url = "1.5" toml = "0.4" toml-query = "0.6" +prettytable-rs = "0.6" libimagstore = { version = "0.7.0", path = "../../../lib/core/libimagstore" } libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" } diff --git a/bin/core/imag-link/src/main.rs b/bin/core/imag-link/src/main.rs index 7f67b53e..d68142bc 100644 --- a/bin/core/imag-link/src/main.rs +++ b/bin/core/imag-link/src/main.rs @@ -35,6 +35,7 @@ #[macro_use] extern crate log; extern crate clap; extern crate url; +#[macro_use] extern crate prettytable; #[cfg(test)] extern crate toml; #[cfg(test)] extern crate toml_query; #[cfg(test)] extern crate env_logger; @@ -263,23 +264,28 @@ fn list_linkings(rt: &Runtime) { .unwrap(); // safed by clap let list_externals = cmd.is_present("list-externals-too"); + let list_plain = cmd.is_present("list-plain"); + + let mut tab = ::prettytable::Table::new(); + tab.set_titles(row!["#", "Link"]); for entry in cmd.values_of("entries").unwrap() { // safed by clap match rt.store().get(PathBuf::from(entry)) { Ok(Some(entry)) => { - let mut i = 0; - - for link in entry.get_internal_links().map_err_trace_exit_unwrap(1) { + for (i, link) in entry.get_internal_links().map_err_trace_exit_unwrap(1).enumerate() { let link = link .to_str() .map_warn_err(|e| format!("Failed to convert StoreId to string: {:?}", e)) .ok(); if let Some(link) = link { - let _ = writeln!(rt.stdout(), "{: <3}: {}", i, link) - .to_exit_code() - .unwrap_or_exit(); - i += 1; + if list_plain { + let _ = writeln!(rt.stdout(), "{: <3}: {}", i, link) + .to_exit_code() + .unwrap_or_exit(); + } else { + tab.add_row(row![i, link]); + } } } @@ -292,10 +298,13 @@ fn list_linkings(rt: &Runtime) { .map_err_trace_exit_unwrap(1) .into_string(); - let _ = writeln!(rt.stdout(), "{: <3}: {}", i, link) - .to_exit_code() - .unwrap_or_exit(); - + if list_plain { + let _ = writeln!(rt.stdout(), "{: <3}: {}", i, link) + .to_exit_code() + .unwrap_or_exit(); + } else { + tab.add_row(row![i, link]); + } }) } }, @@ -303,6 +312,14 @@ fn list_linkings(rt: &Runtime) { Err(e) => trace_error(&e), } } + + if !list_plain { + let out = rt.stdout(); + let mut lock = out.lock(); + tab.print(&mut lock) + .to_exit_code() + .unwrap_or_exit(); + } } #[cfg(test)] diff --git a/bin/core/imag-link/src/ui.rs b/bin/core/imag-link/src/ui.rs index f43812ce..3f3136eb 100644 --- a/bin/core/imag-link/src/ui.rs +++ b/bin/core/imag-link/src/ui.rs @@ -67,6 +67,13 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .takes_value(false) .required(false) .help("Also list external links (debugging helper that might be removed at some point")) + + .arg(Arg::with_name("list-plain") + .long("plain") + .multiple(false) + .takes_value(false) + .required(false) + .help("List plain rather than in ASCII table")) ) .arg(Arg::with_name("check-consistency")