├── tests ├── _resources │ ├── target.txt │ └── dir with spaces │ │ ├── file 1.txt │ │ ├── file 2.txt │ │ ├── file.json │ │ ├── symlink.txt │ │ ├── file.7z │ │ ├── file.bin │ │ ├── file.pdf │ │ └── fish.png ├── test_open_cmd.fish ├── test_preview_file.fish ├── test_open_file.fish ├── test_group.fish ├── test_source.fish ├── test_file_type.fish ├── test_fifc.fish ├── test_exposed_vars.fish ├── version_test.fish └── test_match_order.fish ├── functions ├── _fifc_open_cmd.fish ├── _fifc_expand_tilde.fish ├── _fifc_open_dir.fish ├── _fifc_parse_pid.fish ├── _fifc_parse_complist.fish ├── _fifc_open_fn.fish ├── _fifc_preview_fn.fish ├── _fifc_preview_dir.fish ├── _fifc_path_to_complete.fish ├── _fifc_preview_cmd.fish ├── _fifc_open_process.fish ├── _fifc_preview_file_default.fish ├── _fifc_source_directories.fish ├── _fifc_preview_process.fish ├── _fifc_open_opt.fish ├── _fifc_test_version.fish ├── _fifc_file_type.fish ├── _fifc_open_file.fish ├── _fifc_completion_group.fish ├── _fifc_source_files.fish ├── _fifc_preview_opt.fish ├── _fifc_preview_file.fish ├── fifc.fish ├── _fifc_action.fish ├── _fifc.fish └── _fifc_help.fish ├── CHANGELOG.md ├── cz.yaml ├── .pre-commit-config.yaml ├── .github ├── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md └── workflows │ └── ci.yml ├── completions └── fifc.fish ├── LICENSE ├── conf.d └── fifc.fish └── README.md /tests/_resources/target.txt: -------------------------------------------------------------------------------- 1 | foo 2 | -------------------------------------------------------------------------------- /tests/_resources/dir with spaces/file 1.txt: -------------------------------------------------------------------------------- 1 | foo 1 2 | -------------------------------------------------------------------------------- /tests/_resources/dir with spaces/file 2.txt: -------------------------------------------------------------------------------- 1 | foo 2 2 | -------------------------------------------------------------------------------- /tests/_resources/dir with spaces/file.json: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /tests/_resources/dir with spaces/symlink.txt: -------------------------------------------------------------------------------- 1 | ../target.txt -------------------------------------------------------------------------------- /functions/_fifc_open_cmd.fish: -------------------------------------------------------------------------------- 1 | function _fifc_open_cmd 2 | _fifc_preview_cmd $fifc_candidate 3 | end 4 | -------------------------------------------------------------------------------- /functions/_fifc_expand_tilde.fish: -------------------------------------------------------------------------------- 1 | function _fifc_expand_tilde 2 | string replace --regex -- '^~' "$HOME" $argv 3 | end 4 | -------------------------------------------------------------------------------- /tests/_resources/dir with spaces/file.7z: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gazorby/fifc/HEAD/tests/_resources/dir with spaces/file.7z -------------------------------------------------------------------------------- /tests/_resources/dir with spaces/file.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gazorby/fifc/HEAD/tests/_resources/dir with spaces/file.bin -------------------------------------------------------------------------------- /tests/_resources/dir with spaces/file.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gazorby/fifc/HEAD/tests/_resources/dir with spaces/file.pdf -------------------------------------------------------------------------------- /tests/_resources/dir with spaces/fish.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gazorby/fifc/HEAD/tests/_resources/dir with spaces/fish.png -------------------------------------------------------------------------------- /functions/_fifc_open_dir.fish: -------------------------------------------------------------------------------- 1 | function _fifc_open_dir 2 | if type -q broot 3 | broot --color=yes $fifc_broot_opts $fifc_candidate 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /functions/_fifc_parse_pid.fish: -------------------------------------------------------------------------------- 1 | function _fifc_parse_pid -d "Extract pid at the beginning of ps output lines" 2 | string match --regex --groups-only -- "^\h*([0-9]+)" "$argv" 3 | end 4 | -------------------------------------------------------------------------------- /tests/test_open_cmd.fish: -------------------------------------------------------------------------------- 1 | set fifc_candidate mkdir 2 | set fifc_bat_opts '--color=never' 3 | 4 | set actual (_fifc_open_cmd) 5 | set expected (man mkdir) 6 | @test "builtin cmd open" "$actual" = "$expected" 7 | -------------------------------------------------------------------------------- /tests/test_preview_file.fish: -------------------------------------------------------------------------------- 1 | set dir "tests/_resources/dir with spaces" 2 | set fifc_candidate "$dir/file 1.txt" 3 | set fifc_bat_opts '--color=never' 4 | 5 | set actual (_fifc_preview_file) 6 | @test "builtin file preview" "$actual" = 'foo 1' 7 | -------------------------------------------------------------------------------- /functions/_fifc_parse_complist.fish: -------------------------------------------------------------------------------- 1 | function _fifc_parse_complist -d "Extract the first column of fish completion list" 2 | cat $_fifc_complist_path \ 3 | | string unescape \ 4 | | uniq \ 5 | | awk -F '\t' '{ print $1 }' 6 | end 7 | -------------------------------------------------------------------------------- /functions/_fifc_open_fn.fish: -------------------------------------------------------------------------------- 1 | function _fifc_open_fn -d "Open a function definition using open file wrapper" 2 | set -l pathname (functions --details $fifc_candidate 2>/dev/null) 3 | if test -f $pathname 4 | _fifc_open_file $pathname 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /functions/_fifc_preview_fn.fish: -------------------------------------------------------------------------------- 1 | function _fifc_preview_fn -d "Preview the function definition" 2 | if type -q bat 3 | type $fifc_candidate | bat --color=always --language fish $fifc_bat_opts 4 | else 5 | type $fifc_candidate 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /functions/_fifc_preview_dir.fish: -------------------------------------------------------------------------------- 1 | function _fifc_preview_dir -d "List content of the selected directory" 2 | if type -q exa 3 | exa --color=always $fifc_exa_opts $fifc_candidate 4 | else 5 | ls --color=always $fifc_ls_opts $fifc_candidate 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /functions/_fifc_path_to_complete.fish: -------------------------------------------------------------------------------- 1 | function _fifc_path_to_complete 2 | set -l token (string unescape -- $fifc_token) 3 | if string match --regex --quiet -- '.*(\w|\.|/)+$' "$token" 4 | _fifc_expand_tilde "$token" 5 | else 6 | echo {$PWD}/ 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /functions/_fifc_preview_cmd.fish: -------------------------------------------------------------------------------- 1 | function _fifc_preview_cmd -d "Open man page of the selected command" 2 | if type -q bat 3 | man $fifc_candidate 2>/dev/null | bat --color=always --language man $fifc_bat_opts 4 | else 5 | man $fifc_candidate 2>/dev/null 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## v0.1.1 (2023-03-05) 2 | 3 | ### Fix 4 | 5 | - **directories**: escape name with spaces instead of quoting them 6 | 7 | ### Perf 8 | 9 | - improve completion group determination 10 | 11 | ## v0.1.0 (2023-01-23) 12 | 13 | ### Feat 14 | 15 | - use `$EDITOR` as default editor 16 | -------------------------------------------------------------------------------- /functions/_fifc_open_process.fish: -------------------------------------------------------------------------------- 1 | function _fifc_open_process -d "Open the tree view of the selected process (procs only)" 2 | set -l pid (_fifc_parse_pid "$fifc_candidate") 3 | 4 | if type -q procs 5 | procs --color=always --tree --pager=always $fifc_procs_opts "$pid" 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /tests/test_open_file.fish: -------------------------------------------------------------------------------- 1 | set dir "tests/_resources/dir with spaces" 2 | set curr_fifc_editor $fifc_editor 3 | set fifc_editor cat 4 | set fifc_candidate "$dir/file 1.txt" 5 | 6 | set actual (_fifc_open_file) 7 | @test "builtin file open" "$actual" = 'foo 1' 8 | 9 | set fifc_editor $curr_fifc_editor 10 | -------------------------------------------------------------------------------- /functions/_fifc_preview_file_default.fish: -------------------------------------------------------------------------------- 1 | function _fifc_preview_file_default -d "Display informations about the selected file" 2 | set -l mime (file --mime-type -b "$argv") 3 | set_color brgreen 4 | echo -e "$mime[1]\n" 5 | set_color --bold white 6 | file -b "$argv" 7 | set_color normal 8 | end 9 | -------------------------------------------------------------------------------- /functions/_fifc_source_directories.fish: -------------------------------------------------------------------------------- 1 | function _fifc_source_directories -d "Return a command to recursively find directories" 2 | if type -q fd 3 | set --local --export fifc_fd_opts -t d 4 | _fifc_source_files 5 | else 6 | set --local --export fifc_find_opts -type d 7 | _fifc_source_files 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /functions/_fifc_preview_process.fish: -------------------------------------------------------------------------------- 1 | function _fifc_preview_process -d "Preview process informations" 2 | set -l pid (_fifc_parse_pid "$fifc_candidate") 3 | set -l err_msg "\nThe process exited" 4 | 5 | if type -q procs 6 | procs --color=always --tree $fifc_procs_opts "$pid" 7 | else 8 | set -l ps_preview_fmt (string join ',' 'pid' 'ppid=PARENT' 'user' '%cpu' 'rss=RSS_IN_KB' 'start=START_TIME' 'command') 9 | ps -o "$ps_preview_fmt" -p "$pid" 2>/dev/null 10 | end 11 | if not ps -p "$pid" &>/dev/null 12 | set_color yellow 13 | echo -e "$err_msg" 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /cz.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | commitizen: 3 | annotated_tag: true 4 | bump_message: v$new_version 5 | name: cz_conventional_commits 6 | style: 7 | - - qmark 8 | - fg:#ff9d00 bold 9 | - - question 10 | - bold 11 | - - answer 12 | - fg:#ff9d00 bold 13 | - - pointer 14 | - fg:#ff9d00 bold 15 | - - highlighted 16 | - fg:#ff9d00 bold 17 | - - selected 18 | - fg:#cc5454 19 | - - separator 20 | - fg:#cc5454 21 | - - instruction 22 | - "" 23 | - - text 24 | - "" 25 | - - disabled 26 | - fg:#858585 italic 27 | tag_format: v$version 28 | version: 0.1.1 29 | -------------------------------------------------------------------------------- /functions/_fifc_open_opt.fish: -------------------------------------------------------------------------------- 1 | function _fifc_open_opt -d "Open a man page starting at the selected option" 2 | set -l cmd (string match --regex --groups-only -- '(^|\h+)(\w+) ?-*$' "$fifc_commandline") 3 | set -l opt (string trim --chars '\n ' -- "$fifc_candidate") 4 | set -l regex "^\h*(-+[^\n]*)*$opt" 5 | 6 | if type -q bat 7 | man $cmd \ 8 | | bat --color=always --language man $fifc_bat_opts \ 9 | # --RAW-CONTROL-CHARS allow color output of bat to be displayed 10 | | less --RAW-CONTROL-CHARS --pattern "$regex" 11 | else 12 | man $cmd | less --pattern "$regex" 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/pre-commit/pre-commit-hooks 3 | rev: v5.0.0 4 | hooks: 5 | - id: check-toml 6 | - id: mixed-line-ending 7 | - id: check-case-conflict 8 | - id: check-merge-conflict 9 | - id: check-docstring-first 10 | - id: check-added-large-files 11 | - id: check-executables-have-shebangs 12 | - id: debug-statements 13 | - id: end-of-file-fixer 14 | - id: fix-byte-order-marker 15 | - id: trailing-whitespace 16 | 17 | - repo: https://github.com/hugoh/pre-commit-fish.git 18 | rev: v1.2 19 | hooks: 20 | - id: fish_syntax 21 | - id: fish_indent 22 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: "" 5 | labels: enhancement 6 | assignees: "" 7 | --- 8 | 9 | **Is your feature request related to a problem? Please describe.** 10 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 11 | 12 | **Describe the solution you'd like** 13 | A clear and concise description of what you want to happen. 14 | 15 | **Describe alternatives you've considered** 16 | A clear and concise description of any alternative solutions or features you've considered. 17 | 18 | **Additional context** 19 | Add any other context or screenshots about the feature request here. 20 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: "" 5 | labels: bug 6 | assignees: Gazorby 7 | --- 8 | 9 | **Describe the bug** 10 | A clear and concise description of what the bug is. 11 | 12 | **To Reproduce** 13 | Steps to reproduce the behavior: 14 | 15 | **Expected behavior** 16 | A clear and concise description of what you expected to happen. 17 | 18 | **Screenshots** 19 | If applicable, add screenshots to help explain your problem. 20 | 21 | **System (please complete the following information):** 22 | 23 | - fish version 24 | - Distribution `cat /etc/os-release` 25 | 26 | **Additional context** 27 | Add any other context about the problem here. 28 | -------------------------------------------------------------------------------- /completions/fifc.fish: -------------------------------------------------------------------------------- 1 | complete fifc --long condition --short -n --description "Command or fish function to be evaluated" 2 | complete fifc --long regex --short r --description "Regex matching the part of the commandline before cursor" 3 | complete fifc --long extract --short e --description "Regex applied on the sele cted candidate(s) to extract string to bo appended to the commandline (using capture groups)" 4 | complete fifc --long order --short O --description "Order in which this rule must be evaluated" 5 | complete fifc --long preview --short p --description "Preview command to be evaluated on the current candidate" 6 | complete fifc --long open --short o --description "Open command to be evaluated on the current candidate" 7 | complete fifc --long fzf-options --short f --description "Custom fzf options" 8 | -------------------------------------------------------------------------------- /functions/_fifc_test_version.fish: -------------------------------------------------------------------------------- 1 | function _fifc_test_version -d "Compare version numbers" 2 | set -l arg_1 (string replace --regex --all '[^\d]' '' -- "$argv[1]") 3 | set -l arg_2 (string replace --regex --all '[^\d]' '' -- "$argv[3]") 4 | set -l op "$argv[2]" 5 | 6 | set -l v_diff (math (string length -- $arg_1) - (string length -- $arg_2)) 7 | 8 | # Ensure both versions are the same length 9 | if test $v_diff -gt 0 10 | set arg_2 (string join '' -- "$arg_2" (string repeat -N -n $v_diff '0')) 11 | else if test $v_diff -lt 0 12 | set v_diff (math abs $v_diff) 13 | set arg_1 (string join '' -- "$arg_1" (string repeat -N -n $v_diff '0')) 14 | end 15 | 16 | set -l cmd (string collect -- "test " "$arg_1" " $op " "$arg_2") 17 | eval $cmd 18 | end 19 | -------------------------------------------------------------------------------- /tests/test_group.fish: -------------------------------------------------------------------------------- 1 | set _fifc_complist_path (mktemp) 2 | 3 | set _commandline "kill " 4 | complete -C --escape -- "$_commandline" >$_fifc_complist_path 5 | set fifc_commandline "$_commandline" 6 | set actual (_fifc_completion_group) 7 | @test "group test pid" "$actual" = processes 8 | 9 | set _commandline "ls tests/_resources/dir\ with\ spaces/" 10 | complete -C --escape -- "$_commandline" >$_fifc_complist_path 11 | set fifc_commandline "$_commandline" 12 | set actual (_fifc_completion_group) 13 | @test "group test files" "$actual" = files 14 | 15 | set _commandline "ls -" 16 | complete -C --escape -- "$_commandline" >$_fifc_complist_path 17 | set fifc_commandline "$_commandline" 18 | set actual (_fifc_completion_group) 19 | @test "group test options" "$actual" = options 20 | 21 | set -e _fifc_complist 22 | set -e fifc_commandline 23 | rm $_fifc_complist_path 24 | -------------------------------------------------------------------------------- /functions/_fifc_file_type.fish: -------------------------------------------------------------------------------- 1 | function _fifc_file_type -d "Figure out file type (txt, json, image, pdf, archive, binary)" 2 | set -l mime (file --mime-type -b "$argv") 3 | set -l binary 0 4 | if string match --quiet '*binary*' -- (file --mime -b -L "$argv") 5 | set binary 1 6 | end 7 | 8 | switch $mime 9 | case application/{gzip,java-archive,x-{7z-compressed,bzip2,chrome-extension,rar,tar,xar},zip} 10 | echo archive 11 | return 12 | case "image/*" 13 | echo image 14 | return 15 | end 16 | 17 | if test $binary = 1 18 | echo binary 19 | else 20 | switch $mime 21 | case application/pdf 22 | echo pdf 23 | case application/json 24 | echo json 25 | case "*" 26 | echo txt 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /functions/_fifc_open_file.fish: -------------------------------------------------------------------------------- 1 | function _fifc_open_file -d "Open a file with the right tool depending on its type" 2 | set -l filepath "$fifc_candidate" 3 | 4 | if test -n "$argv" 5 | set filepath "$argv" 6 | end 7 | 8 | set -q fifc_editor || set -l fifc_editor "$EDITOR" 9 | 10 | set -l file_type (_fifc_file_type "$filepath") 11 | 12 | switch $file_type 13 | case txt json archive 14 | $fifc_editor "$filepath" 15 | case image 16 | if type -q chafa 17 | chafa --watch $fifc_chafa_opts "$filepath" 18 | else 19 | $fifc_editor "$filepath" 20 | end 21 | case binary 22 | if type -q hexyl 23 | hexyl $fifc_hexyl_opts "$filepath" | less --RAW-CONTROL-CHARS 24 | else 25 | $fifc_editor "$filepath" 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /tests/test_source.fish: -------------------------------------------------------------------------------- 1 | set curr_fifc_unordered_comp $_fifc_unordered_comp 2 | set curr_fifc_ordered_comp $_fifc_ordered_comp 3 | set _fifc_complist_path (mktemp) 4 | 5 | # Add unordered sources 6 | set comp_1 'test "$any" = "1"' '^kill $' '' '' 'echo comp_1' 7 | set comp_2 'test "$any" = "2"' '^ps -p $' '' '' 'echo comp_2' 8 | set _fifc_unordered_comp comp_1 comp_2 9 | 10 | set fifc_commandline "kill " 11 | set any 1 12 | set actual (_fifc_action "source") 13 | @test "source match first" "$actual" = "echo comp_1" 14 | 15 | set fifc_commandline "ps -p " 16 | set any 2 17 | set actual (_fifc_action "source") 18 | @test "source match second" "$actual" = "echo comp_2" 19 | 20 | set fifc_commandline "foo " 21 | set actual (_fifc_action "source") 22 | @test "source fallback fish suggestions" "$actual" = _fifc_parse_complist 23 | 24 | set -e fifc_commandline 25 | set -gx _fifc_unordered_comp $curr_fifc_unordered_comp 26 | set -gx _fifc_unordered_comp $curr_fifc_ordered_comp 27 | rm $_fifc_complist_path 28 | -------------------------------------------------------------------------------- /tests/test_file_type.fish: -------------------------------------------------------------------------------- 1 | set actual (_fifc_file_type "tests/_resources/dir with spaces/file 71.txt") 2 | @test "file type test txt" "$actual" = txt 3 | 4 | set actual (_fifc_file_type "tests/_resources/dir with spaces/file.json") 5 | @test "file type test json" "$actual" = json -o "$actual" = txt 6 | 7 | set actual (_fifc_file_type "tests/_resources/dir with spaces/fish.png") 8 | @test "file type test image" "$actual" = image 9 | 10 | set actual (_fifc_file_type "tests/_resources/dir with spaces/file.bin") 11 | @test "file type test binary" "$actual" = binary 12 | 13 | set actual (_fifc_file_type "tests/_resources/dir with spaces/file.7z") 14 | @test "file type test archive" "$actual" = archive 15 | 16 | set actual (_fifc_file_type "tests/_resources/dir with spaces/file.pdf") 17 | @test "file type test pdf" "$actual" = pdf 18 | 19 | set actual_target (_fifc_file_type "tests/_resources/target.txt") 20 | @test "file type test link target" "$actual_target" = txt 21 | 22 | set actual_link (_fifc_file_type "tests/_resources/dir with spaces/symlink.txt") 23 | @test "file type test link source" "$actual_link" = "$actual_target" 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Matthieu MN 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /functions/_fifc_completion_group.fish: -------------------------------------------------------------------------------- 1 | function _fifc_completion_group -d "Determine completion group" 2 | set -l path_candidate (_fifc_path_to_complete) 3 | # Null means that either $path is empty or is not a directory 4 | set -l is_null (ls -A -- $path_candidate 2> /dev/null | string collect) 5 | set -l complist (string escape -- (_fifc_expand_tilde (_fifc_parse_complist))) 6 | # Directories 7 | set -l dir_test "test -d $complist[1]" 8 | set dir_test (string join -- " -a -d " $dir_test $complist[2..-1]) 9 | if test -n "$is_null"; and eval "$dir_test" 10 | echo directories 11 | # Files 12 | # When complist is big, avoid calling ls with all arguments if first is neither a file nor a directory 13 | else if test -n "$is_null"; and echo $complist | xargs ls -d -- &>/dev/null 14 | echo files 15 | # Options 16 | else if string match --regex --quiet -- '\h+\-+\h*$' $fifc_commandline 17 | set -e fifc_query 18 | echo options 19 | # PIDs 20 | else if string join -- '' $complist | string match --regex --quiet '^[0-9]+$' 21 | echo processes 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /tests/test_fifc.fish: -------------------------------------------------------------------------------- 1 | set curr_fifc_unordered_comp $_fifc_unordered_comp 2 | set _fifc_unordered_comp 3 | 4 | fifc \ 5 | -n 'test -n "$fifc_desc"' \ 6 | -r '^functions\h+|^\h+' \ 7 | -p _fifc_preview_fn \ 8 | -o _fifc_open_fn 9 | 10 | @test "fifc command completion added" (count $_fifc_unordered_comp) = 1 11 | @test "fifc command completion condition" "$$_fifc_unordered_comp[1][1]" = 'test -n "$fifc_desc"' 12 | @test "fifc command completion regex" "$$_fifc_unordered_comp[1][2]" = '^functions\h+|^\h+' 13 | @test "fifc command completion preview" "$$_fifc_unordered_comp[1][3]" = _fifc_preview_fn 14 | @test "fifc command completion open" "$$_fifc_unordered_comp[1][4]" = _fifc_open_fn 15 | 16 | # set _fifc_unordered_comp 17 | fifc \ 18 | -n 'test -n "$fifc_desc"' \ 19 | -r '^functions\h+|^\h+' \ 20 | -s source_cmd 21 | 22 | @test "fifc source added" (count $_fifc_unordered_comp) = 2 23 | @test "fifc source condition" "$$_fifc_unordered_comp[2][1]" = 'test -n "$fifc_desc"' 24 | @test "fifc source regex" "$$_fifc_unordered_comp[2][2]" = '^functions\h+|^\h+' 25 | @test "fifc source command" "$$_fifc_unordered_comp[2][5]" = source_cmd 26 | 27 | set -gx _fifc_unordered_comp $curr_fifc_unordered_comp 28 | -------------------------------------------------------------------------------- /tests/test_exposed_vars.fish: -------------------------------------------------------------------------------- 1 | set curr_fifc_unordered_comp $_fifc_unordered_comp 2 | set dir "tests/_resources/dir with spaces" 3 | set _fifc_complist_path (mktemp) 4 | 5 | function _fifc_test_exposed_vars 6 | switch $var 7 | case candidate 8 | echo -n "$fifc_candidate" 9 | case extracted 10 | echo -n "$fifc_extracted" 11 | case query 12 | echo -n "$fifc_query" 13 | end 14 | end 15 | 16 | 17 | set comp_1 \ 18 | 'test -f $fifc_candidate' \ 19 | '.*' \ 20 | _fifc_test_exposed_vars \ 21 | open_cmd \ 22 | source_cmd \ 23 | --fzf_option \ 24 | '.*/(.*\.txt)$' 25 | 26 | set _fifc_unordered_comp comp_1 27 | 28 | set var candidate 29 | set actual (_fifc_action "preview" "$dir/file 1.txt") 30 | @test "exposed vars fifc_candidate" "$actual" = "$dir/file 1.txt" 31 | 32 | set var extracted 33 | set -x fifc_extracted 34 | set _fifc_extract_regex '.*/(.*\.txt)$' 35 | set actual (_fifc_action "preview" "$dir/file 1.txt") 36 | @test "exposed vars fifc_extracted" "$actual" = "file 1.txt" 37 | 38 | set var query 39 | set -x fifc_query 40 | set actual (_fifc_action "preview" "$dir/file 1.txt" "1") 41 | @test "exposed vars fifc_query" "$actual" = 1 42 | 43 | rm $_fifc_complist_path 44 | -------------------------------------------------------------------------------- /tests/version_test.fish: -------------------------------------------------------------------------------- 1 | set -l actual (_fifc_test_version "3.4" -gt "3.0") 2 | @test "version test basic gt" $status = 0 3 | 4 | set -l actual (_fifc_test_version "3.4" -ge "3.4") 5 | @test "version test basic ge equal" $status = 0 6 | 7 | set -l actual (_fifc_test_version "3.5" -ge "3.4") 8 | @test "version test basic ge greater" $status = 0 9 | 10 | set -l actual (_fifc_test_version "3.4" -lt "3.5") 11 | @test "version test basic lt" $status = 0 12 | 13 | set -l actual (_fifc_test_version "3.4" -le "3.4") 14 | @test "version test basic le equal" $status = 0 15 | 16 | set -l actual (_fifc_test_version "3.4" -le "3.5") 17 | @test "version test basic le lower" $status = 0 18 | 19 | set -l actual (_fifc_test_version "3.4" -gt "3") 20 | @test "version test length not equal" $status = 0 21 | 22 | set -l actual (_fifc_test_version "3.4" -gt "3") 23 | @test "version test length not equal 1" $status = 0 24 | 25 | set -l actual (_fifc_test_version "3" -gt "3.4") 26 | @test "version test length not equal 2" $status = 1 27 | 28 | set -l actual (_fifc_test_version "fish 3.5.0" -gt "3.4.2") 29 | @test "version test extract version left" $status = 0 30 | 31 | set -l actual (_fifc_test_version "3.5.0" -gt "fish 3.4.2") 32 | @test "version test extract version right" $status = 0 33 | -------------------------------------------------------------------------------- /functions/_fifc_source_files.fish: -------------------------------------------------------------------------------- 1 | function _fifc_source_files -d "Return a command to recursively find files" 2 | set -l path (_fifc_path_to_complete | string escape) 3 | set -l hidden (string match "*." "$path") 4 | 5 | if string match --quiet -- '~*' "$fifc_query" 6 | set -e fifc_query 7 | end 8 | 9 | if type -q fd 10 | if _fifc_test_version (fd --version) -ge "8.3.0" 11 | set fd_custom_opts --strip-cwd-prefix 12 | end 13 | 14 | if test "$path" = {$PWD}/ 15 | echo "fd . $fifc_fd_opts --color=always $fd_custom_opts" 16 | else if test "$path" = "." 17 | echo "fd . $fifc_fd_opts --color=always --hidden $fd_custom_opts" 18 | else if test -n "$hidden" 19 | echo "fd . $fifc_fd_opts --color=always --hidden -- $path" 20 | else 21 | echo "fd . $fifc_fd_opts --color=always -- $path" 22 | end 23 | else if test -n "$hidden" 24 | # Use sed to strip cwd prefix 25 | echo "find . $path $fifc_find_opts ! -path . -print 2>/dev/null | sed 's|^\./||'" 26 | else 27 | # Exclude hidden directories 28 | echo "find . $path $fifc_find_opts ! -path . ! -path '*/.*' -print 2>/dev/null | sed 's|^\./||'" 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /functions/_fifc_preview_opt.fish: -------------------------------------------------------------------------------- 1 | function _fifc_preview_opt -d "Open man page of a command starting at the selected option" 2 | set -l regex "(?s)^(\-+[^\n]+)*$fifc_candidate([^\-\w\.]([^\.\n]|\.{2,}|\w+\.)*|)\n{1,2}.*?(^(\-+[^\n]+|\w+))" 3 | set -l regex_replace '^\h+(\-+[^\n]+.*)' 4 | set -l cmd (string match --regex --groups-only -- '(^|\h+)(\w+) ?-*$' $fifc_commandline) 5 | echo $group 6 | set out (man $cmd 2>/dev/null | string replace -r $regex_replace '$1' \ 7 | | begin 8 | if type -q rg 9 | rg --multiline $regex 10 | else if type -q pcre2grep 11 | pcre2grep --multiline $regex 12 | else 13 | pcregrep --multiline $regex 14 | end 15 | end \ 16 | # Remove last line as it should describes the next option 17 | | awk 'n>=1 { print a[n%1] } { a[n%1]=$0; n=n+1 }' \ 18 | | string trim \ 19 | ) 20 | 21 | # Fallback to fish description if there is no man page 22 | if test -z "$out" 23 | echo "$fifc_desc" 24 | end 25 | 26 | # Pretty printing 27 | set_color brgreen 28 | echo -e "$out[1]" 29 | set_color --bold white 30 | echo -e (string join -- "\n" "" $out[2..-1]) 31 | set_color normal 32 | end 33 | -------------------------------------------------------------------------------- /functions/_fifc_preview_file.fish: -------------------------------------------------------------------------------- 1 | function _fifc_preview_file -d "Preview the selected file with the right tool depending on its type" 2 | set -l file_type (_fifc_file_type "$fifc_candidate") 3 | 4 | switch $file_type 5 | case txt 6 | if type -q bat 7 | bat --color=always $fifc_bat_opts "$fifc_candidate" 8 | else 9 | cat "$fifc_candidate" 10 | end 11 | case json 12 | if type -q bat 13 | bat --color=always -l json $fifc_bat_opts "$fifc_candidate" 14 | else 15 | cat "$fifc_candidate" 16 | end 17 | case image pdf 18 | if type -q chafa 19 | chafa $fifc_chafa_opts "$fifc_candidate" 20 | else 21 | _fifc_preview_file_default "$fifc_candidate" 22 | end 23 | case archive 24 | if type -q 7z 25 | 7z l ""$fifc_candidate"" | tail -n +17 | awk '{ print $6 }' 26 | else 27 | _fifc_preview_file_default "$fifc_candidate" 28 | end 29 | case binary 30 | if type -q hexyl 31 | hexyl $fifc_hexyl_opts "$fifc_candidate" 32 | else 33 | _fifc_preview_file_default "$fifc_candidate" 34 | end 35 | 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /tests/test_match_order.fish: -------------------------------------------------------------------------------- 1 | set curr_fifc_unordered_comp $_fifc_unordered_comp 2 | set curr_fifc_ordered_comp $_fifc_ordered_comp 3 | set dir "tests/_resources/dir with spaces" 4 | set _fifc_complist_path (mktemp) 5 | 6 | # Add unordered completions 7 | set comp_1 \ 8 | 'test -f $fifc_candidate' \ 9 | '^cat $' \ 10 | 'echo comp_1' \ 11 | open_cmd \ 12 | source_cmd \ 13 | --fzf_option \ 14 | extract_regex 15 | 16 | set comp_2 \ 17 | 'test -d $fifc_candidate' \ 18 | '^ls $' \ 19 | 'echo comp_2' \ 20 | open_cmd \ 21 | source_cmd \ 22 | --fzf_option \ 23 | extract_regex 24 | 25 | set _fifc_unordered_comp comp_1 comp_2 26 | 27 | set fifc_commandline "cat " 28 | set actual (_fifc_action "preview" "$dir/file 1.txt") 29 | @test "preview match condition and regex first completion" "$actual" = comp_1 30 | 31 | set fifc_commandline "ls " 32 | set actual (_fifc_action "preview" "$dir") 33 | @test "preview match condition and regex second completion" "$actual" = comp_2 34 | 35 | echo "fallback description" >$_fifc_complist_path 36 | set fifc_commandline "fallback " 37 | set actual (_fifc_action "preview" 'fallback') 38 | @test "preview fallback fish description" "$actual" = description 39 | 40 | # Add ordered completion, should be evaluated before unordered ones 41 | set o_comp_1 \ 42 | 'test -f $fifc_candidate' \ 43 | '^cat $' \ 44 | 'echo o_comp_1' \ 45 | open_cmd \ 46 | source_cmd_1 \ 47 | --fzf_option_1 \ 48 | extract_regex_1 49 | 50 | set _fifc_ordered_comp o_comp_1 51 | set fifc_commandline "cat " 52 | set actual (_fifc_action "preview" "$dir/file 1.txt") 53 | @test "preview match condition and regex ordered completion" "$actual" = o_comp_1 54 | 55 | set -e fifc_commandline 56 | set -gx _fifc_unordered_comp $curr_fifc_unordered_comp 57 | set -gx _fifc_ordered_comp $curr_fifc_ordered_comp 58 | rm $_fifc_complist_path 59 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | 8 | # Allows you to run this workflow manually from the Actions tab 9 | workflow_dispatch: 10 | 11 | jobs: 12 | tests: 13 | runs-on: ${{ matrix.os }} 14 | strategy: 15 | matrix: 16 | os: [macos-latest, ubuntu-latest] 17 | 18 | steps: 19 | - uses: actions/checkout@v3 20 | - uses: fish-actions/install-fish@v1.1.0 21 | 22 | - name: Install abbreviation-tips with Fisher 23 | uses: fish-shop/install-plugin@v1 24 | with: 25 | plugin-manager: fisher 26 | plugins: gazorby/fifc 27 | 28 | - name: Set up Homebrew 29 | uses: Homebrew/actions/setup-homebrew@master 30 | # Homebrew is only missing from ubuntu images per https://github.com/actions/runner-images/issues/6283 31 | if: matrix.os == 'ubuntu-latest' 32 | 33 | - name: Install fzf and fd 34 | run: brew install fzf fd 35 | 36 | - name: Run Fishtape tests 37 | uses: fish-shop/run-fishtape-tests@v1 38 | with: 39 | pattern: tests/**.fish 40 | # timeout in case tests get stuck on fzf 41 | timeout-minutes: 3 42 | 43 | syntax-check: 44 | runs-on: ubuntu-latest 45 | 46 | steps: 47 | - uses: actions/checkout@v3 48 | 49 | - uses: fish-actions/install-fish@v1 50 | 51 | - uses: fish-actions/syntax-check@v1 52 | 53 | # check Fish format 54 | format-check: 55 | runs-on: ubuntu-latest 56 | 57 | steps: 58 | - uses: actions/checkout@v3 59 | 60 | - uses: fish-actions/install-fish@v1 61 | 62 | - uses: fish-actions/format-check@v1 63 | 64 | # check Markdown and Yaml format 65 | prettier: 66 | runs-on: ubuntu-latest 67 | steps: 68 | - uses: actions/checkout@v3 69 | - uses: actionsx/prettier@v2 70 | with: 71 | args: --check . 72 | -------------------------------------------------------------------------------- /conf.d/fifc.fish: -------------------------------------------------------------------------------- 1 | # Private 2 | set -gx _fifc_comp_count 0 3 | set -gx _fifc_unordered_comp 4 | set -gx _fifc_ordered_comp 5 | 6 | if status is-interactive 7 | # Keybindings 8 | set -qU fifc_keybinding 9 | or set -U fifc_keybinding \t 10 | 11 | set -qU fifc_open_keybinding 12 | or set -U fifc_open_keybinding ctrl-o 13 | 14 | for mode in default insert 15 | bind --mode $mode \t _fifc 16 | bind --mode $mode $fifc_keybinding _fifc 17 | end 18 | 19 | # Set sources rules 20 | fifc \ 21 | -n 'test "$fifc_group" = "directories"' \ 22 | -s _fifc_source_directories 23 | fifc \ 24 | -n 'test "$fifc_group" = "files"' \ 25 | -s _fifc_source_files 26 | fifc \ 27 | -n 'test "$fifc_group" = processes' \ 28 | -s 'ps -ax -o pid=,command=' 29 | end 30 | 31 | # Load fifc preview rules only when fish is launched fzf 32 | if set -q _fifc_launched_by_fzf 33 | # Builtin preview/open commands 34 | fifc \ 35 | -n 'test "$fifc_group" = "options"' \ 36 | -p _fifc_preview_opt \ 37 | -o _fifc_open_opt 38 | fifc \ 39 | -n 'test \( -n "$fifc_desc" -o -z "$fifc_commandline" \); and type -q -f -- "$fifc_candidate"' \ 40 | -r '^(?!\\w+\\h+)' \ 41 | -p _fifc_preview_cmd \ 42 | -o _fifc_open_cmd 43 | fifc \ 44 | -n 'test -n "$fifc_desc" -o -z "$fifc_commandline"' \ 45 | -r '^(functions)?\\h+' \ 46 | -p _fifc_preview_fn \ 47 | -o _fifc_open_fn 48 | fifc \ 49 | -n 'test -f "$fifc_candidate"' \ 50 | -p _fifc_preview_file \ 51 | -o _fifc_open_file 52 | fifc \ 53 | -n 'test -d "$fifc_candidate"' \ 54 | -p _fifc_preview_dir \ 55 | -o _fifc_open_dir 56 | fifc \ 57 | -n 'test "$fifc_group" = processes -a (ps -p (_fifc_parse_pid "$fifc_candidate") &>/dev/null)' \ 58 | -p _fifc_preview_process \ 59 | -o _fifc_open_process \ 60 | -e '^\\h*([0-9]+)' 61 | end 62 | 63 | 64 | # Fisher 65 | function _fifc_uninstall --on-event fifc_uninstall 66 | end 67 | -------------------------------------------------------------------------------- /functions/fifc.fish: -------------------------------------------------------------------------------- 1 | function __fifc_check_flag -d "Check flag value for fifc" 2 | switch $_flag_name 3 | case O order 4 | if not test 0 -lt $_flag_value 5 | echo "$_argparse_cmd: Order must be a positive integer" 6 | return 1 7 | end 8 | # Ensure regex is valid 9 | case r regex 10 | set -l out (string match --regex --quiet $_flag_value 2>&1 | string join '\n') 11 | if test -n "$out" 12 | echo -e "$_argparse_cmd:\n$out" 13 | end 14 | end 15 | end 16 | 17 | function fifc -d "Add your own fish fzf completions" 18 | set -l option_spec 'n/condition=' 'p/preview=' 'o/open=' 's/source=' 'e/extract=' 'f/fzf-options=' h/help 19 | set -a option_spec 'r/regex=!__fifc_check_flag' 'O/order=!__fifc_check_flag' 20 | 21 | argparse --name fifc $option_spec -- $argv 22 | 23 | if test "$status" != 0 24 | return 1 25 | end 26 | 27 | if test -n "$_flag_h" 28 | _fifc_help 29 | return 30 | end 31 | 32 | if test \( -n "$_flag_n" -o -n "$_flag_r" \) \ 33 | -a \( -z "$_flag_p" -a -z "$_flag_o" -a -z "$_flag_s" -a -z "$_flag_e" -a -z "$_flag_f" \) 34 | 35 | echo "fifc: You have not specified any binding (preview, open, source or extract)" 36 | return 1 37 | end 38 | 39 | set _fifc_comp_count (math $_fifc_comp_count + 1) 40 | set -l count $_fifc_comp_count 41 | # Ensure completion vars are empty before setting them 42 | set -e "_fifc_comp_$count" 43 | set -gx "_fifc_comp_$count" 44 | set -a "_fifc_comp_$count" "$_flag_n" 45 | set -a "_fifc_comp_$count" "$_flag_r" 46 | set -a "_fifc_comp_$count" "$_flag_p" 47 | set -a "_fifc_comp_$count" "$_flag_o" 48 | set -a "_fifc_comp_$count" "$_flag_s" 49 | set -a "_fifc_comp_$count" "$_flag_f" 50 | set -a "_fifc_comp_$count" "$_flag_e" 51 | 52 | if test -z "$_flag_O" 53 | set -a _fifc_unordered_comp "_fifc_comp_$count" 54 | else 55 | set _fifc_ordered_comp[$_flag_O] "_fifc_comp_$count" 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /functions/_fifc_action.fish: -------------------------------------------------------------------------------- 1 | function _fifc_action 2 | # Can be either "preview", "open" or "source" 3 | set -l action $argv[1] 4 | set -l comp $_fifc_ordered_comp $_fifc_unordered_comp 5 | set -l regex_val (string escape --style=regex -- "$argv[2]") 6 | # Escape '/' for sed processing 7 | set regex_val (string replace '/' '\/' --all "$regex_val") 8 | 9 | # Variables exposed to evaluated commands 10 | set -x fifc_desc (sed -nr (printf 's/^%s[[:blank:]]+(.*)/\\\1/p' "$regex_val") $_fifc_complist_path | string trim) 11 | set -x fifc_candidate "$argv[2]" 12 | set -x fifc_extracted (string match --regex --groups-only -- "$_fifc_extract_regex" "$argv[2]") 13 | 14 | if test "$action" = preview 15 | set default_preview 1 16 | set fifc_query "$argv[3]" 17 | 18 | else if test "$action" = open 19 | set fifc_query "$argv[3]" 20 | 21 | else if test "$action" = source 22 | set default_source 1 23 | end 24 | 25 | for i in (seq (count $comp)) 26 | set -l condition_cmd 27 | set -l regex_cmd 28 | set -l valid 1 29 | if test -n "$$comp[$i][1]" 30 | set condition_cmd "$$comp[$i][1]" 31 | else 32 | set condition_cmd true 33 | end 34 | if test -n "$$comp[$i][2]" 35 | set -l val (string escape -- "$fifc_commandline") 36 | set regex_cmd "string match --regex --quiet -- '$$comp[$i][2]' $val" 37 | else 38 | set regex_cmd true 39 | end 40 | 41 | if not eval "$condition_cmd; and $regex_cmd" 42 | set valid 0 43 | continue 44 | end 45 | 46 | set _fifc_extract_regex "$$comp[$i][7]" 47 | 48 | if test "$action" = preview; and test -n "$$comp[$i][3]" 49 | eval $$comp[$i][3] 50 | set default_preview 0 51 | break 52 | else if test "$action" = open; and test -n "$$comp[$i][4]" 53 | eval $$comp[$i][4] 54 | break 55 | else if test "$action" = source; and test -n "$$comp[$i][5]" 56 | set _fifc_custom_fzf_opts "$$comp[$i][6]" 57 | if functions "$$comp[$i][5]" 1>/dev/null 58 | eval $$comp[$i][5] 59 | else 60 | echo $$comp[$i][5] 61 | end 62 | set default_source 0 63 | break 64 | end 65 | end 66 | 67 | # We are in preview mode, but nothing matched 68 | # fallback to fish description 69 | if test "$default_preview" = 1 70 | echo "$fifc_desc" 71 | else if test "$default_source" = 1 72 | echo _fifc_parse_complist 73 | end 74 | end 75 | -------------------------------------------------------------------------------- /functions/_fifc.fish: -------------------------------------------------------------------------------- 1 | function _fifc 2 | set -f --export SHELL (command --search fish) 3 | set -l result 4 | set -Ux _fifc_extract_regex 5 | set -gx _fifc_complist_path (string join '' (mktemp) "_fifc") 6 | set -gx _fifc_custom_fzf_opts 7 | set -gx fifc_extracted 8 | set -gx fifc_commandline 9 | set -gx fifc_token (commandline --current-token) 10 | set -gx fifc_query "$fifc_token" 11 | 12 | # Get commandline buffer 13 | if test "$argv" = "" 14 | set fifc_commandline (commandline --cut-at-cursor) 15 | else 16 | set fifc_commandline $argv 17 | end 18 | 19 | if _fifc_test_version "$FISH_VERSION" -ge "3.4" 20 | set complete_opts --escape 21 | end 22 | 23 | complete -C $complete_opts -- "$fifc_commandline" | string split '\n' >$_fifc_complist_path 24 | 25 | set -gx fifc_group (_fifc_completion_group) 26 | set source_cmd (_fifc_action source) 27 | 28 | set fifc_fzf_query (string trim --chars '\'' -- "$fifc_fzf_query") 29 | 30 | set -l fzf_cmd " 31 | _fifc_launched_by_fzf=1 SHELL=fish fzf \ 32 | -d \t \ 33 | --exact \ 34 | --tiebreak=length \ 35 | --select-1 \ 36 | --exit-0 \ 37 | --ansi \ 38 | --tabstop=4 \ 39 | --multi \ 40 | --reverse \ 41 | --header '$header' \ 42 | --preview '_fifc_action preview {} {q}' \ 43 | --bind='$fifc_open_keybinding:execute(_fifc_action open {} {q} &> /dev/tty)' \ 44 | --query '$fifc_query' \ 45 | $_fifc_custom_fzf_opts" 46 | 47 | set -l cmd (string join -- " | " $source_cmd $fzf_cmd) 48 | # We use eval hack because wrapping source command 49 | # inside a function cause some delay before fzf to show up 50 | eval $cmd | while read -l token 51 | # don't escape '~' for path, `$` for environ 52 | if string match --quiet '~*' -- $token 53 | set -a result (string join -- "" "~" (string sub --start 2 -- $token | string escape)) 54 | else if string match --quiet '$*' -- $token 55 | set -a result (string join -- "" "\$" (string sub --start 2 -- $token | string escape)) 56 | else 57 | set -a result (string escape --no-quoted -- $token) 58 | end 59 | # Perform extraction if needed 60 | if test -n "$_fifc_extract_regex" 61 | set result[-1] (string match --regex --groups-only -- "$_fifc_extract_regex" "$token") 62 | end 63 | end 64 | 65 | # Add space trailing space only if: 66 | # - there is no trailing space already present 67 | # - Result is not a directory 68 | # We need to unescape $result for directory test as we escaped it before 69 | if test (count $result) -eq 1; and not test -d (string unescape -- $result[1]) 70 | set -l buffer (string split -- "$fifc_commandline" (commandline -b)) 71 | if not string match -- ' *' "$buffer[2]" 72 | set -a result '' 73 | end 74 | end 75 | 76 | if test -n "$result" 77 | commandline --replace --current-token -- (string join -- ' ' $result) 78 | end 79 | 80 | commandline --function repaint 81 | 82 | rm $_fifc_complist_path 83 | # Clean state 84 | set -e _fifc_extract_regex 85 | set -e _fifc_custom_fzf_opts 86 | set -e _fifc_complist_path 87 | set -e fifc_token 88 | set -e fifc_group 89 | set -e fifc_extracted 90 | set -e fifc_candidate 91 | set -e fifc_commandline 92 | set -e fifc_query 93 | end 94 | -------------------------------------------------------------------------------- /functions/_fifc_help.fish: -------------------------------------------------------------------------------- 1 | function __fifc_help_print 2 | argparse -s "i/indentation=?" "l/level=?" "c/color=?" e/escape n -- $argv 3 | set -l spaces "$_flag_i" 4 | set -l level "$_flag_l" 5 | set -l color "$_flag_c" 6 | set -l echo_opt 7 | 8 | if test -z "$spaces" 9 | set spaces 4 10 | end 11 | if test -z "$level" 12 | set level 1 13 | end 14 | if test -n "$_flag_e" 15 | set -a echo_opt -e 16 | end 17 | if test -n "$_flag_n" 18 | set -a echo_opt -n 19 | end 20 | if test -z "$_flag_c" 21 | set color -o white 22 | end 23 | set indent (string repeat --count (math $spaces x $level) " ") 24 | 25 | set_color $color 26 | echo $echo_opt "$indent$argv[1]" 27 | for line in $argv[2..-1] 28 | set_color $color 29 | echo $echo_opt "\n$indent$line" 30 | end 31 | # end 32 | set_color normal 33 | end 34 | 35 | function __fifc_help_section 36 | __fifc_help_print -e -l0 --color=yellow (string join -- "" (string upper -- "$argv") "\n") 37 | end 38 | 39 | function __fifc_help_opt 40 | set -l opt (string split -- '=' $argv[1]) 41 | __fifc_help_print -n -e --color=green -l1 -- "$opt[1]" 42 | if test (count $opt) -eq 2 43 | set_color yellow 44 | echo "=$opt[2]" 45 | else 46 | echo "" 47 | end 48 | set -l desc (string split -- '\n' $argv[2..-1] | string trim) 49 | __fifc_help_print -e -l2 -- $desc 50 | echo "" 51 | end 52 | 53 | function _fifc_help -d "Print fifc help message" 54 | __fifc_help_section NAME 55 | __fifc_help_print -e "fifc - Set custom completion rules for use with fifc fish plugin\n" 56 | 57 | __fifc_help_section SYNOPSIS 58 | __fifc_help_print -e "fifc [OPTIONS]\n" 59 | 60 | __fifc_help_section DESCRIPTION 61 | 62 | __fifc_help_print -e -n \ 63 | "The fifc command allows you to add custom completion rules that can enhance fish completions or override them.\n" \ 64 | "A rule is composed of condition(s) that, if valid, trigger commands that can:" \ 65 | " - Change fzf preview (-p)" \ 66 | " - Feed fzf input (-s)" \ 67 | " - Execute when fifc_open_keybinding is pressed (defaults to ctrl-o) (-o)" 68 | 69 | __fifc_help_print -e "\n\n" 70 | 71 | __fifc_help_print -e -n \ 72 | "A condition can be either:" \ 73 | " - A regex that must match commandline before cursor position (-r)" \ 74 | " - An arbitrary command that must exit with a non-zero status (-n)" 75 | 76 | __fifc_help_print -e "\n" 77 | 78 | __fifc_help_print -e -n \ 79 | "Rule are evaluated in the order in which they are defined," \ 80 | "and fifc will stop at the first rule where all conditions are met" 81 | 82 | __fifc_help_print -e "\n\n" 83 | 84 | __fifc_help_opt \ 85 | "-r, --regex=REGEX" \ 86 | "Regex that must match commandline preceding the cursor for the rule to be valid" 87 | 88 | __fifc_help_opt \ 89 | "-e, --extract=COMMAND" \ 90 | "Regex used to extract string from selected results before appending them to the commandline" 91 | 92 | __fifc_help_opt \ 93 | "-n, --condition=COMMAND" \ 94 | "Command or function that must exit with a non-zero status for the rule to be valid" 95 | 96 | __fifc_help_opt \ 97 | "-p, --preview=COMMAND" \ 98 | "Preview command passed to fzf if the rule is valid" 99 | 100 | __fifc_help_opt \ 101 | "-s, --source=COMMAND" \ 102 | "Command that will feed fzf input if the rule is valid" 103 | 104 | __fifc_help_opt \ 105 | "-o, --open=COMMAND" \ 106 | "Command binded to fifc_open_keybinding (defaults to ctrl-o) when using fzf" 107 | 108 | __fifc_help_opt \ 109 | "-O, --order=INT" \ 110 | "The order in which the rule is evaluated." \ 111 | "If missing, the rule will be evaluated after all ordered ones, and all unordered rules defined before." 112 | 113 | __fifc_help_opt \ 114 | "-f, --fzf-options" \ 115 | "Custom fzf options (can override previous ones)" 116 | 117 | __fifc_help_opt \ 118 | "-h, --help" \ 119 | "Show this help" 120 | 121 | __fifc_help_print -e "Examples:\n" 122 | 123 | __fifc_help_print -e -l2 -- "- Preview files using bat (already builtin):\n" 124 | __fifc_help_print -e -l2 --color=white -- \ 125 | ' fifc -n \'test -f "$fifc_candidate"\' -p "bat --color=always $fifc_candidate"' 126 | 127 | __fifc_help_print -e "\n" 128 | 129 | __fifc_help_print -e -l2 -- "- Use fd to search files recursively (already builtin):\n" 130 | __fifc_help_print -e -l2 --color=white -- \ 131 | ' fifc -n \'test "$fifc_group" = files\' -s \'fd . --color=always --strip-cwd-prefix\'' 132 | 133 | __fifc_help_print -e "\n" 134 | 135 | __fifc_help_print -e -l2 -- "- Interactively search packages on archlinux:\n" 136 | __fifc_help_print -e -n -l2 --color=white -- \ 137 | " fifc \\" \ 138 | " -r '^pacman(\h*\-S)?\h+\w+' \\" \ 139 | " -s 'pacman --color=always -Ss "\$fifc_token" | string match -r \"^[^\h+].*\"' \\" \ 140 | " -e '.*/(.*?)\h.*' \\" \ 141 | " -f '--query \"\"' \\" \ 142 | " -p 'pacman -Si "\$fifc_extracted"' \\ \n" 143 | end 144 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |