Add fuzzy history search and distinct arg

This commit is contained in:
Ellie Huxtable 2021-02-13 22:06:27 +00:00
parent 3e7af55a9c
commit d8aacb4a80
6 changed files with 42 additions and 23 deletions

2
Cargo.lock generated
View file

@ -49,7 +49,7 @@ dependencies = [
[[package]]
name = "atuin"
version = "0.2.2"
version = "0.2.3"
dependencies = [
"chrono",
"directories",

View file

@ -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"

View file

@ -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
```

View file

@ -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

View file

@ -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),
}
}
}

View file

@ -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(())