Skip to content
Merged
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
13 changes: 9 additions & 4 deletions jaq/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use jaq_json::Val;
use std::io::{self, BufRead, Write};
use std::path::{Path, PathBuf};
use std::process::{ExitCode, Termination};
use is_terminal::IsTerminal;

type Filter = jaq_core::Filter<Native<Val>>;

Expand All @@ -16,7 +17,6 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

fn main() -> ExitCode {
use env_logger::Env;
use is_terminal::IsTerminal;
env_logger::Builder::from_env(Env::default().filter_or("LOG", "debug"))
.format(|buf, record| match record.level() {
// format error messages (yielded by `stderr`) without newline
Expand Down Expand Up @@ -489,7 +489,7 @@ fn fmt_val(f: &mut Formatter, opts: &PpOpts, level: usize, v: &Val) -> fmt::Resu
}
}

fn print(w: &mut impl Write, cli: &Cli, val: &Val) -> io::Result<()> {
fn print(w: &mut (impl Write + ?Sized), cli: &Cli, val: &Val) -> io::Result<()> {
let f = |f: &mut Formatter| {
let opts = PpOpts {
compact: cli.compact_output,
Expand Down Expand Up @@ -518,8 +518,13 @@ fn print(w: &mut impl Write, cli: &Cli, val: &Val) -> io::Result<()> {
}
}

fn with_stdout<T>(f: impl FnOnce(&mut io::StdoutLock) -> T) -> T {
f(&mut io::stdout().lock())
fn with_stdout<T>(f: impl FnOnce(&mut dyn Write) -> T) -> T {
let stdout = io::stdout();
if stdout.is_terminal() {
f(&mut stdout.lock())
} else {
f(&mut io::BufWriter::new(stdout.lock()))
}
}

type StringColors = Vec<(String, Option<Color>)>;
Expand Down