aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-01-19 10:24:24 -0800
committerKaz Kylheku <kaz@kylheku.com>2024-01-19 10:24:24 -0800
commit64bbf231833016531f7c7adc42fad3ebeeb66bfd (patch)
tree465dbb6e8ffbdac3644b14c279d79161f8d3cb88
parentc99883c2dceea4df797bb9f82c755a62cd4598d4 (diff)
downloadcdlog-64bbf231833016531f7c7adc42fad3ebeeb66bfd.tar.gz
cdlog-64bbf231833016531f7c7adc42fad3ebeeb66bfd.tar.bz2
cdlog-64bbf231833016531f7c7adc42fad3ebeeb66bfd.zip
Generalize swap to rotate.
* cdlog.sh (cdlog.swap): Function removed. (cdlog.rot): New function. (cs): Alias now points to cdlog.rot. * README.md: Updated.
-rw-r--r--README.md6
-rw-r--r--cdlog.sh46
2 files changed, 33 insertions, 19 deletions
diff --git a/README.md b/README.md
index a98c6e5..28236fd 100644
--- a/README.md
+++ b/README.md
@@ -23,7 +23,11 @@ The first four entries are also copied into the variables `x`, `y`,
the current directory with a selected `cslog` entry selected
by a numeric argument, which defaults to 1. The current directory
overwrites that entry in the log, and then the shell changes to the directory
-which was previously in that entry.
+which was previously in that entry. If `cs` is given two or more
+arguments, it rotates among those. The directory is changed to the
+`cdlog` item indicated by the first argument. That item is overwritten
+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
diff --git a/cdlog.sh b/cdlog.sh
index 645c483..a617b4b 100644
--- a/cdlog.sh
+++ b/cdlog.sh
@@ -50,40 +50,50 @@ cdlog.chdir()
fi
}
-# Swap current directory with specified directory
-cdlog.swap()
+# Rotate through specified directories.
+cdlog.rot()
{
local cur=$PWD
- local n=1
+ local p=$cur
+ local n=$cur
local d=("" "$c1" "$c2" "$c3" "$c4" "$c5" "$c6" "$c7" "$c8" "$c9")
unset d[0]
- if [ $# -eq 1 ]; then
- n=$1
- fi
-
- case $n in
- ( x ) n = 1 ;;
- ( y ) n = 2 ;;
- ( z ) n = 3 ;;
- ( w ) n = 4 ;;
- esac
+ [ $# -gt 0 ] || set 1
+ n=$1; shift
case $n in
( [1-9] )
- if command cd "${d[$n]}" ; then
- d[$n]=$cur
- fi
+ command cd "${d[$n]}" || return
+ p=$n
;;
( * )
- printf "cdlog.swap: bad argument: %s\n" "$n"
+ printf "cdlog.rot: bad argument: %s\n" "$n"
+ return 1
;;
esac
+ while [ $# -gt 0 ] ; do
+ n=$1; shift
+ case $n in
+ ( [1-9] )
+ d[$p]=${d[$n]}
+ p=$n
+ ;;
+ ( * )
+ printf "cdlog.rot: bad argument: %s\n" "$n"
+ return 1
+ ;;
+ esac
+ done
+
+ d[$p]=$cur
+
cdlog.args "${d[@]}"
}
+
# Change to most recent diretory in cdlog and remove it
# from the log.
cdlog.pop()
@@ -112,7 +122,7 @@ cdlog()
# Aliases.
alias cd='cdlog.chdir -P'
alias pd='cdlog.pop'
-alias cs='cdlog.swap'
+alias cs='cdlog.rot'
# Better completion for $x[Tab]
shopt -s direxpand