Skip to content

Commit

Permalink
Add --offset flag to atuin search
Browse files Browse the repository at this point in the history
This flag allows the user to continue searching at an offset. This is
useful for building tools that use atuin to search for previous
commands and return only one result.

```

atuin search --limit 1
atuin search --limit 1 --offset 1
atuin search --limit 1 --offset 2
```
  • Loading branch information
takac committed Mar 28, 2023
1 parent c64674d commit f60d692
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions atuin-client/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub trait Database: Send + Sync + 'static {
context: &Context,
query: &str,
limit: Option<i64>,
offset: Option<i64>,
before: Option<i64>,
after: Option<i64>,
) -> Result<Vec<History>>;
Expand Down Expand Up @@ -341,6 +342,7 @@ impl Database for Sqlite {
context: &Context,
query: &str,
limit: Option<i64>,
offset: Option<i64>,
before: Option<i64>,
after: Option<i64>,
) -> Result<Vec<History>> {
Expand All @@ -362,6 +364,10 @@ impl Database for Sqlite {
sql.and_where_lt("timestamp", before);
}

if let Some(offset) = offset {
sql.offset(offset);
}

match filter {
FilterMode::Global => &mut sql,
FilterMode::Host => sql.and_where_eq("hostname", quote(&context.hostname)),
Expand Down
8 changes: 8 additions & 0 deletions src/command/client/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ pub struct Cmd {
#[arg(long)]
limit: Option<i64>,

/// Offset from the start of the results
#[arg(long)]
offset: Option<i64>,

/// Open interactive search UI
#[arg(long, short)]
interactive: bool,
Expand Down Expand Up @@ -112,6 +116,7 @@ impl Cmd {
self.before.clone(),
self.after.clone(),
self.limit,
self.offset,
&self.query,
&mut db,
)
Expand Down Expand Up @@ -141,6 +146,7 @@ impl Cmd {
self.before.clone(),
self.after.clone(),
self.limit,
self.offset,
&self.query,
&mut db,
)
Expand All @@ -166,6 +172,7 @@ async fn run_non_interactive(
before: Option<String>,
after: Option<String>,
limit: Option<i64>,
offset: Option<i64>,
query: &[String],
db: &mut impl Database,
) -> Result<Vec<History>> {
Expand Down Expand Up @@ -194,6 +201,7 @@ async fn run_non_interactive(
&context,
query.join(" ").as_str(),
limit,
offset,
before,
after,
)
Expand Down
1 change: 1 addition & 0 deletions src/command/client/search/engines/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl SearchEngine for Search {
Some(200),
None,
None,
None,
)
.await?
.into_iter()
Expand Down

0 comments on commit f60d692

Please sign in to comment.