aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-02-03 21:51:12 -0800
committerKaz Kylheku <kaz@kylheku.com>2024-02-03 21:51:12 -0800
commitdaf140a6a54968a20920f8f5c167cdd27709a83c (patch)
treee7c0b1c8c31a50660e8d9e94c8bd072ae433af47
parentbe4d728fe35561af37d98e9f51a6e89232a1e53e (diff)
downloadcdlog-daf140a6a54968a20920f8f5c167cdd27709a83c.tar.gz
cdlog-daf140a6a54968a20920f8f5c167cdd27709a83c.tar.bz2
cdlog-daf140a6a54968a20920f8f5c167cdd27709a83c.zip
pd: support optional argument about which entry to pop.
* cdlog.sh (cdlog.pop): Rewritten to take argument 1-9, and iterate from there. * README.md: Documented.
-rw-r--r--README.md8
-rw-r--r--cdlog.sh31
2 files changed, 28 insertions, 11 deletions
diff --git a/README.md b/README.md
index 6b96ef4..c25ef67 100644
--- a/README.md
+++ b/README.md
@@ -29,9 +29,11 @@ arguments, it rotates among those. The directory is changed to the
by the second item, the second by the third and so on. The last item
receives the previously current directory.
-* `pd` (pop dir) is an alias for a command which changes to the most recent
-directory in the log, and removes that entry from the log. The second
-most recent directory becomes most recent and so on.
+* `pd` (pop dir) is an alias for a command which changes to (by default) the
+most recent directory in the log, and removes that entry from the log. The second most
+recent directory becomes most recent and so on. If it is given an argument
+value in the range 1 to 9, it changes to the specified entry, and removes it,
+moving the others down to close the gap. Thus `pd 1` is equivalent to `pd`.
* `cdlog` function shows a listing of the four most recent entries in the
log, or all nine if given the `-l` argument. The `cl` command is an alias for
diff --git a/cdlog.sh b/cdlog.sh
index 272d42e..cf10868 100644
--- a/cdlog.sh
+++ b/cdlog.sh
@@ -91,17 +91,32 @@ cdlog.rot()
# from the log.
cdlog.pop()
{
- if [ -n "$c1" ] && command cd "$c1"; then
- c1=$c2; c2=$c3; c3=$c4
- c4=$c5; c5=$c6; c6=$c7
- c7=$c8; c8=$c9;
-
- c9=
+ local d=("" "$c1" "$c2" "$c3" "$c4" "$c5" "$c6" "$c7" "$c8" "$c9")
+ local n=1
+ local i
- cdlog.nicks
+ unset d[0]
- printf "%s\n" "$PWD"
+ if [ $# -gt 0 ] ; then
+ n=$1; shift
fi
+
+ case $n in
+ ( [1-9] )
+ if command cd "${d[$n]}" ; then
+ for ((i = n; i <= 8; i++)); do
+ d[$i]=${d[$((i + 1))]}
+ done
+ d[9]=
+ cdlog.args "${d[@]}"
+ printf "%s\n" "$PWD"
+ fi
+ ;;
+ ( * )
+ printf "cdlog.pop: bad argument: %s\n" "$n"
+ return 1
+ ;;
+ esac
}
# Print four recent cdlog entries.