From d8aacb4a806c097fb316305dab8c481546d72b8b Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Sat, 13 Feb 2021 22:06:27 +0000 Subject: [PATCH] Add fuzzy history search and distinct arg --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 10 ++++++++++ atuin.plugin.zsh | 10 ++++++++++ src/command/history.rs | 7 +++++-- src/local/database.rs | 34 +++++++++++++++------------------- 6 files changed, 42 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fbf0360..2914a89 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 f3f71cd..b224ce6 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 "] edition = "2018" license = "MIT" diff --git a/README.md b/README.md index 851f71c..4899abd 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 d46346f..e6d4ef2 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 4caf4c1..f8a5b27 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 e9882fd..2b014bc 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) -> Result<()>; fn load(&self, id: &str) -> Result; - 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(())