From cc196c89e9713ed1c6cb97f8f115659a0501c9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20La=C3=ADn?= Date: Wed, 11 Oct 2023 11:21:56 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(fish):=20abbrevation=20tips=20?= =?UTF-8?q?plugin=20installed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config/fish/conf.d/abbr_tips.fish | 140 ++++++++++++++++++ .config/fish/fish_plugins | 1 + .config/fish/fish_variables | 7 +- .../functions/__abbr_tips_bind_newline.fish | 10 ++ .../functions/__abbr_tips_bind_space.fish | 11 ++ .config/fish/functions/__abbr_tips_clean.fish | 16 ++ .config/fish/functions/__abbr_tips_init.fish | 24 +++ 7 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 .config/fish/conf.d/abbr_tips.fish create mode 100644 .config/fish/functions/__abbr_tips_bind_newline.fish create mode 100644 .config/fish/functions/__abbr_tips_bind_space.fish create mode 100644 .config/fish/functions/__abbr_tips_clean.fish create mode 100644 .config/fish/functions/__abbr_tips_init.fish diff --git a/.config/fish/conf.d/abbr_tips.fish b/.config/fish/conf.d/abbr_tips.fish new file mode 100644 index 00000000..1c6f180d --- /dev/null +++ b/.config/fish/conf.d/abbr_tips.fish @@ -0,0 +1,140 @@ +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 +end + +set -g __abbr_tips_used 0 + +# Trim simple/double quotes from args +function trim_value + echo "$argv" | string trim --left --right --chars '"\'' | string join ' ' +end + +function __abbr_tips_install --on-event abbr_tips_install + # Regexes used to find abbreviation inside command + # Only the first matching group will be tested as an abbr + set -Ux ABBR_TIPS_REGEXES + set -a ABBR_TIPS_REGEXES '(^(\w+\s+)+(-{1,2})\w+)(\s\S+)' + set -a ABBR_TIPS_REGEXES '(^(\s?(\w-?)+){3}).*' + set -a ABBR_TIPS_REGEXES '(^(\s?(\w-?)+){2}).*' + 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 + + __abbr_tips_init +end + +function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the current command" + set -l command (string split ' ' -- "$argv") + set -l cmd (string replace -r -a '\\s+' ' ' -- "$argv" ) + + # Update abbreviations lists when adding/removing abbreviations + if test "$command[1]" = abbr + # Parse args as abbr options + 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 + set -a __ABBR_TIPS_KEYS "$argv[2]" + set -a __ABBR_TIPS_VALUES (trim_value "$argv[3..-1]") + else if set -q _flag_e + and set -l abb (contains -i -- "$argv[2]" $__ABBR_TIPS_KEYS) + set -e __ABBR_TIPS_KEYS[$abb] + set -e __ABBR_TIPS_VALUES[$abb] + end + 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 + + if string match -q '*=*' -- "$argv[2]" + 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] + end + + set alias_value (trim_value "$alias_value") + + if set -l abb (contains -i -- "$argv[3..-1]" $__ABBR_TIPS_KEYS) + set __ABBR_TIPS_KEYS[$abb] $alias_key + set __ABBR_TIPS_VALUES[$abb] $alias_value + else + set -a __ABBR_TIPS_KEYS $alias_key + set -a __ABBR_TIPS_VALUES $alias_value + end + else if test "$command[1]" = functions + # Parse args as `functions` options + argparse --name functions e/erase -- $command + + # Update abbreviations list when removing aliases + if set -q _flag_e + and set -l abb (contains -i -- a__{$argv[2]} $__ABBR_TIPS_KEYS) + set -e __ABBR_TIPS_KEYS[$abb] + set -e __ABBR_TIPS_VALUES[$abb] + end + end + + # Exit in the following cases : + # - abbreviation has been used + # - command is already an abbreviation + # - command not found + # - or it's a function (alias) + if test $__abbr_tips_used = 1 + set -g __abbr_tips_used 0 + return + else if abbr -q "$cmd" + or not type -q "$command[1]" + return + else if string match -q -- "alias $cmd *" (alias) + return + 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 + end + + set -l abb + if not set abb (contains -i -- "$cmd" $__ABBR_TIPS_VALUES) + for r in $ABBR_TIPS_REGEXES + if set abb (contains -i -- (string replace -r -a -- "$r" '$1' "$cmd") $__ABBR_TIPS_VALUES) + break + end + end + end + + if test -n "$abb" + if string match -q "a__*" -- "$__ABBR_TIPS_KEYS[$abb]" + set -l alias (string sub -s 4 -- "$__ABBR_TIPS_KEYS[$abb]") + if functions -q "$alias" + echo -e (string replace -a '{{ .cmd }}' -- "$__ABBR_TIPS_VALUES[$abb]" \ + (string replace -a '{{ .abbr }}' -- "$alias" "$ABBR_TIPS_PROMPT")) + else + set -e __ABBR_TIPS_KEYS[$abb] + set -e __ABBR_TIPS_VALUES[$abb] + end + else + echo -e (string replace -a '{{ .cmd }}' -- "$__ABBR_TIPS_VALUES[$abb]" \ + (string replace -a '{{ .abbr }}' -- "$__ABBR_TIPS_KEYS[$abb]" "$ABBR_TIPS_PROMPT")) + end + end + + return +end + +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/.config/fish/fish_plugins b/.config/fish/fish_plugins index 64bec91d..9ce2b051 100644 --- a/.config/fish/fish_plugins +++ b/.config/fish/fish_plugins @@ -4,3 +4,4 @@ joehillen/to-fish patrickf1/fzf.fish wfxr/forgit danhper/fish-ssh-agent +gazorby/fish-abbreviation-tips diff --git a/.config/fish/fish_variables b/.config/fish/fish_variables index 3b219158..0e7c767e 100644 --- a/.config/fish/fish_variables +++ b/.config/fish/fish_variables @@ -1,5 +1,7 @@ # This file contains fish universal variable definitions. # VERSION: 3.0 +SETUVAR --export ABBR_TIPS_PROMPT:\x5cn\U0001f4a1\x20\x5ce\x5b1m\x7b\x7b\x20\x2eabbr\x20\x7d\x7d\x5ce\x5b0m\x20\x3d\x3e\x20\x7b\x7b\x20\x2ecmd\x20\x7d\x7d +SETUVAR --export ABBR_TIPS_REGEXES:\x28\x5e\x28\x5cw\x2b\x5cs\x2b\x29\x2b\x28\x2d\x7b1\x2c2\x7d\x29\x5cw\x2b\x29\x28\x5cs\x5cS\x2b\x29\x1e\x28\x5e\x28\x5cs\x3f\x28\x5cw\x2d\x3f\x29\x2b\x29\x7b3\x7d\x29\x2e\x2a\x1e\x28\x5e\x28\x5cs\x3f\x28\x5cw\x2d\x3f\x29\x2b\x29\x7b2\x7d\x29\x2e\x2a\x1e\x28\x5e\x28\x5cs\x3f\x28\x5cw\x2d\x3f\x29\x2b\x29\x7b1\x7d\x29\x2e\x2a SETUVAR --export EDITOR:nvim SETUVAR --export --path GOPATH:/home/matt/\x2elocal/share/go SETUVAR --export LANG:es_ES\x2eUTF\x2d8 @@ -11,13 +13,16 @@ SETUVAR --export XDG_CACHE_HOME:/home/matt/\x2ecache SETUVAR --export XDG_CONFIG_HOME:/home/matt/\x2econfig SETUVAR --export XDG_DATA_HOME:/home/matt/\x2elocal/share SETUVAR --export XDG_SCRIPT_HOME:/home/matt/\x2elocal/script +SETUVAR --export __ABBR_TIPS_KEYS:a__bruh\x1ea__cat\x1ea__cd\x1ea__clock\x1ea__codeinfo\x1ea__disks\x1ea__dots\x1ea__dsize\x1ea__ea\x1ea__ef\x1ea__eg\x1ea__ev\x1ea__f\x1ea__fetch\x1ea__files\x1ea__fsend\x1ea__ga\x1ea__gbd\x1ea__gbl\x1ea__gcb\x1ea__gcf\x1ea__gclean\x1ea__gco\x1ea__gcp\x1ea__gct\x1ea__gd\x1ea__gfetch\x1ea__gfu\x1ea__gi\x1ea__glo\x1ea__gpt\x1ea__grb\x1ea__grc\x1ea__grh\x1ea__gsp\x1ea__gss\x1ea__h\x1ea__info\x1ea__install\x1ea__ip\x1ea__l\x1ea__ld\x1ea__ldh\x1ea__lg\x1ea__lh\x1ea__ls\x1ea__lsh\x1ea__lt\x1ea__lth\x1ea__matrix\x1ea__pages\x1ea__q\x1ea__svn\x1ea__sysproc\x1ea__uninstall\x1ea__update\x1ea__vi\x1ea__vim\x1ea__yP\x1ea__ya\x1ea__yarn\x1ea__yc\x1ea__ym\x1ea__yp\x1ea__yst\x1ea__ysw\x1ea__z\x1ea__zi +SETUVAR --export __ABBR_TIPS_VALUES:genact\x20\x2ds\x204\x1ebat\x1ez\x1etty\x2dclock\x20\x2dsbc\x1escc\x20\x2e/\x1eduf\x1eyadm\x20enter\x20lazygit\x1edua\x20i\x1envim\x20\x7e/\x2econfig/fish/aliases\x2efish\x1envim\x20\x7e/\x2econfig/fish/config\x2efish\x1envim\x20\x7e/\x2egitconfig\x1envim\x20\x7e/\x2econfig/fish/variables\x2efish\x1efzf\x1eneofetch\x1explr\x1efloaterm\x1eforgit\x3a\x3aadd\x1eforgit\x3a\x3abranch\x3a\x3adelete\x1eforgit\x3a\x3ablame\x1eforgit\x3a\x3acheckout\x3a\x3abranch\x1eforgit\x3a\x3acheckout\x3a\x3afile\x1eforgit\x3a\x3aclean\x1eforgit\x3a\x3acheckout\x3a\x3acommit\x1eforgit\x3a\x3acherry\x3a\x3apick\x3a\x3afrom\x3a\x3abranch\x1eforgit\x3a\x3acheckout\x3a\x3atag\x1eforgit\x3a\x3adiff\x1eonefetch\x1eforgit\x3a\x3afixup\x1eforgit\x3a\x3aignore\x1eforgit\x3a\x3alog\x1etgpt\x20\x2di\x1eforgit\x3a\x3arebase\x1eforgit\x3a\x3arevert\x3a\x3acommit\x1eforgit\x3a\x3areset\x3a\x3ahead\x1eforgit\x3a\x3astash\x3a\x3apush\x1eforgit\x3a\x3astash\x3a\x3ashow\x1ezi\x1etldr\x1eyay\x20\x2dS\x20\x1eip\x20\x2dc\x20a\x1eeza\x20\x2d\x2dlong\x20\x2d\x2dheader\x20\x2da\x20\x2d\x2dicons\x20\x2d\x2dgit\x20\x2d\x2dgroup\x2ddirectories\x2dfirst\x1eeza\x20\x2d\x2dlong\x20\x2d\x2dheader\x20\x2da\x20\x2d\x2dicons\x20\x2dD\x20\x2d\x2dgit\x1eeza\x20\x2d\x2dlong\x20\x2d\x2dheader\x20\x2d\x2dicons\x20\x2dD\x20\x2d\x2dgit\x1elazygit\x1eeza\x20\x2d\x2dlong\x20\x2d\x2dheader\x20\x2d\x2dicons\x20\x2d\x2dgit\x20\x2d\x2dgroup\x2ddirectories\x2dfirst\x1eeza\x20\x2da\x20\x2d\x2dicons\x20\x2d\x2dgroup\x2ddirectories\x2dfirst\x1eeza\x20\x2d\x2dicons\x20\x2d\x2dgit\x20\x2d\x2dgroup\x2ddirectories\x2dfirst\x1eeza\x20\x2d\x2dlong\x20\x2d\x2dheader\x20\x2da\x20\x2d\x2dicons\x20\x2d\x2dtree\x20\x2d\x2dgit\x20\x2d\x2dgroup\x2ddirectories\x2dfirst\x1eeza\x20\x2d\x2dlong\x20\x2d\x2dheader\x20\x2d\x2dicons\x20\x2d\x2dtree\x20\x2d\x2dgit\x20\x2d\x2dgroup\x2ddirectories\x2dfirst\x1eunimatrix\x20\x2ds\x2095\x1enavi\x1eexit\x1esvn\x5c\x5c\x5c\x20\x2d\x2dconfig\x2ddir\x5c\x5c\x5c\x20\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x22\x5c\x5c\x5c\x24XDG_CONFIG_HOME\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x22/subversion\x1esysz\x1eyay\x20\x2dR\x20\x1eyay\x20\x2dSyu\x1envim\x1envim\x1eyadm\x20push\x1eyadm\x20add\x1eyarn\x20\x2d\x2duse\x2dyarnrc\x20\x22\x24XDG_CONFIG_HOME/yarn/config\x22\x1eyadm\x20commit\x20\x2dS\x20\x2da\x20\x2dm\x1eyadm\x20merge\x1eyadm\x20pull\x1eyadm\x20status\x1eyadm\x20switch\x1e__zoxide_z\x1e__zoxide_zi SETUVAR __fish_initialized:3400 SETUVAR _fisher_acomagu_2F_fish_2D_async_2D_prompt_files:\x7e/\x2econfig/fish/conf\x2ed/__async_prompt\x2efish SETUVAR _fisher_danhper_2F_fish_2D_ssh_2D_agent_files:\x7e/\x2econfig/fish/functions/__ssh_agent_is_started\x2efish\x1e\x7e/\x2econfig/fish/functions/__ssh_agent_start\x2efish\x1e\x7e/\x2econfig/fish/conf\x2ed/fish\x2dssh\x2dagent\x2efish SETUVAR _fisher_franciscolourenco_2F_done_files:\x7e/\x2econfig/fish/conf\x2ed/done\x2efish +SETUVAR _fisher_gazorby_2F_fish_2D_abbreviation_2D_tips_files:\x7e/\x2econfig/fish/functions/__abbr_tips_bind_newline\x2efish\x1e\x7e/\x2econfig/fish/functions/__abbr_tips_bind_space\x2efish\x1e\x7e/\x2econfig/fish/functions/__abbr_tips_clean\x2efish\x1e\x7e/\x2econfig/fish/functions/__abbr_tips_init\x2efish\x1e\x7e/\x2econfig/fish/conf\x2ed/abbr_tips\x2efish SETUVAR _fisher_joehillen_2F_to_2D_fish_files:\x7e/\x2econfig/fish/functions/to\x2efish\x1e\x7e/\x2econfig/fish/conf\x2ed/to\x2efish\x1e\x7e/\x2econfig/fish/completions/to\x2efish SETUVAR _fisher_patrickf1_2F_fzf_2E_fish_files:\x7e/\x2econfig/fish/functions/_fzf_configure_bindings_help\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_extract_var_info\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_preview_changed_file\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_preview_file\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_report_diff_type\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_report_file_type\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_directory\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_git_log\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_git_status\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_history\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_processes\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_variables\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_wrapper\x2efish\x1e\x7e/\x2econfig/fish/functions/fzf_configure_bindings\x2efish\x1e\x7e/\x2econfig/fish/conf\x2ed/fzf\x2efish\x1e\x7e/\x2econfig/fish/completions/fzf_configure_bindings\x2efish -SETUVAR _fisher_plugins:franciscolourenco/done\x1eacomagu/fish\x2dasync\x2dprompt\x1ejoehillen/to\x2dfish\x1epatrickf1/fzf\x2efish\x1ewfxr/forgit\x1edanhper/fish\x2dssh\x2dagent +SETUVAR _fisher_plugins:franciscolourenco/done\x1eacomagu/fish\x2dasync\x2dprompt\x1ejoehillen/to\x2dfish\x1epatrickf1/fzf\x2efish\x1ewfxr/forgit\x1edanhper/fish\x2dssh\x2dagent\x1egazorby/fish\x2dabbreviation\x2dtips SETUVAR _fisher_upgraded_to_4_4:\x1d SETUVAR _fisher_wfxr_2F_forgit_files:\x7e/\x2econfig/fish/conf\x2ed/bin\x1e\x7e/\x2econfig/fish/conf\x2ed/forgit\x2eplugin\x2efish\x1e\x7e/\x2econfig/fish/completions/_git\x2dforgit\x1e\x7e/\x2econfig/fish/completions/git\x2dforgit\x2ebash\x1e\x7e/\x2econfig/fish/completions/git\x2dforgit\x2ezsh SETUVAR fish_color_autosuggestion:6e738d diff --git a/.config/fish/functions/__abbr_tips_bind_newline.fish b/.config/fish/functions/__abbr_tips_bind_newline.fish new file mode 100644 index 00000000..6d91aebd --- /dev/null +++ b/.config/fish/functions/__abbr_tips_bind_newline.fish @@ -0,0 +1,10 @@ +function __abbr_tips_bind_newline + if test $__abbr_tips_used != 1 + if abbr -q -- (string trim -- (commandline)) + set -g __abbr_tips_used 1 + else + set -g __abbr_tips_used 0 + end + end + commandline -f execute +end diff --git a/.config/fish/functions/__abbr_tips_bind_space.fish b/.config/fish/functions/__abbr_tips_bind_space.fish new file mode 100644 index 00000000..e83caf62 --- /dev/null +++ b/.config/fish/functions/__abbr_tips_bind_space.fish @@ -0,0 +1,11 @@ +function __abbr_tips_bind_space + commandline -i " " + if test $__abbr_tips_used != 1 + if abbr -q -- (string trim -- (commandline)) + set -g __abbr_tips_used 1 + else + set -g __abbr_tips_used 0 + end + end + commandline -f expand-abbr +end diff --git a/.config/fish/functions/__abbr_tips_clean.fish b/.config/fish/functions/__abbr_tips_clean.fish new file mode 100644 index 00000000..772bcc6d --- /dev/null +++ b/.config/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/.config/fish/functions/__abbr_tips_init.fish b/.config/fish/functions/__abbr_tips_init.fish new file mode 100644 index 00000000..9cda53ad --- /dev/null +++ b/.config/fish/functions/__abbr_tips_init.fish @@ -0,0 +1,24 @@ +function __abbr_tips_init -d "Initialize abbreviations variables for fish-abbr-tips" + set -e __ABBR_TIPS_KEYS + set -e __ABBR_TIPS_VALUES + set -Ux __ABBR_TIPS_KEYS + set -Ux __ABBR_TIPS_VALUES + + set -l i 1 + set -l abb (string replace -r '.*-- ' '' -- (abbr -s)) + while test $i -le (count $abb) + set -l current_abb (string split -m1 -- ' ' "$abb[$i]") + set -a __ABBR_TIPS_KEYS "$current_abb[1]" + set -a __ABBR_TIPS_VALUES (string trim -c '\'' -- "$current_abb[2]") + set i (math $i + 1) + end + + set -l i 1 + set -l abb (string replace -r '.*-- ' '' -- (alias -s)) + while test $i -le (count $abb) + set -l current_abb (string split -m2 -- ' ' "$abb[$i]") + set -a __ABBR_TIPS_KEYS "a__$current_abb[2]" + set -a __ABBR_TIPS_VALUES (string trim -c '\'' -- "$current_abb[3]") + set i (math $i + 1) + end +end