From 41f072a8b443b5a404916598f0c9b7a52f5c05fb Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Mon, 15 Feb 2021 23:33:30 +0000 Subject: [PATCH] Add init command (#12) * Add init command This makes setting up the shell part of A'tuin much easier. Eval the output of "atuin init". * Update readme, add up binding --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 70 ++++++++++++++++++++++--- src/command/history.rs | 6 +-- src/command/init.rs | 19 +++++++ src/command/mod.rs | 5 ++ src/main.rs | 2 +- atuin.plugin.zsh => src/shell/atuin.zsh | 26 +++++++-- 8 files changed, 115 insertions(+), 17 deletions(-) create mode 100644 src/command/init.rs rename atuin.plugin.zsh => src/shell/atuin.zsh (51%) diff --git a/Cargo.lock b/Cargo.lock index f4859c9..fb5fbd1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -106,7 +106,7 @@ dependencies = [ [[package]] name = "atuin" -version = "0.3.1" +version = "0.3.2" dependencies = [ "chrono", "chrono-english", diff --git a/Cargo.toml b/Cargo.toml index d29c3ed..0defe74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "atuin" -version = "0.3.1" +version = "0.3.2" authors = ["Ellie Huxtable "] edition = "2018" license = "MIT" diff --git a/README.md b/README.md index 9ff1a98..5c44041 100644 --- a/README.md +++ b/README.md @@ -25,15 +25,26 @@ As well as the expected command, A'tuin stores - time - a unique session ID +## Supported Shells + +- zsh + +## Requirements + +- [fzf](https://github.com/junegunn/fzf) + ## Install ### AUR + A'tuin is available on the [AUR](https://aur.archlinux.org/packages/atuin/) + ``` yay -S atuin # or your AUR helper of choice ``` ### With cargo + `atuin` needs a nightly version of Rust + Cargo! It's best to use [rustup](https://rustup.rs/) for getting set up there. @@ -44,6 +55,7 @@ cargo install atuin ``` ### From source + ``` rustup default nightly git clone https://github.com/ellie/atuin.git @@ -52,25 +64,51 @@ cargo install --path . ``` ### Shell plugin -Once the binary is installed, the shell plugin requires installing: -zplug: +Once the binary is installed, the shell plugin requires installing. Add ``` -zplug "ellie/atuin", at:main +eval "$(atuin init)" ``` -otherwise, clone the repo and `source /path/to/repo/atuin.plugin.zsh` in your `.zshrc` +to your `.zshrc`/`.bashrc`/whatever your shell uses. ## 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`: +### History search + +By default A'tuin will rebind ctrl-r to use fzf to fuzzy search your history. +It will also rebind the up arrow to use fzf, just without sorting. You can +prevent this by putting ``` -export ATUIN_FUZZY=fzy +export ATUIN_BINDKEYS="false" ``` +into your shell config. + +You may also change the default history selection. The default behaviour will search your entire history, however + +``` +export ATUIN_HISTORY="atuin history list --cwd" +``` + +will switch to only searching history for the current directory. + +Similarly, + +``` +export ATUIN_HISTORY="atuin history list --session" +``` + +will search for the current session only, and + +``` +export ATUIN_HISTORY="atuin history list --session --cwd" +``` + +will do both! + ### Import history ``` @@ -83,10 +121,28 @@ atuin import zsh # specify shell ### List history +List all history + ``` atuin history list ``` +List history for the current directory + +``` +atuin history list --cwd + +atuin h l -c # alternative, shorter version +``` + +List history for the current session + +``` +atuin history list --session + +atuin h l -s # similarly short +``` + ### Stats A'tuin can calculate statistics for a single day, and accepts "natural language" style date input, as well as absolute dates: diff --git a/src/command/history.rs b/src/command/history.rs index 09dd436..bd44016 100644 --- a/src/command/history.rs +++ b/src/command/history.rs @@ -30,7 +30,7 @@ pub enum Cmd { )] List { #[structopt(long, short)] - dir: bool, + cwd: bool, #[structopt(long, short)] session: bool, @@ -77,13 +77,13 @@ impl Cmd { Ok(()) } - Self::List { session, dir, .. } => { + Self::List { session, cwd, .. } => { const QUERY_SESSION: &str = "select * from history where session = ?;"; const QUERY_DIR: &str = "select * from history where cwd = ?;"; const QUERY_SESSION_DIR: &str = "select * from history where cwd = ?1 and session = ?2;"; - let params = (session, dir); + let params = (session, cwd); let cwd = env::current_dir()?.display().to_string(); let session = env::var("ATUIN_SESSION")?; diff --git a/src/command/init.rs b/src/command/init.rs new file mode 100644 index 0000000..022021d --- /dev/null +++ b/src/command/init.rs @@ -0,0 +1,19 @@ +use std::env; + +use eyre::{eyre, Result}; + +fn init_zsh() { + let full = include_str!("../shell/atuin.zsh"); + println!("{}", full); +} + +pub fn init() -> Result<()> { + let shell = env::var("SHELL")?; + + if shell.ends_with("zsh") { + init_zsh(); + Ok(()) + } else { + Err(eyre!("Could not detect shell, or shell unsupported")) + } +} diff --git a/src/command/mod.rs b/src/command/mod.rs index 78e55a0..0952540 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -6,6 +6,7 @@ use crate::local::database::Database; mod history; mod import; +mod init; mod server; mod stats; @@ -26,6 +27,9 @@ pub enum AtuinCmd { #[structopt(about = "calculate statistics for your history")] Stats(stats::Cmd), + #[structopt(about = "output shell setup")] + Init, + #[structopt(about = "generates a UUID")] Uuid, } @@ -41,6 +45,7 @@ impl AtuinCmd { Self::Import(import) => import.run(db), Self::Server(server) => server.run(), Self::Stats(stats) => stats.run(db), + Self::Init => init::init(), Self::Uuid => { println!("{}", uuid_v4()); diff --git a/src/main.rs b/src/main.rs index c4ac30b..380df59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ mod remote; #[derive(StructOpt)] #[structopt( author = "Ellie Huxtable ", - version = "0.3.1", + version = "0.3.2", about = "Magical shell history" )] struct Atuin { diff --git a/atuin.plugin.zsh b/src/shell/atuin.zsh similarity index 51% rename from atuin.plugin.zsh rename to src/shell/atuin.zsh index 83c931e..a984428 100644 --- a/atuin.plugin.zsh +++ b/src/shell/atuin.zsh @@ -1,6 +1,7 @@ # Source this in your ~/.zshrc export ATUIN_SESSION=$(atuin uuid) -export ATUIN_FUZZY=fzf +export ATUIN_HISTORY="atuin history list" +export ATUIN_BINDKEYS="true" _atuin_preexec(){ id=$(atuin history start $1) @@ -19,10 +20,23 @@ _atuin_search(){ emulate -L zsh zle -I - output=$(atuin history list --distinct | $ATUIN_FUZZY) + output=$(eval $ATUIN_HISTORY | fzf) if [[ -n $output ]] ; then - LBUFFER=$output + BUFFER=$output + fi + + zle reset-prompt +} + +_atuin_up_search(){ + emulate -L zsh + zle -I + + output=$(eval $ATUIN_HISTORY | fzf --no-sort --tac) + + if [[ -n $output ]] ; then + BUFFER=$output fi zle reset-prompt @@ -32,5 +46,9 @@ add-zsh-hook preexec _atuin_preexec add-zsh-hook precmd _atuin_precmd zle -N _atuin_search_widget _atuin_search +zle -N _atuin_up_search_widget _atuin_up_search -bindkey '^r' _atuin_search_widget +if [[ $ATUIN_BINDKEYS == "true" ]]; then + bindkey '^r' _atuin_search_widget + bindkey '^[[A' _atuin_up_search_widget +fi