Skip to content

Commit

Permalink
Merge pull request #17 from dflemstr/color
Browse files Browse the repository at this point in the history
Implement basic color support for JSON
  • Loading branch information
dflemstr authored Aug 19, 2016
2 parents c1eddf7 + cd1d6a4 commit 616fbec
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
38 changes: 19 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ env_logger = "*"
regex = "*"

[dependencies]
ansi_term = "*"
docopt = "*"
env_logger = "*"
error-chain = "*"
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// For pest parser generation
#![recursion_limit = "1024"]

extern crate ansi_term;
extern crate duk;
#[macro_use]
extern crate error_chain;
Expand Down
47 changes: 40 additions & 7 deletions src/value/json.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::char;
use std::io;

use ansi_term;
use serde;
use serde_json;

Expand All @@ -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]
Expand Down Expand Up @@ -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(),
}
}
}
Expand All @@ -92,25 +108,38 @@ 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<W>(&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)
}

fn colon<W>(&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<W>(&mut self, writer: &mut W, ch: u8) -> serde_json::error::Result<()>
Expand All @@ -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(())
}
}

Expand Down

0 comments on commit 616fbec

Please sign in to comment.