diff --git a/Cargo.lock b/Cargo.lock index 8412f2bf..5e0867ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,7 +2,7 @@ name = "record-query" version = "0.2.2" dependencies = [ - "ansi_term 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ansi_term 0.8.0 (git+https://github.com/dflemstr/rust-ansi-term.git)", "docopt 0.6.82 (registry+https://github.com/rust-lang/crates.io-index)", "duk 0.1.0 (git+https://github.com/dflemstr/duk.git)", "env_logger 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -16,8 +16,8 @@ dependencies = [ "regex 0.1.73 (registry+https://github.com/rust-lang/crates.io-index)", "rmp 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde-hjson 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde-hjson 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde_cbor 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde_protobuf 0.1.1", @@ -37,7 +37,7 @@ dependencies = [ [[package]] name = "ansi_term" version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" +source = "git+https://github.com/dflemstr/rust-ansi-term.git#d89020d53744556f63d44b2de5feca2631e6eef1" [[package]] name = "backtrace" @@ -183,8 +183,8 @@ name = "linked-hash-map" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "serde 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_test 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_test 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -215,7 +215,7 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.1.34" +version = "0.1.35" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -223,7 +223,7 @@ name = "ordered-float" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -287,19 +287,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde" -version = "0.8.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde-hjson" -version = "0.8.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.73 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -308,7 +308,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -318,8 +318,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "dtoa 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -329,15 +329,15 @@ dependencies = [ "linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "protobuf 1.0.23 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "serde_test" -version = "0.8.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "serde 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -345,7 +345,7 @@ name = "serde_yaml" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "serde 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "yaml-rust 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index 5e8f293f..51f23e0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,6 @@ env_logger = "*" regex = "*" [dependencies] -ansi_term = "*" docopt = "*" env_logger = "*" error-chain = "*" @@ -30,6 +29,9 @@ serde_yaml = "*" xdg-basedir = "*" yaml-rust = "*" +[dependencies.ansi_term] +git = "https://github.com/dflemstr/rust-ansi-term.git" + [dependencies.duk] features = ["debug", "logging"] git = "https://github.com/dflemstr/duk.git" diff --git a/src/lib.rs b/src/lib.rs index b663e608..a34a3b7d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ // For pest parser generation #![recursion_limit = "1024"] +extern crate ansi_term; extern crate duk; #[macro_use] extern crate error_chain; diff --git a/src/value/json.rs b/src/value/json.rs index 23b0a5a9..d6c2491f 100644 --- a/src/value/json.rs +++ b/src/value/json.rs @@ -1,5 +1,7 @@ +use std::char; use std::io; +use ansi_term; use serde; use serde_json; @@ -21,6 +23,13 @@ pub trait FormatterClone { #[derive(Clone, Debug)] pub struct ReadableFormatter { current_indent: usize, + + bracket_style: ansi_term::Style, + colon_style: ansi_term::Style, + comma_style: ansi_term::Style, + + field_style: ansi_term::Style, + value_style: ansi_term::Style, } #[inline] @@ -83,6 +92,13 @@ impl ReadableFormatter { fn new() -> ReadableFormatter { ReadableFormatter { current_indent: 0, + + bracket_style: ansi_term::Colour::White.bold(), + colon_style: ansi_term::Colour::White.bold(), + comma_style: ansi_term::Colour::White.bold(), + + field_style: ansi_term::Colour::Blue.bold(), + value_style: ansi_term::Colour::Green.normal(), } } } @@ -92,17 +108,25 @@ impl serde_json::ser::Formatter for ReadableFormatter { where W: io::Write { self.current_indent += 1; - writer.write_all(&[ch]).map_err(From::from) + // TODO: optimize this maybe? + let char_str = format!("{}", char::from_u32(ch as u32).unwrap()); + try!(write!(writer, "{}", self.bracket_style.paint(char_str))); + + if ch as char == '{' { + try!(write!(writer, "{}", self.field_style.prefix())); + } + Ok(()) } fn comma(&mut self, writer: &mut W, first: bool) -> serde_json::error::Result<()> where W: io::Write { - if first { - try!(writer.write_all(b"\n")); - } else { - try!(writer.write_all(b",\n")); + if !first { + try!(write!(writer, "{}", self.comma_style.paint(","))); } + try!(writer.write_all(b"\n")); + + try!(write!(writer, "{}", self.field_style.prefix())); indent(writer, self.current_indent) } @@ -110,7 +134,12 @@ impl serde_json::ser::Formatter for ReadableFormatter { fn colon(&mut self, writer: &mut W) -> serde_json::error::Result<()> where W: io::Write { - writer.write_all(b": ").map_err(From::from) + try!(write!(writer, "{}", self.colon_style.paint(":"))); + try!(write!(writer, " ")); + + try!(write!(writer, "{}", self.value_style.prefix())); + + Ok(()) } fn close(&mut self, writer: &mut W, ch: u8) -> serde_json::error::Result<()> @@ -120,7 +149,11 @@ impl serde_json::ser::Formatter for ReadableFormatter { try!(writer.write(b"\n")); try!(indent(writer, self.current_indent)); - writer.write_all(&[ch]).map_err(From::from) + // TODO: optimize this maybe? + let char_str = format!("{}", char::from_u32(ch as u32).unwrap()); + try!(write!(writer, "{}", self.bracket_style.paint(char_str))); + + Ok(()) } }