From fc42d6b4bf3660fad3c081c499f0303241bb449f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 28 Dec 2015 13:56:01 +0100 Subject: [PATCH 1/4] FilePrinter: Add print_file_custom(), print_files_custom() --- src/ui/file.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/ui/file.rs b/src/ui/file.rs index b99726f4..6c9eaf0d 100644 --- a/src/ui/file.rs +++ b/src/ui/file.rs @@ -23,6 +23,21 @@ pub trait FilePrinter { } } + fn print_file_custom(&self, file: Rc>, f: &F) + where F: Fn(Rc>) -> String + { + info!("{}", f(file)); + } + + fn print_files_custom(&self, files: I, f: &F) + where I: Iterator>>, + F: Fn(Rc>) -> String + { + for file in files { + self.print_file_custom(file, f); + } + } + } struct DebugPrinter { From f0c186f33b76b942770c3990840e2d635584b945 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 28 Dec 2015 13:56:20 +0100 Subject: [PATCH 2/4] Impl FilePrinter for DebugPrinter ::print_file_custom() --- src/ui/file.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ui/file.rs b/src/ui/file.rs index 6c9eaf0d..43c46231 100644 --- a/src/ui/file.rs +++ b/src/ui/file.rs @@ -58,6 +58,14 @@ impl FilePrinter for DebugPrinter { } } + fn print_file_custom(&self, file: Rc>, f: &F) + where F: Fn(Rc>) -> String + { + if self.debug { + debug!("[DebugPrinter] ->\n{:?}", f(file)); + } + } + } struct SimplePrinter { From 6455c45630c97f5a1ae446c6ba4405b541afb094 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 28 Dec 2015 13:56:38 +0100 Subject: [PATCH 3/4] Impl FilePrinter for SimplePrinter ::print_file_custom() --- src/ui/file.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/ui/file.rs b/src/ui/file.rs index 43c46231..e9ab2e38 100644 --- a/src/ui/file.rs +++ b/src/ui/file.rs @@ -92,6 +92,19 @@ impl FilePrinter for SimplePrinter { } } + fn print_file_custom(&self, file: Rc>, f: &F) + where F: Fn(Rc>) -> String + { + let s = f(file); + if self.debug { + debug!("{:?}", s); + } else if self.verbose { + info!("{}", s); + } else { + info!("[File]: {}", s); + } + } + } pub struct TablePrinter { From 51605e1314637c137d98701662c85076241aec71 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 28 Dec 2015 13:56:53 +0100 Subject: [PATCH 4/4] Impl FilePrinter for TablePrinter ::print_files_custom() --- src/ui/file.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/ui/file.rs b/src/ui/file.rs index e9ab2e38..a45bb71e 100644 --- a/src/ui/file.rs +++ b/src/ui/file.rs @@ -158,4 +158,41 @@ impl FilePrinter for TablePrinter { } } + fn print_files_custom(&self, files: I, f: &F) + where I: Iterator>>, + F: Fn(Rc>) -> String + { + use prettytable::Table; + use prettytable::row::Row; + use prettytable::cell::Cell; + + let titles = row!["File#", "Owner", "ID", "..."]; + + let mut tab = Table::new(); + tab.set_titles(titles); + + let mut i = 0; + for file in files { + debug!("Printing file: {:?}", file); + i += 1; + let cell_i = Cell::new(&format!("{}", i)[..]); + let cell_o = Cell::new(&format!("{}", file.deref().borrow().owner_name())[..]); + + let id : String = file.deref().borrow().id().clone().into(); + let cell_id = Cell::new(&id[..]); + + let cell_extra = Cell::new(&f(file)[..]); + + let row = Row::new(vec![cell_i, cell_o, cell_id, cell_extra]); + tab.add_row(row); + } + + if i != 0 { + debug!("Printing {} table entries", i); + tab.printstd(); + } else { + debug!("Not printing table because there are zero entries"); + } + } + }