Skip to content

Commit ea64569

Browse files
authored
Merge pull request #20 from DannyBen/add/sanitize-history
Add `cd -p` to prune all gone dirs
2 parents 9db186f + 5070e9a commit ea64569

File tree

9 files changed

+40
-3
lines changed

9 files changed

+40
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ You are encouraged to inspect the [setup script](setup) before running.
6868
cd -l list history with fzf
6969
cd -e edit history file
7070
cd -s show history file
71+
cd -p prune non-existent directories from history
7172
cd -d [DIR] delete current or specified directory from history
7273
cd -c show completions function [usage: eval "$(cd -c)"]
7374
cd -v show version

fuzzycd

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

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

77
_fzcd_is_dirname() {
@@ -89,6 +89,24 @@ fuzzycd_run() {
8989
echo "fuzzycd $version"
9090
}
9191

92+
_fzcd_prune_history() {
93+
local dir tmpfile removed
94+
tmpfile=$(mktemp "${histfile}.XXXXXX") || return 1
95+
removed=0
96+
97+
while IFS= read -r dir; do
98+
if [[ -d "$dir" ]]; then
99+
echo "$dir" >>"$tmpfile"
100+
else
101+
echo "pruned $dir"
102+
removed=1
103+
fi
104+
done <"$histfile"
105+
106+
mv "$tmpfile" "$histfile"
107+
[[ $removed -eq 0 ]] && echo "nothing to prune"
108+
}
109+
92110
_fzcd_delete_dir() {
93111
dir="${1:-$PWD}"
94112
grep -Fxv "$dir" "$histfile" >"$HOME/.fuzzycd-history.tmp"
@@ -106,6 +124,7 @@ fuzzycd_run() {
106124
echo " cd -l list history with fzf"
107125
echo " cd -e edit history file"
108126
echo " cd -s show history file"
127+
echo " cd -p prune non-existent directories from history"
109128
echo " cd -d [DIR] delete current or specified directory from history"
110129
echo " cd -c show completions function [usage: eval \"\$(cd -c)\"]"
111130
echo " cd -v show version"
@@ -161,6 +180,7 @@ fuzzycd_run() {
161180
"-v") _fzcd_show_version ;;
162181
"-e") _fzcd_edit_histfile ;;
163182
"-s") _fzcd_show_histfile ;;
183+
"-p") _fzcd_prune_history ;;
164184
"-c") _fzcd_show_completions ;;
165185
"-d")
166186
shift

test/approvals/cat_histfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/usr/local/bin
2+
/another/gone/dir
3+
/and/another/gone/dir

test/approvals/cat_histfile_pruned

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/usr/local/bin

test/approvals/cd_h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
fuzzycd 0.2.4
1+
fuzzycd 0.2.5
22

33
Usage:
44
cd DIR change working directory
55
cd SEARCH change working directory or show selection menu
66
cd -l list history with fzf
77
cd -e edit history file
88
cd -s show history file
9+
cd -p prune non-existent directories from history
910
cd -d [DIR] delete current or specified directory from history
1011
cd -c show completions function [usage: eval "$(cd -c)"]
1112
cd -v show version

test/approvals/cd_p

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pruned /another/gone/dir
2+
pruned /and/another/gone/dir

test/approvals/cd_p_nothing_pruned

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nothing to prune

test/approvals/cd_v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fuzzycd 0.2.4
1+
fuzzycd 0.2.5

test/approve

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,22 @@ context "when the history file contains some entries"
7575
context "when the history file contains gone directories"
7676
echo /usr/local/bin > "$FUZZYCD_HISTORY_FILE"
7777
echo /no/such/dir >> "$FUZZYCD_HISTORY_FILE"
78+
echo /another/gone/dir >> "$FUZZYCD_HISTORY_FILE"
79+
echo /and/another/gone/dir >> "$FUZZYCD_HISTORY_FILE"
7880

7981
describe "cd DIR"
8082
it "deletes the gone directory"
8183
approve "cd suchdir" || true
8284
expect_exit_code 1
8385
approve "cat $FUZZYCD_HISTORY_FILE" "cat_histfile"
8486

87+
describe "cd -p"
88+
it "removes all gone directories"
89+
approve "cd -p"
90+
approve "cat $FUZZYCD_HISTORY_FILE" "cat_histfile_pruned"
91+
92+
it "states that nothing was pruned if there is nothing to do"
93+
approve "cd -p" "cd_p_nothing_pruned"
8594

8695
context "when CDPATH contains entries"
8796
echo /usr/local/bin > "$FUZZYCD_HISTORY_FILE"

0 commit comments

Comments
 (0)