Auto merge of #54 - matthiasbeyer:colored-output, r=matthiasbeyer

Colored output

Closes #38
This commit is contained in:
Homu 2016-01-04 04:29:22 +09:00
commit 31c1cf078b
6 changed files with 50 additions and 50 deletions

33
Cargo.lock generated
View file

@ -2,6 +2,7 @@
name = "imag"
version = "0.1.0"
dependencies = [
"ansi_term 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 1.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"config 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -12,10 +13,8 @@ dependencies = [
"open 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"prettytable-rs 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
"rustty 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"term 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
"term_grid 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"url 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
"uuid 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)",
@ -43,6 +42,11 @@ name = "ansi_term"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "ansi_term"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bitflags"
version = "0.3.2"
@ -114,11 +118,6 @@ dependencies = [
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.1.10"
@ -145,15 +144,6 @@ dependencies = [
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "nix"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "nom"
version = "0.3.11"
@ -212,17 +202,6 @@ name = "rustc-serialize"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rustty"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"nix 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"term 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde"
version = "0.6.1"

View file

@ -22,11 +22,10 @@ serde = "0.6.1"
serde_json = "0.6.0"
clap = { version = "1.4.5", features = ["yaml"] }
rustty = "0.1.9"
term = "0.2.12"
term_grid = "0.1.2"
prettytable-rs = "0.4.0"
open = "1.1.0"
itertools = "0.4.5"
hoedown = "3.0.3"
ansi_term = "0.7.1"

View file

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

View file

@ -34,6 +34,7 @@ impl<'a> Notes<'a> {
fn command_add(&self, matches: &ArgMatches) -> bool {
use std::process::exit;
use ansi_term::Colour::Yellow;
use self::header::build_header;
use ui::external::editor::let_user_provide_content;
@ -57,13 +58,14 @@ impl<'a> Notes<'a> {
.store()
.load(self, &parser, &fileid)
.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))
})
.unwrap_or(false)
}
fn command_edit(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::{Red, Green};
use ui::external::editor::edit_content;
let parser = Parser::new(JsonHeaderParser::new(None));
@ -116,13 +118,14 @@ impl<'a> Notes<'a> {
let (worked, failed) = result;
info!("Editing succeeded for {} files", worked);
info!("Editing failed for {} files", failed);
info!("{}", Green.paint(format!("Editing succeeded for {} files", worked)));
info!("{}", Red.paint(format!( "Editing failed for {} files", failed)));
return failed == 0;
}
fn command_list(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::{Red, Green};
use ui::file::{FilePrinter, TablePrinter};
use self::header::get_name_from_header;
use self::header::get_tags_from_header;
@ -162,6 +165,7 @@ 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, TablePrinter};
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");
}
info!("Listing links succeeded for {} files", worked);
info!("Listing links failed for {} files", failed);
info!("{}", Green.paint(format!("Listing links succeeded for {} files", worked)));
info!("{}", Red.paint( format!("Listing links failed for {} files", failed)));
return failed == 0;
}
fn command_remove(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::{Red, Green};
let parser = Parser::new(JsonHeaderParser::new(None));
let filter = {
@ -297,8 +303,9 @@ impl<'a> Notes<'a> {
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;
}
@ -343,6 +350,8 @@ impl<'a> Notes<'a> {
impl<'a> Module<'a> for Notes<'a> {
fn exec(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::Red;
match matches.subcommand_name() {
Some("add") => {
self.command_add(matches.subcommand_matches("add").unwrap())
@ -377,7 +386,7 @@ impl<'a> Module<'a> for Notes<'a> {
},
Some(_) | None => {
info!("No command given, doing nothing");
info!("{}", Red.bold().paint("No command given, doing nothing"));
false
},
}

View file

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