Add --reverse
to atuin search
(#862)
Add `-r/--reverse` flag to `atuin search` to allow searching by oldest results first. Example to find the oldest `cargo` command: ``` atuin search --limit 1 --reverse cargo ```
This commit is contained in:
parent
e0c4ec5498
commit
e149a0a6e9
3 changed files with 18 additions and 4 deletions
|
@ -35,6 +35,7 @@ pub struct OptFilters {
|
||||||
pub after: Option<String>,
|
pub after: Option<String>,
|
||||||
pub limit: Option<i64>,
|
pub limit: Option<i64>,
|
||||||
pub offset: Option<i64>,
|
pub offset: Option<i64>,
|
||||||
|
pub reverse: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn current_context() -> Context {
|
pub fn current_context() -> Context {
|
||||||
|
@ -354,9 +355,7 @@ impl Database for Sqlite {
|
||||||
) -> Result<Vec<History>> {
|
) -> Result<Vec<History>> {
|
||||||
let mut sql = SqlBuilder::select_from("history");
|
let mut sql = SqlBuilder::select_from("history");
|
||||||
|
|
||||||
sql.group_by("command")
|
sql.group_by("command").having("max(timestamp)");
|
||||||
.having("max(timestamp)")
|
|
||||||
.order_desc("timestamp");
|
|
||||||
|
|
||||||
if let Some(limit) = filter_options.limit {
|
if let Some(limit) = filter_options.limit {
|
||||||
sql.limit(limit);
|
sql.limit(limit);
|
||||||
|
@ -366,6 +365,12 @@ impl Database for Sqlite {
|
||||||
sql.offset(offset);
|
sql.offset(offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if filter_options.reverse {
|
||||||
|
sql.order_asc("timestamp");
|
||||||
|
} else {
|
||||||
|
sql.order_desc("timestamp");
|
||||||
|
}
|
||||||
|
|
||||||
match filter {
|
match filter {
|
||||||
FilterMode::Global => &mut sql,
|
FilterMode::Global => &mut sql,
|
||||||
FilterMode::Host => sql.and_where_eq("hostname", quote(&context.hostname)),
|
FilterMode::Host => sql.and_where_eq("hostname", quote(&context.hostname)),
|
||||||
|
|
|
@ -22,6 +22,7 @@ appended with a wildcard).
|
||||||
| `--limit` | Limit the number of results (default: none) |
|
| `--limit` | Limit the number of results (default: none) |
|
||||||
| `--offset` | Offset from the start of the results (default: none) |
|
| `--offset` | Offset from the start of the results (default: none) |
|
||||||
| `--delete` | Delete history matching this query |
|
| `--delete` | Delete history matching this query |
|
||||||
|
| `--reverse` | Reverse order of search results, oldest first |
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
@ -48,5 +49,8 @@ atuin search --delete --exit 0 --after "yesterday 3pm" cargo
|
||||||
atuin search --limit 1 cargo
|
atuin search --limit 1 cargo
|
||||||
|
|
||||||
# Search for a single result for a command beginning with cargo, skipping (offsetting) one result
|
# Search for a single result for a command beginning with cargo, skipping (offsetting) one result
|
||||||
atuin search --offset 1 --limit cargo
|
atuin search --offset 1 --limit 1 cargo
|
||||||
|
|
||||||
|
# Find the oldest cargo command
|
||||||
|
atuin search --limit 1 --reverse cargo
|
||||||
```
|
```
|
||||||
|
|
|
@ -83,6 +83,10 @@ pub struct Cmd {
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
delete: bool,
|
delete: bool,
|
||||||
|
|
||||||
|
/// Reverse the order of results, oldest first
|
||||||
|
#[arg(long, short)]
|
||||||
|
reverse: bool,
|
||||||
|
|
||||||
/// Available variables: {command}, {directory}, {duration}, {user}, {host}, {time}, {exit} and
|
/// Available variables: {command}, {directory}, {duration}, {user}, {host}, {time}, {exit} and
|
||||||
/// {relativetime}.
|
/// {relativetime}.
|
||||||
/// Example: --format "{time} - [{duration}] - {directory}$\t{command}"
|
/// Example: --format "{time} - [{duration}] - {directory}$\t{command}"
|
||||||
|
@ -116,6 +120,7 @@ impl Cmd {
|
||||||
after: self.after,
|
after: self.after,
|
||||||
limit: self.limit,
|
limit: self.limit,
|
||||||
offset: self.offset,
|
offset: self.offset,
|
||||||
|
reverse: self.reverse,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut entries =
|
let mut entries =
|
||||||
|
|
Loading…
Reference in a new issue