Skip to content

Commit

Permalink
Make JSON printing more compact.
Browse files Browse the repository at this point in the history
  • Loading branch information
01mf02 committed Nov 2, 2023
1 parent 732459c commit ea4517a
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions jaq/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,21 +434,16 @@ fn print(cli: &Cli, val: Val, writer: &mut impl Write) -> io::Result<()> {
match val {
Val::Str(s) if cli.raw_output => write!(writer, "{s}")?,
_ => {
let val = serde_json::Value::from(val);
let mode = cli.color_mode();

// this looks ugly, but it is hard to abstract over the `Formatter` because
// we cannot create a `Box<dyn Formatter>` because
// Rust says that the `Formatter` trait is not "object safe"
if cli.compact {
ColoredFormatter::new(CompactFormatter).write_colored_json(
&val.into(),
writer,
cli.color_mode(),
)
ColoredFormatter::new(CompactFormatter).write_colored_json(&val, writer, mode)
} else {
ColoredFormatter::new(PrettyFormatter::new()).write_colored_json(
&val.into(),
writer,
cli.color_mode(),
)
ColoredFormatter::new(PrettyFormatter::new()).write_colored_json(&val, writer, mode)
}?
}
};
Expand Down

0 comments on commit ea4517a

Please sign in to comment.