add --reverse to history list (#1252)
* wip: add --reverse to history list * fix: remove print_list being called twice when reverse=true * chore: update documentation * feat: Avoid unneeded reverse by iterate forward or backward instead * feat: Make sure to only iterate once and avoid unneeded reverse call * feat: It used to be reverse by default. So make it default true here to be backwards compatible * fix clap --------- Co-authored-by: Dieter Eickstädt <eickstaedt@deicon.de>
This commit is contained in:
parent
865f0e65f1
commit
504400673f
3 changed files with 24 additions and 5 deletions
|
@ -51,6 +51,13 @@ pub enum Cmd {
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
cmd_only: bool,
|
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}.
|
/// Available variables: {command}, {directory}, {duration}, {user}, {host}, {exit} and {time}.
|
||||||
/// Example: --format "{time} - [{duration}] - {directory}$\t{command}"
|
/// Example: --format "{time} - [{duration}] - {directory}$\t{command}"
|
||||||
#[arg(long, short)]
|
#[arg(long, short)]
|
||||||
|
@ -93,7 +100,7 @@ impl ListMode {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::cast_sign_loss)]
|
#[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 w = std::io::stdout();
|
||||||
let mut w = w.lock();
|
let mut w = w.lock();
|
||||||
|
|
||||||
|
@ -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(),
|
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))) {
|
match writeln!(w, "{}", parsed_fmt.with_args(&FmtHistory(h))) {
|
||||||
Ok(()) => {}
|
Ok(()) => {}
|
||||||
// ignore broken pipe (issue #626)
|
// ignore broken pipe (issue #626)
|
||||||
|
@ -266,6 +279,7 @@ impl Cmd {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
async fn handle_list(
|
async fn handle_list(
|
||||||
db: &mut impl Database,
|
db: &mut impl Database,
|
||||||
settings: &Settings,
|
settings: &Settings,
|
||||||
|
@ -274,6 +288,7 @@ impl Cmd {
|
||||||
cwd: bool,
|
cwd: bool,
|
||||||
mode: ListMode,
|
mode: ListMode,
|
||||||
format: Option<String>,
|
format: Option<String>,
|
||||||
|
reverse: bool,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let session = if session {
|
let session = if session {
|
||||||
Some(env::var("ATUIN_SESSION")?)
|
Some(env::var("ATUIN_SESSION")?)
|
||||||
|
@ -304,7 +319,7 @@ impl Cmd {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
print_list(&history, mode, format.as_deref());
|
print_list(&history, mode, format.as_deref(), reverse);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -320,10 +335,12 @@ impl Cmd {
|
||||||
cwd,
|
cwd,
|
||||||
human,
|
human,
|
||||||
cmd_only,
|
cmd_only,
|
||||||
|
reverse,
|
||||||
format,
|
format,
|
||||||
} => {
|
} => {
|
||||||
let mode = ListMode::from_flags(human, cmd_only);
|
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 {
|
Self::Last {
|
||||||
|
@ -337,6 +354,7 @@ impl Cmd {
|
||||||
last,
|
last,
|
||||||
ListMode::from_flags(human, cmd_only),
|
ListMode::from_flags(human, cmd_only),
|
||||||
format.as_deref(),
|
format.as_deref(),
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -169,7 +169,7 @@ impl Cmd {
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
} else {
|
} 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(())
|
Ok(())
|
||||||
|
|
|
@ -11,6 +11,7 @@ title: Listing History
|
||||||
| `--session`/`-s` | Enable listing history for the current session only (default: false) |
|
| `--session`/`-s` | Enable listing history for the current session only (default: false) |
|
||||||
| `--human` | Use human-readable formatting for the timestamp and duration (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) |
|
| `--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` | Specify the formatting of a command (see below) |
|
||||||
|
|
||||||
## Format
|
## Format
|
||||||
|
|
Loading…
Reference in a new issue