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

Add --print0 to history list #1274

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 48 additions & 11 deletions atuin/src/command/client/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ pub enum Cmd {
#[arg(long)]
cmd_only: bool,

/// Terminate the output with a null, for better multiline support
#[arg(long)]
print0: bool,

#[arg(long, short, default_value = "true")]
// accept no value
#[arg(num_args(0..=1), default_missing_value("true"))]
Expand Down Expand Up @@ -100,7 +104,13 @@ impl ListMode {
}

#[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 mut w = w.lock();

Expand All @@ -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>>
};

let entry_terminator = if print0 { "\0" } else { "\n" };
conradludgate marked this conversation as resolved.
Show resolved Hide resolved
let flush_each_line = print0;

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(()) => {}
// ignore broken pipe (issue #626)
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => {
Expand All @@ -138,15 +156,28 @@ pub fn print_list(h: &[History], list_mode: ListMode, format: Option<&str>, reve
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() {
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);
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);
}
}
}
}
Expand Down Expand Up @@ -288,6 +319,7 @@ impl Cmd {
cwd: bool,
mode: ListMode,
format: Option<String>,
print0: bool,
reverse: bool,
) -> Result<()> {
let session = if session {
Expand Down Expand Up @@ -319,7 +351,7 @@ impl Cmd {
}
};

print_list(&history, mode, format.as_deref(), reverse);
print_list(&history, mode, format.as_deref(), print0, reverse);

Ok(())
}
Expand All @@ -335,12 +367,16 @@ impl Cmd {
cwd,
human,
cmd_only,
print0,
reverse,
format,
} => {
let mode = ListMode::from_flags(human, cmd_only);
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 {
Expand All @@ -354,6 +390,7 @@ impl Cmd {
last,
ListMode::from_flags(human, cmd_only),
format.as_deref(),
false,
true,
);

Expand Down
8 changes: 7 additions & 1 deletion atuin/src/command/client/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ impl Cmd {
.await?;
}
} 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(())
Expand Down