diff --git a/fish/conf.d/abbr_tips.fish b/fish/conf.d/abbr_tips.fish index 848ee6d..1c6f180 100644 --- a/fish/conf.d/abbr_tips.fish +++ b/fish/conf.d/abbr_tips.fish @@ -1,7 +1,7 @@ for mode in default insert - bind --mode $mode " " '__abbr_tips_bind_space' - bind --mode $mode \n '__abbr_tips_bind_newline' - bind --mode $mode \r '__abbr_tips_bind_newline' + bind --mode $mode " " __abbr_tips_bind_space + bind --mode $mode \n __abbr_tips_bind_newline + bind --mode $mode \r __abbr_tips_bind_newline end set -g __abbr_tips_used 0 @@ -21,13 +21,9 @@ function __abbr_tips_install --on-event abbr_tips_install set -a ABBR_TIPS_REGEXES '(^(\s?(\w-?)+){1}).*' set -Ux ABBR_TIPS_PROMPT "\n💡 \e[1m{{ .abbr }}\e[0m => {{ .cmd }}" - set -gx ABBR_TIPS_AUTO_UPDATE 'background' + set -gx ABBR_TIPS_AUTO_UPDATE background - # Locking mechanism - # Prevent this file to spawn more than one subshell - if test "$USER" != 'root' - fish -c '__abbr_tips_init' & - end + __abbr_tips_init end function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the current command" @@ -35,9 +31,9 @@ function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the set -l cmd (string replace -r -a '\\s+' ' ' -- "$argv" ) # Update abbreviations lists when adding/removing abbreviations - if test "$command[1]" = "abbr" + if test "$command[1]" = abbr # Parse args as abbr options - argparse --name 'abbr' --ignore-unknown 'a/add' 'e/erase' 'g/global' 'U/universal' -- $command + argparse --name abbr --ignore-unknown a/add e/erase g/global U/universal -- $command if set -q _flag_a and not contains -- "$argv[2]" $__ABBR_TIPS_KEYS @@ -48,21 +44,18 @@ function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the set -e __ABBR_TIPS_KEYS[$abb] set -e __ABBR_TIPS_VALUES[$abb] end - else if test "$command[1]" = "alias" + else if test "$command[1]" = alias # Update abbreviations list when adding aliases set -l alias_key set -l alias_value # Parse args as `alias` options - argparse --name 'alias' --ignore-unknown 's/save' -- $command + argparse --name alias --ignore-unknown s/save -- $command if string match -q '*=*' -- "$argv[2]" - if test (count $argv) = 2 - set command_split (string split '=' -- $argv[2]) - set alias_key "a__$command_split[1]" - set alias_value $command_split[2] - set -a alias_value $command[3..-1] - end + set command_split (string split '=' -- $argv[2]) + set alias_key "a__$command_split[1]" + set alias_value $command_split[2..-1] else set alias_key "a__$argv[2]" set alias_value $argv[3..-1] @@ -77,9 +70,9 @@ function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the set -a __ABBR_TIPS_KEYS $alias_key set -a __ABBR_TIPS_VALUES $alias_value end - else if test "$command[1]" = "functions" + else if test "$command[1]" = functions # Parse args as `functions` options - argparse --name 'functions' 'e/erase' -- $command + argparse --name functions e/erase -- $command # Update abbreviations list when removing aliases if set -q _flag_e @@ -102,7 +95,7 @@ function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the return else if string match -q -- "alias $cmd *" (alias) return - else if test (type -t "$command[1]") = 'function' + else if test (type -t "$command[1]") = function and count $ABBR_TIPS_ALIAS_WHITELIST >/dev/null and not contains "$command[1]" $ABBR_TIPS_ALIAS_WHITELIST return @@ -136,20 +129,12 @@ function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the return end -function __abbr_tips_uninstall --on-event abbr_tips_uninstall - bind --erase \n - bind --erase \r - bind --erase " " - set --erase __abbr_tips_used - set --erase __abbr_tips_run_once - set --erase __ABBR_TIPS_VALUES - set --erase __ABBR_TIPS_KEYS - set --erase ABBR_TIPS_PROMPT - set --erase ABBR_TIPS_AUTO_UPDATE - set --erase ABBR_TIPS_ALIAS_WHITELIST - set --erase ABBR_TIPS_REGEXES - functions --erase __abbr_tips_init - functions --erase __abbr_tips_bind_newline - functions --erase __abbr_tips_bind_space - functions --erase __abbr_tips +function __abbr_tips_update --on-event abbr_tips_update + __abbr_tips_clean + __abbr_tips_install +end + +function __abbr_tips_uninstall --on-event abbr_tips_uninstall + __abbr_tips_clean + functions --erase __abbr_tips_init end diff --git a/fish/conf.d/sponge.fish b/fish/conf.d/sponge.fish new file mode 100644 index 0000000..e37fe38 --- /dev/null +++ b/fish/conf.d/sponge.fish @@ -0,0 +1,52 @@ +# Sponge version +set --global sponge_version 1.1.0 + +# Allow to repeat previous command by default +if not set --query --universal sponge_delay + set --universal sponge_delay 2 +end + +# Purge entries both after `sponge_delay` entries and on exit by default +if not set --query --universal sponge_purge_only_on_exit + set --universal sponge_purge_only_on_exit false +end + +# Add default filters +if not set --query --universal sponge_filters + set --universal sponge_filters sponge_filter_failed sponge_filter_matched +end + +# Don't filter out commands that already have been in the history by default +if not set --query --universal sponge_allow_previously_successful + set --universal sponge_allow_previously_successful true +end + +# Consider `0` the only successful exit code by default +if not set --query --universal sponge_successful_exit_codes + set --universal sponge_successful_exit_codes 0 +end + +# No active regex patterns by default +if not set --query --universal sponge_regex_patterns + set --universal sponge_regex_patterns +end + +# Attach event handlers +functions --query \ + _sponge_on_prompt \ + _sponge_on_preexec \ + _sponge_on_postexec \ + _sponge_on_exit + +# Initialize empty state for the first run +function _sponge_install --on-event sponge_install + set --global _sponge_current_command '' + set --global _sponge_current_command_exit_code 0 + set --global _sponge_current_command_previously_in_history false +end + +# Clean up variables +function _sponge_uninstall --on-event sponge_uninstall + _sponge_clear_state + set --erase sponge_version +end diff --git a/fish/fish_plugins b/fish/fish_plugins index 4b727d9..2749b79 100644 --- a/fish/fish_plugins +++ b/fish/fish_plugins @@ -1,6 +1,7 @@ -jorgebucaran/fisher -jethrokuan/z -jorgebucaran/replay.fish -jorgebucaran/autopair.fish -gazorby/fish-abbreviation-tips edc/bass +gazorby/fish-abbreviation-tips +jethrokuan/z +jorgebucaran/autopair.fish +jorgebucaran/fisher +jorgebucaran/replay.fish +meaningful-ooo/sponge diff --git a/fish/functions/__abbr_tips_bind_newline.fish b/fish/functions/__abbr_tips_bind_newline.fish index 8cc8992..6d91aeb 100644 --- a/fish/functions/__abbr_tips_bind_newline.fish +++ b/fish/functions/__abbr_tips_bind_newline.fish @@ -6,5 +6,5 @@ function __abbr_tips_bind_newline set -g __abbr_tips_used 0 end end - commandline -f 'execute' + commandline -f execute end diff --git a/fish/functions/__abbr_tips_bind_space.fish b/fish/functions/__abbr_tips_bind_space.fish index 0f37103..e83caf6 100644 --- a/fish/functions/__abbr_tips_bind_space.fish +++ b/fish/functions/__abbr_tips_bind_space.fish @@ -7,5 +7,5 @@ function __abbr_tips_bind_space set -g __abbr_tips_used 0 end end - commandline -f 'expand-abbr' + commandline -f expand-abbr end diff --git a/fish/functions/__abbr_tips_clean.fish b/fish/functions/__abbr_tips_clean.fish new file mode 100644 index 0000000..772bcc6 --- /dev/null +++ b/fish/functions/__abbr_tips_clean.fish @@ -0,0 +1,16 @@ +function __abbr_tips_clean -d "Clean plugin variables and functions" + bind --erase \n + bind --erase \r + bind --erase " " + set --erase __abbr_tips_used + set --erase __abbr_tips_run_once + set --erase __ABBR_TIPS_VALUES + set --erase __ABBR_TIPS_KEYS + set --erase ABBR_TIPS_PROMPT + set --erase ABBR_TIPS_AUTO_UPDATE + set --erase ABBR_TIPS_ALIAS_WHITELIST + set --erase ABBR_TIPS_REGEXES + functions --erase __abbr_tips_bind_newline + functions --erase __abbr_tips_bind_space + functions --erase __abbr_tips +end diff --git a/fish/functions/_sponge_clear_state.fish b/fish/functions/_sponge_clear_state.fish new file mode 100644 index 0000000..c6a07da --- /dev/null +++ b/fish/functions/_sponge_clear_state.fish @@ -0,0 +1,5 @@ +function _sponge_clear_state + set --erase --global _sponge_current_command + set --erase --global _sponge_current_command_exit_code + set --erase --global _sponge_current_command_previously_in_history +end diff --git a/fish/functions/_sponge_on_exit.fish b/fish/functions/_sponge_on_exit.fish new file mode 100644 index 0000000..0c6cc19 --- /dev/null +++ b/fish/functions/_sponge_on_exit.fish @@ -0,0 +1,3 @@ +function _sponge_on_exit --on-event fish_exit + sponge_delay=0 _sponge_remove_from_history +end diff --git a/fish/functions/_sponge_on_postexec.fish b/fish/functions/_sponge_on_postexec.fish new file mode 100644 index 0000000..1d67935 --- /dev/null +++ b/fish/functions/_sponge_on_postexec.fish @@ -0,0 +1,24 @@ +function _sponge_on_postexec --on-event fish_postexec + set --global _sponge_current_command_exit_code $status + + # Remove command from the queue if it's been added previously + if set --local index (contains --index -- $_sponge_current_command $_sponge_queue) + set --erase _sponge_queue[$index] + end + + # Ignore empty commands + if test -n $_sponge_current_command + set --local command '' + # Run filters + for filter in $sponge_filters + if $filter \ + $_sponge_current_command \ + $_sponge_current_command_exit_code \ + $_sponge_current_command_previously_in_history + set command $_sponge_current_command + break + end + end + set --prepend --global _sponge_queue $command + end +end diff --git a/fish/functions/_sponge_on_preexec.fish b/fish/functions/_sponge_on_preexec.fish new file mode 100644 index 0000000..a866491 --- /dev/null +++ b/fish/functions/_sponge_on_preexec.fish @@ -0,0 +1,16 @@ +function _sponge_on_preexec --on-event fish_preexec \ + --argument-names command + _sponge_clear_state + + set --global _sponge_current_command $command + + builtin history search --case-sensitive --exact --max=1 --null $command \ + | read --local --null found_entries + + # If a command is in the history and in the queue, ignore it, like if it wasn’t in the history + if test (count $found_entries) -ne 0; and not contains $command $_sponge_queue + set --global _sponge_current_command_previously_in_history true + else + set --global _sponge_current_command_previously_in_history false + end +end diff --git a/fish/functions/_sponge_on_prompt.fish b/fish/functions/_sponge_on_prompt.fish new file mode 100644 index 0000000..03e989a --- /dev/null +++ b/fish/functions/_sponge_on_prompt.fish @@ -0,0 +1,5 @@ +function _sponge_on_prompt --on-event fish_prompt + if test $sponge_purge_only_on_exit = false + _sponge_remove_from_history + end +end diff --git a/fish/functions/_sponge_remove_from_history.fish b/fish/functions/_sponge_remove_from_history.fish new file mode 100644 index 0000000..4d4f827 --- /dev/null +++ b/fish/functions/_sponge_remove_from_history.fish @@ -0,0 +1,9 @@ +function _sponge_remove_from_history + + while test (count $_sponge_queue) -gt $sponge_delay + builtin history delete --case-sensitive --exact -- $_sponge_queue[-1] + set --erase _sponge_queue[-1] + end + + builtin history save +end diff --git a/fish/functions/sponge_filter_failed.fish b/fish/functions/sponge_filter_failed.fish new file mode 100644 index 0000000..be26e5d --- /dev/null +++ b/fish/functions/sponge_filter_failed.fish @@ -0,0 +1,11 @@ +function sponge_filter_failed \ + --argument-names command exit_code previously_in_history + + if test $previously_in_history = true -a $sponge_allow_previously_successful = true + return 1 + end + + if contains $exit_code $sponge_successful_exit_codes + return 1 + end +end diff --git a/fish/functions/sponge_filter_matched.fish b/fish/functions/sponge_filter_matched.fish new file mode 100644 index 0000000..c3c7ea2 --- /dev/null +++ b/fish/functions/sponge_filter_matched.fish @@ -0,0 +1,11 @@ +function sponge_filter_matched \ + --argument-names command + + for pattern in $sponge_regex_patterns + if string match --regex --quiet $pattern -- $command + return + end + end + + return 1 +end