Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ You are encouraged to inspect the [setup script](setup) before running.
cd -l list history with fzf
cd -e edit history file
cd -s show history file
cd -p prune non-existent directories from history
cd -d [DIR] delete current or specified directory from history
cd -c show completions function [usage: eval "$(cd -c)"]
cd -v show version
Expand Down
22 changes: 21 additions & 1 deletion fuzzycd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

fuzzycd_run() {
local version="0.2.4"
local version="0.2.5"
local histfile=${FUZZYCD_HISTORY_FILE:-"$HOME/.fuzzycd-history"}

_fzcd_is_dirname() {
Expand Down Expand Up @@ -89,6 +89,24 @@ fuzzycd_run() {
echo "fuzzycd $version"
}

_fzcd_prune_history() {
local dir tmpfile removed
tmpfile=$(mktemp "${histfile}.XXXXXX") || return 1
removed=0

while IFS= read -r dir; do
if [[ -d "$dir" ]]; then
echo "$dir" >>"$tmpfile"
else
echo "pruned $dir"
removed=1
fi
done <"$histfile"

mv "$tmpfile" "$histfile"
[[ $removed -eq 0 ]] && echo "nothing to prune"
}

_fzcd_delete_dir() {
dir="${1:-$PWD}"
grep -Fxv "$dir" "$histfile" >"$HOME/.fuzzycd-history.tmp"
Expand All @@ -106,6 +124,7 @@ fuzzycd_run() {
echo " cd -l list history with fzf"
echo " cd -e edit history file"
echo " cd -s show history file"
echo " cd -p prune non-existent directories from history"
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"
Expand Down Expand Up @@ -161,6 +180,7 @@ fuzzycd_run() {
"-v") _fzcd_show_version ;;
"-e") _fzcd_edit_histfile ;;
"-s") _fzcd_show_histfile ;;
"-p") _fzcd_prune_history ;;
"-c") _fzcd_show_completions ;;
"-d")
shift
Expand Down
2 changes: 2 additions & 0 deletions test/approvals/cat_histfile
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/usr/local/bin
/another/gone/dir
/and/another/gone/dir
1 change: 1 addition & 0 deletions test/approvals/cat_histfile_pruned
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/usr/local/bin
3 changes: 2 additions & 1 deletion test/approvals/cd_h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
fuzzycd 0.2.4
fuzzycd 0.2.5

Usage:
cd DIR change working directory
cd SEARCH change working directory or show selection menu
cd -l list history with fzf
cd -e edit history file
cd -s show history file
cd -p prune non-existent directories from history
cd -d [DIR] delete current or specified directory from history
cd -c show completions function [usage: eval "$(cd -c)"]
cd -v show version
Expand Down
2 changes: 2 additions & 0 deletions test/approvals/cd_p
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pruned /another/gone/dir
pruned /and/another/gone/dir
1 change: 1 addition & 0 deletions test/approvals/cd_p_nothing_pruned
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nothing to prune
2 changes: 1 addition & 1 deletion test/approvals/cd_v
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fuzzycd 0.2.4
fuzzycd 0.2.5
9 changes: 9 additions & 0 deletions test/approve
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,22 @@ context "when the history file contains some entries"
context "when the history file contains gone directories"
echo /usr/local/bin > "$FUZZYCD_HISTORY_FILE"
echo /no/such/dir >> "$FUZZYCD_HISTORY_FILE"
echo /another/gone/dir >> "$FUZZYCD_HISTORY_FILE"
echo /and/another/gone/dir >> "$FUZZYCD_HISTORY_FILE"

describe "cd DIR"
it "deletes the gone directory"
approve "cd suchdir" || true
expect_exit_code 1
approve "cat $FUZZYCD_HISTORY_FILE" "cat_histfile"

describe "cd -p"
it "removes all gone directories"
approve "cd -p"
approve "cat $FUZZYCD_HISTORY_FILE" "cat_histfile_pruned"

it "states that nothing was pruned if there is nothing to do"
approve "cd -p" "cd_p_nothing_pruned"

context "when CDPATH contains entries"
echo /usr/local/bin > "$FUZZYCD_HISTORY_FILE"
Expand Down