From 2f9df9350d5b645c867a440e63071d323fd53409 Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Fri, 20 Oct 2023 07:57:29 +0100 Subject: [PATCH] Add fish support for `enter_accept` (#1315) * Add fish support for `enter_accept` Also fixes shell detection. Who trusted me to write jetlagged code last night huh? * Document --- atuin-common/src/utils.rs | 7 ++++++- atuin/src/command/client/search/interactive.rs | 3 ++- atuin/src/shell/atuin.fish | 12 ++++++++++-- atuin/src/shell/atuin.zsh | 3 +-- docs/docs/config/config.md | 2 +- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/atuin-common/src/utils.rs b/atuin-common/src/utils.rs index cd2cd4d..65fa40c 100644 --- a/atuin-common/src/utils.rs +++ b/atuin-common/src/utils.rs @@ -115,7 +115,12 @@ pub fn get_current_dir() -> String { pub fn is_zsh() -> bool { // only set on zsh - env::var("ZSH_VERSION").is_ok() + env::var("ATUIN_SHELL_ZSH").is_ok() +} + +pub fn is_fish() -> bool { + // only set on zsh + env::var("ATUIN_SHELL_FISH").is_ok() } #[cfg(test)] diff --git a/atuin/src/command/client/search/interactive.rs b/atuin/src/command/client/search/interactive.rs index bfee81d..d900433 100644 --- a/atuin/src/command/client/search/interactive.rs +++ b/atuin/src/command/client/search/interactive.rs @@ -703,9 +703,10 @@ pub async fn history( if index < results.len() { let mut command = results.swap_remove(index).command; - if accept && utils::is_zsh() { + if accept && (utils::is_zsh() || utils::is_fish()) { command = String::from("__atuin_accept__:") + &command; } + // index is in bounds so we return that entry Ok(command) } else if index == RETURN_ORIGINAL { diff --git a/atuin/src/shell/atuin.fish b/atuin/src/shell/atuin.fish index 5b7b64e..2f007fc 100644 --- a/atuin/src/shell/atuin.fish +++ b/atuin/src/shell/atuin.fish @@ -8,18 +8,25 @@ end function _atuin_postexec --on-event fish_postexec set s $status + if test -n "$ATUIN_HISTORY_ID" ATUIN_LOG=error atuin history end --exit $s -- $ATUIN_HISTORY_ID &>/dev/null & disown end + set --erase ATUIN_HISTORY_ID end function _atuin_search - set h (ATUIN_LOG=error atuin search $argv -i -- (commandline -b) 3>&1 1>&2 2>&3) + set h (ATUIN_SHELL_FISH=t ATUIN_LOG=error atuin search $argv -i -- (commandline -b) 3>&1 1>&2 2>&3) commandline -f repaint + if test -n "$h" - commandline -r "$h" + if string match -g '__atuin_accept__:*' "$h" + eval (string match -r '__atuin_accept__:(.*|\s*)' "$h" | awk 'NR == 2') + else + commandline -r "$h" + end end end @@ -32,6 +39,7 @@ function _atuin_bind_up # Only invoke atuin if we're on the top line of the command set -l lineno (commandline --line) + switch $lineno case 1 _atuin_search --shell-up-key-binding diff --git a/atuin/src/shell/atuin.zsh b/atuin/src/shell/atuin.zsh index 19941be..44dda4f 100644 --- a/atuin/src/shell/atuin.zsh +++ b/atuin/src/shell/atuin.zsh @@ -10,7 +10,6 @@ autoload -U add-zsh-hook export ATUIN_SESSION=$(atuin uuid) -export ATUIN_HISTORY="atuin history list" _atuin_preexec() { local id @@ -34,7 +33,7 @@ _atuin_search() { # swap stderr and stdout, so that the tui stuff works # TODO: not this # shellcheck disable=SC2048 - output=$(ATUIN_LOG=error atuin search $* -i -- $BUFFER 3>&1 1>&2 2>&3) + output=$(ATUIN_SHELL_ZSH=t ATUIN_LOG=error atuin search $* -i -- $BUFFER 3>&1 1>&2 2>&3) zle reset-prompt diff --git a/docs/docs/config/config.md b/docs/docs/config/config.md index 5cdbe92..90f4fe5 100644 --- a/docs/docs/config/config.md +++ b/docs/docs/config/config.md @@ -309,7 +309,7 @@ remote sync server. Any longer than this and the request will fail. ## enter_accept Default: false -Only supported on Zsh. +Only supported on Zsh and Fish. When set to true, Atuin will default to immediately executing a command rather than the user having to press enter twice. Pressing tab will return to the