Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print colorized json #163

Merged
merged 1 commit into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 124 additions & 14 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ time-fmt = "0.3.4"
time-tz = { version = "0.5.2", features = ["system", "posix-tz"] }
onig = { version = "6.3.1", default-features = false }

colored_json = "3.1.0"
clap = { version = "4.0.0", features = ["derive"], optional = true }
clap-verbosity-flag = { version = "2.0.0", optional = true }
anyhow = { version = "1.0.56", optional = true }
simplelog = { version = "0.12.0", optional = true }
serde_yaml = { version = "0.8.23", optional = true }
is-terminal = "0.4.7"

[dev-dependencies]
criterion = "0.3.5"
Expand Down
43 changes: 42 additions & 1 deletion src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use anyhow::{anyhow, Context, Result};
use clap::Parser;
use clap_verbosity_flag::Verbosity;
use cli::input::Input;
use is_terminal::IsTerminal;
use xq::{module_loader::PreludeLoader, run_query, InputError, Value};

use crate::cli::input::Tied;
Expand Down Expand Up @@ -111,6 +112,14 @@ struct OutputFormatArg {
/// Compact output
#[clap(short, long, conflicts_with = "output-format")]
compact_output: bool,

/// Colorize output where possible (currently only JSON is supported)
#[clap(short = 'C', long, group = "output-color")]
color_output: bool,

/// Do not colorize output
#[clap(short = 'M', long, group = "output-color")]
monochrome_output: bool,
}

impl OutputFormatArg {
Expand Down Expand Up @@ -161,6 +170,14 @@ fn run_with_input(cli: Cli, input: impl Input) -> Result<()> {
let output_format = cli.output_format.get();
match output_format {
SerializationFormat::Json => {
let is_stdout_terminal = stdout().is_terminal();
let should_colorize_output = cli.output_format.color_output
|| (is_stdout_terminal && !cli.output_format.monochrome_output);
let color_styler = should_colorize_output.then(|| colored_json::Styler {
nil_value: colored_json::Style::new(colored_json::Color::Default).dimmed(),
..Default::default()
});

for value in result_iterator {
match value {
Ok(Value::String(s)) if cli.output_format.raw_output => {
Expand All @@ -169,7 +186,31 @@ fn run_with_input(cli: Cli, input: impl Input) -> Result<()> {
}
Ok(value) => {
if cli.output_format.compact_output {
serde_json::ser::to_writer::<_, Value>(stdout().lock(), &value)?;
if let Some(styler) = color_styler {
let formatter = colored_json::ColoredFormatter::with_styler(
colored_json::CompactFormatter,
styler,
);
formatter.write_colored_json(
&serde_json::to_value(&value)?,
&mut stdout().lock(),
colored_json::ColorMode::On,
)?;
println!();
} else {
serde_json::ser::to_writer::<_, Value>(stdout().lock(), &value)?;
println!();
}
} else if let Some(styler) = color_styler {
let formatter = colored_json::ColoredFormatter::with_styler(
colored_json::PrettyFormatter::default(),
styler,
);
formatter.write_colored_json(
&serde_json::to_value(&value)?,
&mut stdout().lock(),
colored_json::ColorMode::On,
)?;
println!();
} else {
serde_json::ser::to_writer_pretty::<_, Value>(stdout().lock(), &value)?;
Expand Down