Color some info!() output

This commit is contained in:
Matthias Beyer 2016-01-03 20:28:42 +01:00
parent e1407c75a3
commit d775750362
4 changed files with 42 additions and 21 deletions

View file

@ -31,6 +31,8 @@ pub use module::bm::BM;
pub use module::notes::Notes; pub use module::notes::Notes;
fn main() { fn main() {
use ansi_term::Colour::Yellow;
let yaml = load_yaml!("../etc/cli.yml"); let yaml = load_yaml!("../etc/cli.yml");
let app = App::from_yaml(yaml); let app = App::from_yaml(yaml);
let config = CliConfig::new(app); let config = CliConfig::new(app);
@ -53,5 +55,5 @@ fn main() {
_ => false, _ => false,
}; };
info!("Module execution ended with {}", res); info!("{}", Yellow.paint(format!("Module execution ended with {}", res)));
} }

View file

@ -38,6 +38,7 @@ impl<'a> BM<'a> {
* Subcommand: add * Subcommand: add
*/ */
fn command_add(&self, matches: &ArgMatches) -> bool { fn command_add(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::{Green, Yellow, Red};
use std::process::exit; use std::process::exit;
use self::header::build_header; use self::header::build_header;
@ -69,7 +70,7 @@ impl<'a> BM<'a> {
.store() .store()
.load(self, &parser, &fileid) .load(self, &parser, &fileid)
.map(|file| { .map(|file| {
info!("Created file in memory: {}", fileid); info!("{}", Yellow.paint(format!("Created file in memory: {}", fileid)));
self.rt self.rt
.store() .store()
.persist(&parser, file) .persist(&parser, file)
@ -77,9 +78,9 @@ impl<'a> BM<'a> {
.unwrap_or(false); .unwrap_or(false);
if result { if result {
info!("Adding worked"); info!("{}", Red.paint("Adding worked"));
} else { } else {
info!("Adding failed"); info!("{}", Green.paint("Adding failed"));
} }
result result
@ -156,6 +157,7 @@ impl<'a> BM<'a> {
* Subcommand: open * Subcommand: open
*/ */
fn command_open(&self, matches: &ArgMatches) -> bool { fn command_open(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::{Green, Yellow, Red};
use open; use open;
let parser = Parser::new(JsonHeaderParser::new(None)); let parser = Parser::new(JsonHeaderParser::new(None));
@ -175,10 +177,10 @@ impl<'a> BM<'a> {
let f = file.deref().borrow(); let f = file.deref().borrow();
get_url_from_header(f.header()).map(|url| { get_url_from_header(f.header()).map(|url| {
if open::that(&url[..]).is_ok() { if open::that(&url[..]).is_ok() {
info!("open({})", url); info!("{}", Green.paint(format!("open({})", url)));
true true
} else { } else {
info!("could not open({})", url); info!("{}", Red.paint(format!("could not open({})", url)));
false false
} }
}) })
@ -194,8 +196,8 @@ impl<'a> BM<'a> {
}); });
let (succ, fail) = result; let (succ, fail) = result;
info!("open() succeeded for {} files", succ); info!("{}", Green.paint(format!("open() succeeded for {} files", succ)));
info!("open() failed for {} files", fail); info!("{}", Red.paint(format!( "open() failed for {} files", fail)));
return fail == 0; return fail == 0;
} }
@ -203,6 +205,8 @@ impl<'a> BM<'a> {
* Subcommand: remove * Subcommand: remove
*/ */
fn command_remove(&self, matches: &ArgMatches) -> bool { fn command_remove(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::{Green, Yellow, Red};
let parser = Parser::new(JsonHeaderParser::new(None)); let parser = Parser::new(JsonHeaderParser::new(None));
let filter = { let filter = {
@ -233,8 +237,8 @@ impl<'a> BM<'a> {
let (worked, failed) = result; let (worked, failed) = result;
info!("Removing succeeded for {} files", worked); info!("{}", Green.paint(format!("Removing succeeded for {} files", worked)));
info!("Removing failed for {} files", failed); info!("{}", Red.paint(format!( "Removing failed for {} files", failed)));
return failed == 0; return failed == 0;
} }
@ -291,6 +295,8 @@ impl<'a> BM<'a> {
impl<'a> Module<'a> for BM<'a> { impl<'a> Module<'a> for BM<'a> {
fn exec(&self, matches: &ArgMatches) -> bool { fn exec(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::{Green, Yellow, Red};
match matches.subcommand_name() { match matches.subcommand_name() {
Some("add") => { Some("add") => {
self.command_add(matches.subcommand_matches("add").unwrap()) self.command_add(matches.subcommand_matches("add").unwrap())
@ -321,7 +327,7 @@ impl<'a> Module<'a> for BM<'a> {
}, },
Some(_) | None => { Some(_) | None => {
info!("No command given, doing nothing"); info!("{}", Red.bold().paint("No command given, doing nothing"));
false false
}, },
} }

View file

@ -34,6 +34,7 @@ impl<'a> Notes<'a> {
fn command_add(&self, matches: &ArgMatches) -> bool { fn command_add(&self, matches: &ArgMatches) -> bool {
use std::process::exit; use std::process::exit;
use ansi_term::Colour::Yellow;
use self::header::build_header; use self::header::build_header;
use ui::external::editor::let_user_provide_content; use ui::external::editor::let_user_provide_content;
@ -57,13 +58,14 @@ impl<'a> Notes<'a> {
.store() .store()
.load(self, &parser, &fileid) .load(self, &parser, &fileid)
.and_then(|file| { .and_then(|file| {
info!("Created file in memory: {}", fileid); info!("{}", Yellow.paint(format!("Created file in memory: {}", fileid)));
Some(self.rt.store().persist(&parser, file)) Some(self.rt.store().persist(&parser, file))
}) })
.unwrap_or(false) .unwrap_or(false)
} }
fn command_edit(&self, matches: &ArgMatches) -> bool { fn command_edit(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::{Red, Green};
use ui::external::editor::edit_content; use ui::external::editor::edit_content;
let parser = Parser::new(JsonHeaderParser::new(None)); let parser = Parser::new(JsonHeaderParser::new(None));
@ -116,13 +118,14 @@ impl<'a> Notes<'a> {
let (worked, failed) = result; let (worked, failed) = result;
info!("Editing succeeded for {} files", worked); info!("{}", Green.paint(format!("Editing succeeded for {} files", worked)));
info!("Editing failed for {} files", failed); info!("{}", Red.paint(format!( "Editing failed for {} files", failed)));
return failed == 0; return failed == 0;
} }
fn command_list(&self, matches: &ArgMatches) -> bool { fn command_list(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::{Red, Green};
use ui::file::{FilePrinter, TablePrinter}; use ui::file::{FilePrinter, TablePrinter};
use self::header::get_name_from_header; use self::header::get_name_from_header;
use self::header::get_tags_from_header; use self::header::get_tags_from_header;
@ -162,6 +165,7 @@ impl<'a> Notes<'a> {
} }
fn command_links(&self, matches: &ArgMatches) -> bool { fn command_links(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::{Red, Green};
use module::helpers::content::markdown::MarkdownParser; use module::helpers::content::markdown::MarkdownParser;
use ui::file::{FilePrinter, TablePrinter}; use ui::file::{FilePrinter, TablePrinter};
use util::is_url; use util::is_url;
@ -260,13 +264,15 @@ impl<'a> Notes<'a> {
debug!("Not printing table as there wouldn't be any entries in it"); debug!("Not printing table as there wouldn't be any entries in it");
} }
info!("Listing links succeeded for {} files", worked); info!("{}", Green.paint(format!("Listing links succeeded for {} files", worked)));
info!("Listing links failed for {} files", failed); info!("{}", Red.paint( format!("Listing links failed for {} files", failed)));
return failed == 0; return failed == 0;
} }
fn command_remove(&self, matches: &ArgMatches) -> bool { fn command_remove(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::{Red, Green};
let parser = Parser::new(JsonHeaderParser::new(None)); let parser = Parser::new(JsonHeaderParser::new(None));
let filter = { let filter = {
@ -297,8 +303,9 @@ impl<'a> Notes<'a> {
let (worked, failed) = result; let (worked, failed) = result;
info!("Removing succeeded for {} files", worked);
info!("Removing failed for {} files", failed); info!("{}", Green.paint(format!("Removing succeeded for {} files", worked)));
info!("{}", Red.paint( format!("Removing failed for {} files", failed)));
return failed == 0; return failed == 0;
} }
@ -343,6 +350,8 @@ impl<'a> Notes<'a> {
impl<'a> Module<'a> for Notes<'a> { impl<'a> Module<'a> for Notes<'a> {
fn exec(&self, matches: &ArgMatches) -> bool { fn exec(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::Red;
match matches.subcommand_name() { match matches.subcommand_name() {
Some("add") => { Some("add") => {
self.command_add(matches.subcommand_matches("add").unwrap()) self.command_add(matches.subcommand_matches("add").unwrap())
@ -377,7 +386,7 @@ impl<'a> Module<'a> for Notes<'a> {
}, },
Some(_) | None => { Some(_) | None => {
info!("No command given, doing nothing"); info!("{}", Red.bold().paint("No command given, doing nothing"));
false false
}, },
} }

View file

@ -92,25 +92,29 @@ impl FilePrinter for SimplePrinter {
} }
fn print_file(&self, f: Rc<RefCell<File>>) { fn print_file(&self, f: Rc<RefCell<File>>) {
use ansi_term::Colour::Cyan;
if self.debug { if self.debug {
debug!("{:?}", f); debug!("{:?}", f);
} else if self.verbose { } else if self.verbose {
info!("{}", &*f.deref().borrow()); info!("{}", &*f.deref().borrow());
} else { } else {
info!("[File]: {}", f.deref().borrow().id()); info!("{}: {}", Cyan.paint("[File]"), f.deref().borrow().id());
} }
} }
fn print_file_custom<F>(&self, file: Rc<RefCell<File>>, f: &F) fn print_file_custom<F>(&self, file: Rc<RefCell<File>>, f: &F)
where F: Fn(Rc<RefCell<File>>) -> Vec<String> where F: Fn(Rc<RefCell<File>>) -> Vec<String>
{ {
use ansi_term::Colour::Cyan;
let s = f(file).join(" "); let s = f(file).join(" ");
if self.debug { if self.debug {
debug!("{:?}", s); debug!("{:?}", s);
} else if self.verbose { } else if self.verbose {
info!("{}", s); info!("{}", s);
} else { } else {
info!("[File]: {}", s); info!("{}: {}", Cyan.paint("[File]"), s);
} }
} }