From 5e7c9d23c9790afdeaa32a1074739a456d09e368 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Sun, 21 Apr 2024 17:51:30 +0000 Subject: [PATCH 1/5] - Add fuzzy bash completions --- fuzzycd | 13 +++++++++++++ test/approvals/cd_c | 8 ++++++++ test/approvals/cd_h | 1 + test/approve | 4 ++++ 4 files changed, 26 insertions(+) create mode 100644 test/approvals/cd_c diff --git a/fuzzycd b/fuzzycd index fc62e10..c25619e 100644 --- a/fuzzycd +++ b/fuzzycd @@ -107,6 +107,7 @@ fuzzycd_run() { echo " cd -e edit history file" echo " cd -s show history file" echo " cd -d [DIR] delete current or specified directory from history" + echo " cd -c show completions function [usage: eval \"\$(cd -c)\"]" echo " cd -v show version" echo " cd -h show this help" echo "" @@ -125,6 +126,17 @@ fuzzycd_run() { echo " Delete selected directory from history" } + _fzcd_show_completions() { + echo '_fuzzycd_completions() {' + echo ' local cur=${COMP_WORDS[COMP_CWORD]}' + echo ' local histfile=${FUZZYCD_HISTORY_FILE:-"$HOME/.fuzzycd-history"}' + echo ' _cd' # invoke original completions + echo ' [[ $cur =~ ^(/|\.) ]] && return' + echo ' COMPREPLY+=( $(fzf --filter "$cur" --exit-0 <"$histfile") )' + echo '}' + echo 'complete -o nosort -F _fuzzycd_completions cd' + } + _fzcd_handle_command() { if _fzcd_unhandled_command "$@"; then _fzcd_chdir "$@" @@ -143,6 +155,7 @@ fuzzycd_run() { "-v") _fzcd_show_version ;; "-e") _fzcd_edit_histfile ;; "-s") _fzcd_show_histfile ;; + "-c") _fzcd_show_completions ;; "-d") shift _fzcd_delete_dir "$@" diff --git a/test/approvals/cd_c b/test/approvals/cd_c new file mode 100644 index 0000000..bd98798 --- /dev/null +++ b/test/approvals/cd_c @@ -0,0 +1,8 @@ +_fuzzycd_completions() { + local cur=${COMP_WORDS[COMP_CWORD]} + local histfile=${FUZZYCD_HISTORY_FILE:-"$HOME/.fuzzycd-history"} + _cd + [[ $cur =~ ^(/|\.) ]] && return + COMPREPLY+=( $(fzf --filter "$cur" --exit-0 <"$histfile") ) +} +complete -o nosort -F _fuzzycd_completions cd diff --git a/test/approvals/cd_h b/test/approvals/cd_h index 02f3766..7f920b3 100644 --- a/test/approvals/cd_h +++ b/test/approvals/cd_h @@ -7,6 +7,7 @@ Usage: cd -e edit history file cd -s show history file cd -d [DIR] delete current or specified directory from history + cd -c show completions function [usage: eval "$(cd -c)"] cd -v show version cd -h show this help diff --git a/test/approve b/test/approve index 4ce86ca..d6c8af5 100755 --- a/test/approve +++ b/test/approve @@ -33,6 +33,10 @@ context "when the shell is interactive" it "shows version" approve "cd -v" + describe "cd -c" + it "shows completions function" + approve "cd -c" + describe "cd DIR" it "adds it to history" cd tmp/one/two > /dev/null From e6cceb11ffbdba48825f11b3787dc35cbd5e29bb Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Sun, 21 Apr 2024 18:02:31 +0000 Subject: [PATCH 2/5] fix shellcheck/shfmt --- fuzzycd | 1 + 1 file changed, 1 insertion(+) diff --git a/fuzzycd b/fuzzycd index c25619e..642a4fb 100644 --- a/fuzzycd +++ b/fuzzycd @@ -126,6 +126,7 @@ fuzzycd_run() { echo " Delete selected directory from history" } + # shellcheck disable=SC2016 _fzcd_show_completions() { echo '_fuzzycd_completions() {' echo ' local cur=${COMP_WORDS[COMP_CWORD]}' From a15c11941ecdf450be2970e2b6450769f543ff2b Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Sun, 21 Apr 2024 19:46:03 +0000 Subject: [PATCH 3/5] limit number of suggestions in bash completions --- fuzzycd | 7 ++++++- test/approvals/cd_c | 3 ++- test/approvals/cd_h | 4 ++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/fuzzycd b/fuzzycd index 642a4fb..8c4046e 100644 --- a/fuzzycd +++ b/fuzzycd @@ -121,6 +121,10 @@ fuzzycd_run() { echo " i = interactive when needed, no preview" echo " p = interactive when needed, with ls preview" echo "" + echo " FUZZYCD_COMPLETIONS_COUNT" + echo " Maximum number of suggestions to show in bash completions" + echo " (default: 10)" + echo "" echo "Interactive Keyboard Bindings:" echo " Del" echo " Delete selected directory from history" @@ -131,9 +135,10 @@ fuzzycd_run() { echo '_fuzzycd_completions() {' echo ' local cur=${COMP_WORDS[COMP_CWORD]}' echo ' local histfile=${FUZZYCD_HISTORY_FILE:-"$HOME/.fuzzycd-history"}' + echo ' local count=${FUZZYCD_COMPLETIONS_COUNT:-10}' echo ' _cd' # invoke original completions echo ' [[ $cur =~ ^(/|\.) ]] && return' - echo ' COMPREPLY+=( $(fzf --filter "$cur" --exit-0 <"$histfile") )' + echo ' COMPREPLY+=( $(fzf --filter "$cur" --exit-0 <"$histfile" | head -n$count) )' echo '}' echo 'complete -o nosort -F _fuzzycd_completions cd' } diff --git a/test/approvals/cd_c b/test/approvals/cd_c index bd98798..e97d345 100644 --- a/test/approvals/cd_c +++ b/test/approvals/cd_c @@ -1,8 +1,9 @@ _fuzzycd_completions() { local cur=${COMP_WORDS[COMP_CWORD]} local histfile=${FUZZYCD_HISTORY_FILE:-"$HOME/.fuzzycd-history"} + local count=${FUZZYCD_COMPLETIONS_COUNT:-10} _cd [[ $cur =~ ^(/|\.) ]] && return - COMPREPLY+=( $(fzf --filter "$cur" --exit-0 <"$histfile") ) + COMPREPLY+=( $(fzf --filter "$cur" --exit-0 <"$histfile" | head -n$count) ) } complete -o nosort -F _fuzzycd_completions cd diff --git a/test/approvals/cd_h b/test/approvals/cd_h index 7f920b3..5171323 100644 --- a/test/approvals/cd_h +++ b/test/approvals/cd_h @@ -21,6 +21,10 @@ Environment Variables: i = interactive when needed, no preview p = interactive when needed, with ls preview + FUZZYCD_COMPLETIONS_COUNT + Maximum number of suggestions to show in bash completions + (default: 10) + Interactive Keyboard Bindings: Del Delete selected directory from history From 95a6f8bb9d5bd98b9156a53df7357440173fb8ee Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Mon, 22 Apr 2024 04:20:31 +0000 Subject: [PATCH 4/5] update readme --- README.md | 28 +++++++++++++++++++++++----- op.conf | 1 + 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 56912d8..9c7c622 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ instantly using a fuzzy match, with or without an interactive menu. - Minimal - cd to best match - Interactive - Interactive with `ls` preview +- Optional fuzzy bash completions. ## Prerequisites @@ -61,9 +62,6 @@ You are encouraged to inspect the [setup script](setup) before running. ``` $ cd -h - - fuzzycd 0.2.3 - Usage: cd DIR change working directory cd SEARCH change working directory or show selection menu @@ -71,6 +69,7 @@ You are encouraged to inspect the [setup script](setup) before running. cd -e edit history file cd -s show history file cd -d [DIR] delete current or specified directory from history + cd -c show completions function [usage: eval "$(cd -c)"] cd -v show version cd -h show this help @@ -84,6 +83,10 @@ You are encouraged to inspect the [setup script](setup) before running. i = interactive when needed, no preview p = interactive when needed, with ls preview + FUZZYCD_COMPLETIONS_COUNT + Maximum number of suggestions to show in bash completions + (default: 10) + Interactive Keyboard Bindings: Del Delete selected directory from history @@ -119,10 +122,25 @@ matching directories when running in interactive mode, or you will `cd` to the best match when running in non-interactive mode (default). +## Bash completions + +To enable fuzzy bash completions, add the following line to your `~/.bashrc`: + +```bash +eval "$(cd -c)" +``` + +This works best when tab completion is configured for inline completions, which +you can set by adding/updating the `~/.inputrc` file: + +```bash +# ~/.inputrc +TAB: menu-complete +``` + ## Uninstall -1. Remove the `source /usr/local/bin/fuzzycd` line from your startup script(s) - (`~/.bashrc` and/or `~/.zshrc`). +1. Remove the `source /usr/local/bin/fuzzycd` line from your `~/.bashrc`. 2. Delete `/usr/local/bin/fuzzycd`. 3. Optionally, delete the history file (`~/.fuzzycd-history`). 4. Retsrat your session. diff --git a/op.conf b/op.conf index a2446a3..ca4ac10 100644 --- a/op.conf +++ b/op.conf @@ -1,3 +1,4 @@ shellcheck: shellcheck setup fuzzycd && echo PASS shfmt: shfmt -d -i 2 -ci setup fuzzycd && echo PASS +codespell: codespell test: test/approve From a54676e2f80b7bfccf48e4a32b15cc738440cae2 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Mon, 22 Apr 2024 04:30:02 +0000 Subject: [PATCH 5/5] version bump 0.2.4 --- README.md | 2 +- fuzzycd | 2 +- test/approvals/cd_h | 2 +- test/approvals/cd_v | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9c7c622..3a96c47 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ instantly using a fuzzy match, with or without an interactive menu. # Fuzzy CD -![Version](https://img.shields.io/badge/version-0.2.3-blue.svg) +![Version](https://img.shields.io/badge/version-0.2.4-blue.svg) [![Build Status](https://github.com/DannyBen/fuzzycd/workflows/Test/badge.svg)](https://github.com/DannyBen/fuzzycd/actions?query=workflow%3ATest) ## Features diff --git a/fuzzycd b/fuzzycd index 8c4046e..897e802 100644 --- a/fuzzycd +++ b/fuzzycd @@ -1,7 +1,7 @@ #!/usr/bin/env bash fuzzycd_run() { - local version="0.2.3" + local version="0.2.4" local histfile=${FUZZYCD_HISTORY_FILE:-"$HOME/.fuzzycd-history"} _fzcd_is_dirname() { diff --git a/test/approvals/cd_h b/test/approvals/cd_h index 5171323..544e0a0 100644 --- a/test/approvals/cd_h +++ b/test/approvals/cd_h @@ -1,4 +1,4 @@ -fuzzycd 0.2.3 +fuzzycd 0.2.4 Usage: cd DIR change working directory diff --git a/test/approvals/cd_v b/test/approvals/cd_v index f21e581..42f119f 100644 --- a/test/approvals/cd_v +++ b/test/approvals/cd_v @@ -1 +1 @@ -fuzzycd 0.2.3 +fuzzycd 0.2.4