From 71f37f69c3a4e19fe8e6cf8a56e8c386f795deb2 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 6 Jan 2016 19:31:20 +0100 Subject: [PATCH 1/4] Move ::new() out of FilePrinter trait --- src/module/notes/mod.rs | 1 - src/ui/file.rs | 28 +++++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/module/notes/mod.rs b/src/module/notes/mod.rs index e0e107a3..0b445e40 100644 --- a/src/module/notes/mod.rs +++ b/src/module/notes/mod.rs @@ -299,7 +299,6 @@ impl<'a> Notes<'a> { fn command_links(&self, matches: &ArgMatches) -> bool { use ansi_term::Colour::{Red, Green}; use module::helpers::content::markdown::MarkdownParser; - use ui::file::FilePrinter; use util::is_url; use prettytable::Table; use prettytable::row::Row; diff --git a/src/ui/file.rs b/src/ui/file.rs index ce1cd27e..d9151e6c 100644 --- a/src/ui/file.rs +++ b/src/ui/file.rs @@ -10,8 +10,6 @@ use storage::file::File; */ pub trait FilePrinter { - fn new(verbose: bool, debug: bool) -> Self; - /* * Print a single file */ @@ -50,14 +48,18 @@ struct DebugPrinter { debug: bool, } -impl FilePrinter for DebugPrinter { +impl DebugPrinter { - fn new(_: bool, debug: bool) -> DebugPrinter { + pub fn new(debug: bool) -> DebugPrinter { DebugPrinter { debug: debug, } } +} + +impl FilePrinter for DebugPrinter { + fn print_file(&self, f: Rc>) { if self.debug { debug!("[DebugPrinter] ->\n{:?}", f); @@ -82,15 +84,19 @@ struct SimplePrinter { debug: bool, } -impl FilePrinter for SimplePrinter { +impl SimplePrinter { - fn new(verbose: bool, debug: bool) -> SimplePrinter { + pub fn new(verbose: bool, debug: bool) -> SimplePrinter { SimplePrinter { debug: debug, verbose: verbose, } } +} + +impl FilePrinter for SimplePrinter { + fn print_file(&self, f: Rc>) { use ansi_term::Colour::Cyan; @@ -127,14 +133,18 @@ pub struct TablePrinter { sp: SimplePrinter, } -impl FilePrinter for TablePrinter { +impl TablePrinter { - fn new(verbose: bool, debug: bool) -> TablePrinter { + pub fn new(verbose: bool, debug: bool) -> TablePrinter { TablePrinter { - sp: SimplePrinter::new(verbose, debug), + sp: SimplePrinter::new(verbose, debug), } } +} + +impl FilePrinter for TablePrinter { + fn print_file(&self, f: Rc>) { self.sp.print_file(f); } From 56730cb7b8559c560745b80b97c4407893d80cde Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 6 Jan 2016 19:37:52 +0100 Subject: [PATCH 2/4] Add support for printing table without borders --- src/module/bm/mod.rs | 2 +- src/module/notes/mod.rs | 2 +- src/ui/file.rs | 22 ++++++++++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/module/bm/mod.rs b/src/module/bm/mod.rs index 6f25bc8d..ffe45ee9 100644 --- a/src/module/bm/mod.rs +++ b/src/module/bm/mod.rs @@ -130,7 +130,7 @@ impl<'a> BM<'a> { .load_for_module(self, &self.parser) .into_iter() .filter(|file| filter.filter_file(file)); - let printer = TablePrinter::new(self.rt.is_verbose(), self.rt.is_debugging()); + let printer = TablePrinter::new(self.rt.is_verbose(), self.rt.is_debugging(), true); printer.print_files_custom(files, &|file| { diff --git a/src/module/notes/mod.rs b/src/module/notes/mod.rs index 0b445e40..5647e85d 100644 --- a/src/module/notes/mod.rs +++ b/src/module/notes/mod.rs @@ -274,7 +274,7 @@ impl<'a> Notes<'a> { hash_filter.or(Box::new(head_filter)).and(Box::new(text_filter)).and(Box::new(tags_filter)) }; - let printer = TablePrinter::new(self.rt.is_verbose(), self.rt.is_debugging()); + let printer = TablePrinter::new(self.rt.is_verbose(), self.rt.is_debugging(), true); printer.print_files_custom( self.rt.store() diff --git a/src/ui/file.rs b/src/ui/file.rs index d9151e6c..ab9e7943 100644 --- a/src/ui/file.rs +++ b/src/ui/file.rs @@ -130,14 +130,16 @@ impl FilePrinter for SimplePrinter { * Table printer to print file information in a nice ASCII-table */ pub struct TablePrinter { - sp: SimplePrinter, + sp: SimplePrinter, + pretty: bool, } impl TablePrinter { - pub fn new(verbose: bool, debug: bool) -> TablePrinter { + pub fn new(verbose: bool, debug: bool, pretty: bool) -> TablePrinter { TablePrinter { sp: SimplePrinter::new(verbose, debug), + pretty: pretty, } } @@ -151,12 +153,20 @@ impl FilePrinter for TablePrinter { fn print_files>>>(&self, files: I) { use prettytable::Table; + use prettytable::format::TableFormat; use prettytable::row::Row; use prettytable::cell::Cell; let titles = row!["File#", "Owner", "ID"]; let mut tab = Table::new(); + + if !self.pretty { + let plain_format = TableFormat::new(' ', None, None); + debug!("Setting plain format for table"); + tab.set_format(plain_format); + } + tab.set_titles(titles); let mut i = 0; @@ -185,12 +195,20 @@ impl FilePrinter for TablePrinter { F: Fn(Rc>) -> Vec { use prettytable::Table; + use prettytable::format::TableFormat; use prettytable::row::Row; use prettytable::cell::Cell; let titles = row!["#", "Module", "ID", "..."]; let mut tab = Table::new(); + + if !self.pretty { + let plain_format = TableFormat::new(' ', None, None); + debug!("Setting plain format for table"); + tab.set_format(plain_format); + } + tab.set_titles(titles); let mut i = 0; From 75dfc2b097b5dc4cd04f3a1baefef0519c1e68dc Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 6 Jan 2016 19:54:21 +0100 Subject: [PATCH 3/4] Notes: Add support for un-pretty tables --- etc/cli.yml | 6 ++++++ src/module/notes/mod.rs | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/etc/cli.yml b/etc/cli.yml index 553a8f52..0889605f 100644 --- a/etc/cli.yml +++ b/etc/cli.yml @@ -397,6 +397,12 @@ subcommands: version: 0.1 author: Matthias Beyer args: + - pretty: + long: pretty + help: Print table with ASCII-border + required: false + takes_value: false + - namegrep: short: n long: name diff --git a/src/module/notes/mod.rs b/src/module/notes/mod.rs index 5647e85d..b016b8c3 100644 --- a/src/module/notes/mod.rs +++ b/src/module/notes/mod.rs @@ -274,7 +274,9 @@ impl<'a> Notes<'a> { hash_filter.or(Box::new(head_filter)).and(Box::new(text_filter)).and(Box::new(tags_filter)) }; - let printer = TablePrinter::new(self.rt.is_verbose(), self.rt.is_debugging(), true); + let pretty = matches.is_present("pretty"); + debug!("Printing pretty table = {}", pretty); + let printer = TablePrinter::new(self.rt.is_verbose(), self.rt.is_debugging(), pretty); printer.print_files_custom( self.rt.store() From 24c150852c7a3f30c78f55097950d6fb4331c7ad Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 6 Jan 2016 19:56:06 +0100 Subject: [PATCH 4/4] BM: Add support for un-pretty tables --- etc/cli.yml | 6 ++++++ src/module/bm/mod.rs | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/etc/cli.yml b/etc/cli.yml index 0889605f..66743733 100644 --- a/etc/cli.yml +++ b/etc/cli.yml @@ -91,6 +91,12 @@ subcommands: version: 0.1 author: Matthias Beyer args: + - pretty: + long: pretty + help: Print table with ASCII-border + required: false + takes_value: false + - match: short: m long: match diff --git a/src/module/bm/mod.rs b/src/module/bm/mod.rs index ffe45ee9..f0663290 100644 --- a/src/module/bm/mod.rs +++ b/src/module/bm/mod.rs @@ -130,7 +130,8 @@ impl<'a> BM<'a> { .load_for_module(self, &self.parser) .into_iter() .filter(|file| filter.filter_file(file)); - let printer = TablePrinter::new(self.rt.is_verbose(), self.rt.is_debugging(), true); + let pretty = matches.is_present("pretty"); + let printer = TablePrinter::new(self.rt.is_verbose(), self.rt.is_debugging(), pretty); printer.print_files_custom(files, &|file| {