diff --git a/README.md b/README.md index 2349fd0..013b0bb 100644 --- a/README.md +++ b/README.md @@ -46,5 +46,21 @@ Define the sd script in your shell by adding the following to a shell startup fi source "" ``` +#### Fuzzy finding +If [fzf](https://github.com/junegunn/fzf) is in `$PATH` shifting to a non matching point will bring fzf up with a list of all points, pre filtered with your non matching point. + +After: +```bash +sd add foo +sd add bar +sd add baz + +sd ba +``` +you will face fzf prompting yout to choose between baz and bar + + + + # Contributing Pull requests are always welcome. diff --git a/sd b/sd index 845b881..4db8140 100644 --- a/sd +++ b/sd @@ -115,12 +115,17 @@ sd() { point_path="$sdd/$requested_point" if [[ ! -L "$point_path" ]]; then - echo "Can't shift to point '$requested_point' because it doesn't exist." + + type fzf >/dev/null 2>&1 || { + echo "Can't shift to point '$requested_point' because it doesn't exist." return 1 + } + point_path="$sdd/$(find "$sdd" -maxdepth 1 -type l -exec basename "{}" \; |fzf -q "$*")" + if [ "$?" -ne "0" ] ; then + return 1 + fi fi - local requested_destination="$(readlink $point_path)/$subpath" - cd "$requested_destination" return $? } diff --git a/shpec/sd_shpec.sh b/shpec/sd_shpec.sh index da3ab33..02a7d54 100644 --- a/shpec/sd_shpec.sh +++ b/shpec/sd_shpec.sh @@ -80,4 +80,29 @@ describe "sd" rmdir "$path_with_spaces" end end + describe "using fzf" + it "should use fzf if availible" + cd /tmp + sd add __TEMP &> /dev/null + stub_command "fzf" "echo __TEMP" + cd / + shifted_pwd="$(sd tmp && pwd)" + assert equal "$shifted_pwd" "/tmp" + rm "$sdd/__TEMP" + end + + it "should pass all arguments to fzf as a search query" + stub_command "fzf" 'echo "$*" > /tmp/fzfargs' + sd foobar + assert equal "-q foobar" "$(cat /tmp/fzfargs)" + rm /tmp/fzfargs + end + + it "shouldn't do anything if fzf returns non-zero" + stub_command "fzf" "return 130" + cd /tmp + sd nonexistent + assert equal "$(pwd)" "/tmp" + end + end end