From 5070e9a4cc0443a7851e64aa7ba1e64ef45c4a37 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Thu, 13 Nov 2025 03:52:19 +0000 Subject: [PATCH] - Add `cd -p` to prune all gone dirs --- README.md | 1 + fuzzycd | 22 +++++++++++++++++++++- test/approvals/cat_histfile | 2 ++ test/approvals/cat_histfile_pruned | 1 + test/approvals/cd_h | 3 ++- test/approvals/cd_p | 2 ++ test/approvals/cd_p_nothing_pruned | 1 + test/approvals/cd_v | 2 +- test/approve | 9 +++++++++ 9 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 test/approvals/cat_histfile_pruned create mode 100644 test/approvals/cd_p create mode 100644 test/approvals/cd_p_nothing_pruned diff --git a/README.md b/README.md index f64d886..43fe74a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/fuzzycd b/fuzzycd index 897e802..331c3ee 100644 --- a/fuzzycd +++ b/fuzzycd @@ -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() { @@ -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" @@ -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" @@ -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 diff --git a/test/approvals/cat_histfile b/test/approvals/cat_histfile index 85feb78..e071fbe 100644 --- a/test/approvals/cat_histfile +++ b/test/approvals/cat_histfile @@ -1 +1,3 @@ /usr/local/bin +/another/gone/dir +/and/another/gone/dir diff --git a/test/approvals/cat_histfile_pruned b/test/approvals/cat_histfile_pruned new file mode 100644 index 0000000..85feb78 --- /dev/null +++ b/test/approvals/cat_histfile_pruned @@ -0,0 +1 @@ +/usr/local/bin diff --git a/test/approvals/cd_h b/test/approvals/cd_h index 544e0a0..5a58be6 100644 --- a/test/approvals/cd_h +++ b/test/approvals/cd_h @@ -1,4 +1,4 @@ -fuzzycd 0.2.4 +fuzzycd 0.2.5 Usage: cd DIR change working directory @@ -6,6 +6,7 @@ Usage: 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 diff --git a/test/approvals/cd_p b/test/approvals/cd_p new file mode 100644 index 0000000..cb986ee --- /dev/null +++ b/test/approvals/cd_p @@ -0,0 +1,2 @@ +pruned /another/gone/dir +pruned /and/another/gone/dir diff --git a/test/approvals/cd_p_nothing_pruned b/test/approvals/cd_p_nothing_pruned new file mode 100644 index 0000000..1cd7d4d --- /dev/null +++ b/test/approvals/cd_p_nothing_pruned @@ -0,0 +1 @@ +nothing to prune diff --git a/test/approvals/cd_v b/test/approvals/cd_v index 42f119f..bbdd754 100644 --- a/test/approvals/cd_v +++ b/test/approvals/cd_v @@ -1 +1 @@ -fuzzycd 0.2.4 +fuzzycd 0.2.5 diff --git a/test/approve b/test/approve index 44325aa..4f0960c 100755 --- a/test/approve +++ b/test/approve @@ -75,6 +75,8 @@ 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" @@ -82,6 +84,13 @@ context "when the history file contains gone directories" 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"