diff options
author | Ellie Huxtable <e@elm.sh> | 2021-02-15 23:33:30 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-15 23:33:30 +0000 |
commit | 41f072a8b443b5a404916598f0c9b7a52f5c05fb (patch) | |
tree | 449643847a5fee7c290976075a2f6fb36547aca0 /src | |
parent | e980973ba01ecac5fcf9f9802c1022702fe34655 (diff) |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/command/history.rs | 6 | ||||
-rw-r--r-- | src/command/init.rs | 19 | ||||
-rw-r--r-- | src/command/mod.rs | 5 | ||||
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/shell/atuin.zsh | 54 |
5 files changed, 82 insertions, 4 deletions
diff --git a/src/command/history.rs b/src/command/history.rs index 09dd4364..bd440163 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 00000000..022021d0 --- /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 78e55a0d..0952540b 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 c4ac30b2..380df592 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ mod remote; #[derive(StructOpt)] #[structopt( author = "Ellie Huxtable <e@elm.sh>", - version = "0.3.1", + version = "0.3.2", about = "Magical shell history" )] struct Atuin { diff --git a/src/shell/atuin.zsh b/src/shell/atuin.zsh new file mode 100644 index 00000000..a984428f --- /dev/null +++ b/src/shell/atuin.zsh @@ -0,0 +1,54 @@ +# Source this in your ~/.zshrc +export ATUIN_SESSION=$(atuin uuid) +export ATUIN_HISTORY="atuin history list" +export ATUIN_BINDKEYS="true" + +_atuin_preexec(){ + id=$(atuin history start $1) + export ATUIN_HISTORY_ID="$id" +} + +_atuin_precmd(){ + local EXIT="$?" + + [[ -z "${ATUIN_HISTORY_ID}" ]] && return + + atuin history end $ATUIN_HISTORY_ID --exit $EXIT +} + +_atuin_search(){ + emulate -L zsh + zle -I + + output=$(eval $ATUIN_HISTORY | fzf) + + if [[ -n $output ]] ; then + 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 +} + +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 + +if [[ $ATUIN_BINDKEYS == "true" ]]; then + bindkey '^r' _atuin_search_widget + bindkey '^[[A' _atuin_up_search_widget +fi |