From ae9c35cd63e2066462914571b9d41ef7934e5e4d Mon Sep 17 00:00:00 2001 From: Dirk Heilig Date: Thu, 6 Oct 2016 21:35:45 +0200 Subject: [PATCH 1/5] add the ability to use fzf on unknown points. --- sd | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/sd b/sd index 845b881..00f02a6 100644 --- a/sd +++ b/sd @@ -115,8 +115,18 @@ sd() { point_path="$sdd/$requested_point" if [[ ! -L "$point_path" ]]; then - echo "Can't shift to point '$requested_point' because it doesn't exist." - return 1 + if [ -z "$SD_USE_FZF" ]; then + echo "Can't shift to point '$requested_point' because it doesn't exist." + return 1 + else + type fzf >/dev/null 2>&1 || { + echo "Can't shift to point '$requested_point' because it doesn't exist." + echo "Also fzf should be used instead, but fzf can't be found." + return 1 + } + point_path="$sdd/$(find "$sdd" -maxdepth 1 -type l -printf "%f\n"|fzf -q "$*")" + fi + fi local requested_destination="$(readlink $point_path)/$subpath" From 73f4673591823b1cb643ea1d92428465f3c4cbbb Mon Sep 17 00:00:00 2001 From: Dirk Heilig Date: Wed, 12 Oct 2016 17:23:33 +0200 Subject: [PATCH 2/5] remove fzf-enabling env-var --- sd | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sd b/sd index 00f02a6..f37f66d 100644 --- a/sd +++ b/sd @@ -115,18 +115,18 @@ sd() { point_path="$sdd/$requested_point" if [[ ! -L "$point_path" ]]; then - if [ -z "$SD_USE_FZF" ]; then - echo "Can't shift to point '$requested_point' because it doesn't exist." - return 1 - else - type fzf >/dev/null 2>&1 || { - echo "Can't shift to point '$requested_point' because it doesn't exist." - echo "Also fzf should be used instead, but fzf can't be found." + + type fzf >/dev/null 2>&1 || { + echo "Can't shift to point '$requested_point' because it doesn't exist." + echo "Also fzf issn't in path so we can't fuzzy find a match, sorry." + return 1 + } + point_path="$sdd/$(find "$sdd" -maxdepth 1 -type l -printf "%f\n"|fzf -q "$*")" + if [ "$?" -ne "0" ] ; then return 1 - } - point_path="$sdd/$(find "$sdd" -maxdepth 1 -type l -printf "%f\n"|fzf -q "$*")" fi + fi local requested_destination="$(readlink $point_path)/$subpath" From b555e9014e80f6b427179d23c049c4df71a2e46d Mon Sep 17 00:00:00 2001 From: Dirk Heilig Date: Wed, 12 Oct 2016 17:23:52 +0200 Subject: [PATCH 3/5] add test-cases --- shpec/sd_shpec.sh | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/shpec/sd_shpec.sh b/shpec/sd_shpec.sh index da3ab33..cfc3cf6 100644 --- a/shpec/sd_shpec.sh +++ b/shpec/sd_shpec.sh @@ -80,4 +80,42 @@ 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 + + it "should fail with unknown point and no fzf" + stub_command "type" "return 1" + cd /tmp + sd nonexistant &> /dev/null + assert equal "$?" "1" + end + it "should not change cwd with unknown point and no fzf" + stub_command "type" "return 1" + cd /tmp + sd nonexistant &> /dev/null + assert equal "$(pwd)" "/tmp" + end + end end From cdb91733bb245b046d230ce9cd9db3895f3dc965 Mon Sep 17 00:00:00 2001 From: Dirk Heilig Date: Wed, 12 Oct 2016 17:24:06 +0200 Subject: [PATCH 4/5] add documentation for the fzf feature --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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. From 4b70ea811267bd71648fb906075a51a83dda4c99 Mon Sep 17 00:00:00 2001 From: Dirk Heilig Date: Mon, 7 Nov 2016 14:23:39 +0100 Subject: [PATCH 5/5] Fix @rylnd's findings: - remove extra message about fzf not existent - osx find not havong -printf, using -exec basename instead - multiple whitespace issue --- sd | 19 +++++++------------ shpec/sd_shpec.sh | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/sd b/sd index 00f02a6..4db8140 100644 --- a/sd +++ b/sd @@ -115,22 +115,17 @@ sd() { point_path="$sdd/$requested_point" if [[ ! -L "$point_path" ]]; then - if [ -z "$SD_USE_FZF" ]; then - echo "Can't shift to point '$requested_point' because it doesn't exist." - return 1 - else - type fzf >/dev/null 2>&1 || { - echo "Can't shift to point '$requested_point' because it doesn't exist." - echo "Also fzf should be used instead, but fzf can't be found." + + 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 - } - point_path="$sdd/$(find "$sdd" -maxdepth 1 -type l -printf "%f\n"|fzf -q "$*")" 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