summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEllie Huxtable <e@elm.sh>2021-02-13 22:06:27 +0000
committerEllie Huxtable <e@elm.sh>2021-02-13 22:06:27 +0000
commitd8aacb4a806c097fb316305dab8c481546d72b8b (patch)
treee30757902c93c3ccd8f99d14fb6360545566ee6b
parent3e7af55a9c43736a4155a794276597c73580cadd (diff)
Add fuzzy history search and distinct arg
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--README.md10
-rw-r--r--atuin.plugin.zsh10
-rw-r--r--src/command/history.rs7
-rw-r--r--src/local/database.rs34
6 files changed, 42 insertions, 23 deletions
diff --git a/Cargo.lock b/Cargo.lock
index fbf0360b..2914a895 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -49,7 +49,7 @@ dependencies = [
[[package]]
name = "atuin"
-version = "0.2.2"
+version = "0.2.3"
dependencies = [
"chrono",
"directories",
diff --git a/Cargo.toml b/Cargo.toml
index f3f71cdf..b224ce65 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "atuin"
-version = "0.2.2"
+version = "0.2.3"
authors = ["Ellie Huxtable <e@elm.sh>"]
edition = "2018"
license = "MIT"
diff --git a/README.md b/README.md
index 851f71ce..4899abd3 100644
--- a/README.md
+++ b/README.md
@@ -51,10 +51,20 @@ and then add `atuin` to your `plugins` list in `~/.zshrc`
## Usage
+By default A'tuin will rebind ctrl-r to use fzf to fuzzy search your history. You
+can specify a different fuzzy tool by changing the value of `ATUIN_FUZZY`:
+
+```
+export ATUIN_FUZZY=fzy
+```
+
### Import history
```
atuin import auto # detect shell, then import
+
+or
+
atuin import zsh # specify shell
```
diff --git a/atuin.plugin.zsh b/atuin.plugin.zsh
index d46346f2..e6d4ef25 100644
--- a/atuin.plugin.zsh
+++ b/atuin.plugin.zsh
@@ -1,5 +1,6 @@
# Source this in your ~/.zshrc
export ATUIN_SESSION=$(atuin uuid)
+export ATUIN_FUZZY=fzf
_atuin_preexec(){
id=$(atuin history start $1)
@@ -14,5 +15,14 @@ _atuin_precmd(){
atuin history end $ATUIN_HISTORY_ID --exit $EXIT
}
+_atuin_search(){
+ $(atuin history list --distinct | $ATUIN_FUZZY)
+}
+
add-zsh-hook preexec _atuin_preexec
add-zsh-hook precmd _atuin_precmd
+
+zle -N _atuin_search_widget _atuin_search
+
+bindkey -r '^r'
+bindkey '^r' _atuin_search_widget
diff --git a/src/command/history.rs b/src/command/history.rs
index 4caf4c17..f8a5b27e 100644
--- a/src/command/history.rs
+++ b/src/command/history.rs
@@ -28,7 +28,10 @@ pub enum HistoryCmd {
about="list all items in history",
aliases=&["l", "li", "lis"],
)]
- List,
+ List {
+ #[structopt(long)]
+ distinct: bool,
+ },
}
impl HistoryCmd {
@@ -65,7 +68,7 @@ impl HistoryCmd {
Ok(())
}
- HistoryCmd::List => db.list(),
+ HistoryCmd::List { distinct } => db.list(*distinct),
}
}
}
diff --git a/src/local/database.rs b/src/local/database.rs
index e9882fd7..2b014bc0 100644
--- a/src/local/database.rs
+++ b/src/local/database.rs
@@ -11,7 +11,7 @@ pub trait Database {
fn save(&mut self, h: History) -> Result<()>;
fn save_bulk(&mut self, h: &Vec<History>) -> Result<()>;
fn load(&self, id: &str) -> Result<History>;
- fn list(&self) -> Result<()>;
+ fn list(&self, distinct: bool) -> Result<()>;
fn update(&self, h: History) -> Result<()>;
}
@@ -149,33 +149,29 @@ impl Database for SqliteDatabase {
Ok(())
}
- fn list(&self) -> Result<()> {
+ fn list(&self, distinct: bool) -> Result<()> {
debug!("listing history");
- let mut stmt = self.conn.prepare(
- "SELECT id, timestamp, duration, exit, command, cwd, session, hostname FROM history",
- )?;
+ let mut stmt = match distinct {
+ false => self
+ .conn
+ .prepare("SELECT command FROM history order by timestamp asc")?,
+
+ true => self
+ .conn
+ .prepare("SELECT distinct command FROM history order by timestamp asc")?,
+ };
let history_iter = stmt.query_map(params![], |row| {
- Ok(History {
- id: row.get(0)?,
- timestamp: row.get(1)?,
- duration: row.get(2)?,
- exit: row.get(3)?,
- command: row.get(4)?,
- cwd: row.get(5)?,
- session: row.get(6)?,
- hostname: row.get(7)?,
- })
+ let command: String = row.get(0)?;
+
+ Ok(command)
})?;
for h in history_iter {
let h = h.unwrap();
- println!(
- "{} | {} | {} | {} | {} | {} | {}",
- h.timestamp, h.hostname, h.session, h.cwd, h.duration, h.exit, h.command
- );
+ println!("{}", h);
}
Ok(())