aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-01-18 11:53:47 -0800
committerKaz Kylheku <kaz@kylheku.com>2024-01-18 11:53:47 -0800
commit867f00998c07d6917077d5c1b41527fe267d3e5b (patch)
tree4e76fa5c67d7ee1e6b62a1da7f3cc849b1cc4e15
parent8aac2b8ac33edb29718f387fe7fe3022dc2fd8e2 (diff)
downloadcdlog-867f00998c07d6917077d5c1b41527fe267d3e5b.tar.gz
cdlog-867f00998c07d6917077d5c1b41527fe267d3e5b.tar.bz2
cdlog-867f00998c07d6917077d5c1b41527fe267d3e5b.zip
Initial version of cdlog.
-rw-r--r--cdlog.sh114
1 files changed, 114 insertions, 0 deletions
diff --git a/cdlog.sh b/cdlog.sh
new file mode 100644
index 0000000..5e53e4b
--- /dev/null
+++ b/cdlog.sh
@@ -0,0 +1,114 @@
+# License at bottom.
+
+# Initialize cdlog the first time.
+if [ -z "$c1" ] ; then
+ c9=
+ c8=
+ c7=
+ c6=
+ c5=
+ c4=
+ c3=
+ c2=
+fi
+
+# One-letter nicknames for most recent four dirs.
+cdlog_nicks()
+{
+ x=$c1
+ y=$c2
+ z=$c3
+ w=$c4
+}
+
+# Change to directory: this is aliased to cd command.
+cdlog_chdir()
+{
+ local cur=$PWD
+
+ if command cd "$@" && [ "$PWD" != "$cur" ]; then
+ # only if we successfully change to a different
+ # directory do the following
+
+ if [ "$cur" == "$c2" ] && [ "$PWD" == "$c1" ] ; then
+ # Special case: if we changed to the directory that is second
+ # in the cdlog, then just swap between those two.
+ c2=$PWD
+ c1=$cur
+ else
+ # Otherwise rotate through all
+ c9=$c8
+ c8=$c7
+ c7=$c6
+ c6=$c5
+ c5=$c4
+ c4=$c3
+ c3=$c2
+ c2=$c1
+ c1=$cur
+ fi
+
+ cdlog_nicks
+ fi
+}
+
+# Change to most recent diretory in cdlog and remove it
+# 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=
+
+ cdlog_nicks
+ fi
+}
+
+# Print four recen cdlog entries.
+cdlog()
+{
+ # c5 through c9 are "hidden"
+ printf "c1/x: %s\n" "$c1"
+ printf "c2/y: %s\n" "$c2"
+ printf "c3/z: %s\n" "$c3"
+ printf "c4/w: %s\n" "$c4"
+}
+
+# Aliases.
+alias cs='cdlog_chdir "$c1"'
+alias cd='cdlog_chdir -P'
+alias pd='cdlog_pop'
+
+# Better completion for $x[Tab]
+shopt -s direxpand
+
+# Copyright 2024
+# Kaz Kylheku <kaz@kylheku.com>
+# Vancouver, Canada
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following condition is met:
+#
+# 1. Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form need not reproduce any copyright notice.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.