Add --print0 to history list (#1274)

* Add --print0 to `history list`

* Allow too many bools in the handle_list

Gotta pacify clippy; I'm not sure how we'd do this without it!
This commit is contained in:
Chris Rose 2023-10-02 22:31:40 -07:00 committed by GitHub
parent 0abbcd6941
commit 6f803b548b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 12 deletions

View file

@ -51,6 +51,10 @@ pub enum Cmd {
#[arg(long)] #[arg(long)]
cmd_only: bool, cmd_only: bool,
/// Terminate the output with a null, for better multiline support
#[arg(long)]
print0: bool,
#[arg(long, short, default_value = "true")] #[arg(long, short, default_value = "true")]
// accept no value // accept no value
#[arg(num_args(0..=1), default_missing_value("true"))] #[arg(num_args(0..=1), default_missing_value("true"))]
@ -100,7 +104,13 @@ impl ListMode {
} }
#[allow(clippy::cast_sign_loss)] #[allow(clippy::cast_sign_loss)]
pub fn print_list(h: &[History], list_mode: ListMode, format: Option<&str>, reverse: bool) { pub fn print_list(
h: &[History],
list_mode: ListMode,
format: Option<&str>,
print0: bool,
reverse: bool,
) {
let w = std::io::stdout(); let w = std::io::stdout();
let mut w = w.lock(); let mut w = w.lock();
@ -126,8 +136,16 @@ pub fn print_list(h: &[History], list_mode: ListMode, format: Option<&str>, reve
Box::new(h.iter()) as Box<dyn Iterator<Item = &History>> Box::new(h.iter()) as Box<dyn Iterator<Item = &History>>
}; };
let entry_terminator = if print0 { "\0" } else { "\n" };
let flush_each_line = print0;
for h in iterator { for h in iterator {
match writeln!(w, "{}", parsed_fmt.with_args(&FmtHistory(h))) { match write!(
w,
"{}{}",
parsed_fmt.with_args(&FmtHistory(h)),
entry_terminator
) {
Ok(()) => {} Ok(()) => {}
// ignore broken pipe (issue #626) // ignore broken pipe (issue #626)
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => { Err(e) if e.kind() == io::ErrorKind::BrokenPipe => {
@ -138,15 +156,28 @@ pub fn print_list(h: &[History], list_mode: ListMode, format: Option<&str>, reve
std::process::exit(1); std::process::exit(1);
} }
} }
if flush_each_line {
match w.flush() {
Ok(()) => {}
// ignore broken pipe (issue #626)
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => {}
Err(err) => {
eprintln!("ERROR: History output failed with the following error: {err}");
std::process::exit(1);
}
}
}
} }
match w.flush() { if !flush_each_line {
Ok(()) => {} match w.flush() {
// ignore broken pipe (issue #626) Ok(()) => {}
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => {} // ignore broken pipe (issue #626)
Err(err) => { Err(e) if e.kind() == io::ErrorKind::BrokenPipe => {}
eprintln!("ERROR: History output failed with the following error: {err}"); Err(err) => {
std::process::exit(1); eprintln!("ERROR: History output failed with the following error: {err}");
std::process::exit(1);
}
} }
} }
} }
@ -280,6 +311,7 @@ impl Cmd {
} }
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
#[allow(clippy::fn_params_excessive_bools)]
async fn handle_list( async fn handle_list(
db: &mut impl Database, db: &mut impl Database,
settings: &Settings, settings: &Settings,
@ -288,6 +320,7 @@ impl Cmd {
cwd: bool, cwd: bool,
mode: ListMode, mode: ListMode,
format: Option<String>, format: Option<String>,
print0: bool,
reverse: bool, reverse: bool,
) -> Result<()> { ) -> Result<()> {
let session = if session { let session = if session {
@ -319,7 +352,7 @@ impl Cmd {
} }
}; };
print_list(&history, mode, format.as_deref(), reverse); print_list(&history, mode, format.as_deref(), print0, reverse);
Ok(()) Ok(())
} }
@ -335,12 +368,16 @@ impl Cmd {
cwd, cwd,
human, human,
cmd_only, cmd_only,
print0,
reverse, reverse,
format, format,
} => { } => {
let mode = ListMode::from_flags(human, cmd_only); let mode = ListMode::from_flags(human, cmd_only);
let reverse = reverse; let reverse = reverse;
Self::handle_list(db, settings, context, session, cwd, mode, format, reverse).await Self::handle_list(
db, settings, context, session, cwd, mode, format, print0, reverse,
)
.await
} }
Self::Last { Self::Last {
@ -354,6 +391,7 @@ impl Cmd {
last, last,
ListMode::from_flags(human, cmd_only), ListMode::from_flags(human, cmd_only),
format.as_deref(), format.as_deref(),
false,
true, true,
); );

View file

@ -169,7 +169,13 @@ impl Cmd {
.await?; .await?;
} }
} else { } else {
super::history::print_list(&entries, list_mode, self.format.as_deref(), true); super::history::print_list(
&entries,
list_mode,
self.format.as_deref(),
false,
true,
);
} }
}; };
Ok(()) Ok(())