Skip to content

Commit

Permalink
Flush writer after each value when using --join-output.
Browse files Browse the repository at this point in the history
  • Loading branch information
01mf02 committed Nov 21, 2024
1 parent e21d322 commit a51d8da
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions jaq/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{Parser, ValueEnum};
use core::fmt::{self, Display, Formatter};
use jaq_core::{compile, load, Ctx, Native, RcIter};
use jaq_core::{compile, load, Ctx, Native, RcIter, ValT};
use jaq_json::Val;
use std::ffi::{OsStr, OsString};
use std::io::{self, BufRead, Write};
Expand Down Expand Up @@ -528,7 +528,6 @@ fn run(
let input = item.map_err(Error::Parse)?;
//println!("Got {:?}", input);
for output in filter.run((ctx.clone(), input)) {
use jaq_core::ValT;
let output = output.map_err(Error::Jaq)?;
last = Some(output.as_bool());
f(output)?;
Expand Down Expand Up @@ -612,30 +611,31 @@ fn fmt_val(f: &mut Formatter, opts: &PpOpts, level: usize, v: &Val) -> fmt::Resu
}
}

fn print(writer: &mut impl Write, cli: &Cli, val: &Val) -> io::Result<()> {
let f = |f: &mut Formatter| fmt_val_root(f, cli, val);
write!(writer, "{}", FormatterFn(f))
}
fn print(w: &mut impl Write, cli: &Cli, val: &Val) -> io::Result<()> {
let f = |f: &mut Formatter| {
let opts = PpOpts {
compact: cli.compact_output,
indent: if cli.tab {
String::from("\t")
} else {
" ".repeat(cli.indent)
},
};
fmt_val(f, &opts, 0, val)
};

fn fmt_val_root(f: &mut Formatter, cli: &Cli, val: &Val) -> fmt::Result {
match val {
Val::Str(s) if cli.raw_output || cli.join_output => write!(f, "{s}")?,
_ => {
let opts = PpOpts {
compact: cli.compact_output,
indent: if cli.tab {
String::from("\t")
} else {
" ".repeat(cli.indent)
},
};
fmt_val(f, &opts, 0, val)?;
}
Val::Str(s) if cli.raw_output || cli.join_output => write!(w, "{s}")?,
_ => write!(w, "{}", FormatterFn(f))?,
};
if !cli.join_output {
writeln!(f)?;

if cli.join_output {
// when running `jaq -jn '"prompt> " | (., input)'`,
// this flush is necessary to make "prompt> " appear first
w.flush()
} else {
writeln!(w)
}
Ok(())
}

fn with_stdout<T>(f: impl FnOnce(&mut io::StdoutLock) -> Result<T, Error>) -> Result<T, Error> {
Expand Down

0 comments on commit a51d8da

Please sign in to comment.