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 --reverse to history list #1252

Merged
merged 8 commits into from
Sep 29, 2023
26 changes: 22 additions & 4 deletions atuin/src/command/client/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ pub enum Cmd {
#[arg(long)]
cmd_only: bool,

#[arg(long, short, default_value = "true")]
// accept no value
#[arg(num_args(0..=1), default_missing_value("true"))]
// accept a value
#[arg(action = clap::ArgAction::Set)]
reverse: bool,

/// Available variables: {command}, {directory}, {duration}, {user}, {host}, {exit} and {time}.
/// Example: --format "{time} - [{duration}] - {directory}$\t{command}"
#[arg(long, short)]
Expand Down Expand Up @@ -93,7 +100,7 @@ impl ListMode {
}

#[allow(clippy::cast_sign_loss)]
pub fn print_list(h: &[History], list_mode: ListMode, format: Option<&str>) {
pub fn print_list(h: &[History], list_mode: ListMode, format: Option<&str>, reverse: bool) {
let w = std::io::stdout();
let mut w = w.lock();

Expand All @@ -113,7 +120,13 @@ pub fn print_list(h: &[History], list_mode: ListMode, format: Option<&str>) {
ListMode::CmdOnly => std::iter::once(ParseSegment::Key("command")).collect(),
};

for h in h.iter().rev() {
let iterator = if reverse {
Box::new(h.iter().rev()) as Box<dyn Iterator<Item = &History>>
} else {
Box::new(h.iter()) as Box<dyn Iterator<Item = &History>>
};

for h in iterator {
match writeln!(w, "{}", parsed_fmt.with_args(&FmtHistory(h))) {
Ok(()) => {}
// ignore broken pipe (issue #626)
Expand Down Expand Up @@ -263,6 +276,7 @@ impl Cmd {
Ok(())
}

#[allow(clippy::too_many_arguments)]
async fn handle_list(
db: &mut impl Database,
settings: &Settings,
Expand All @@ -271,6 +285,7 @@ impl Cmd {
cwd: bool,
mode: ListMode,
format: Option<String>,
reverse: bool,
conradludgate marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<()> {
let session = if session {
Some(env::var("ATUIN_SESSION")?)
Expand Down Expand Up @@ -301,7 +316,7 @@ impl Cmd {
}
};

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

Ok(())
}
Expand All @@ -317,10 +332,12 @@ impl Cmd {
cwd,
human,
cmd_only,
reverse,
format,
} => {
let mode = ListMode::from_flags(human, cmd_only);
Self::handle_list(db, settings, context, session, cwd, mode, format).await
let reverse = reverse;
Self::handle_list(db, settings, context, session, cwd, mode, format, reverse).await
}

Self::Last {
Expand All @@ -333,6 +350,7 @@ impl Cmd {
&[last],
ListMode::from_flags(human, cmd_only),
format.as_deref(),
true,
);

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion atuin/src/command/client/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Cmd {
.await?;
}
} else {
super::history::print_list(&entries, list_mode, self.format.as_deref());
super::history::print_list(&entries, list_mode, self.format.as_deref(), true);
}
};
Ok(())
Expand Down
1 change: 1 addition & 0 deletions docs/docs/commands/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ title: Listing History
| `--session`/`-s` | Enable listing history for the current session only (default: false) |
| `--human` | Use human-readable formatting for the timestamp and duration (default: false) |
| `--cmd-only` | Show only the text of the command (default: false) |
| `--reverse` | Reverse the order of the output (default: false) |
| `--format` | Specify the formatting of a command (see below) |

## Format
Expand Down