aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-02-08 13:04:38 -0800
committerKaz Kylheku <kaz@kylheku.com>2024-02-08 13:04:38 -0800
commitdf9fa32632c954425fb1c32a6078baa3cc995cab (patch)
tree6d1a0aced25230127fabcab37536848302b3b275
parentdaf140a6a54968a20920f8f5c167cdd27709a83c (diff)
downloadcdlog-df9fa32632c954425fb1c32a6078baa3cc995cab.tar.gz
cdlog-df9fa32632c954425fb1c32a6078baa3cc995cab.tar.bz2
cdlog-df9fa32632c954425fb1c32a6078baa3cc995cab.zip
Persist state in ~/.cdlog.dirs.
* cdlog.sh (cdlog.nicks): Function removed. (cdlog.update): New function, performing task of cdlog.nicks as well as persistence to ~/.cdlog.dirs file. (cdlog.init): New function that initializes the c1-c9 variables. (mainline): Call cdlog.init. This is now done later in the file after the function definitions. (cdlog.args, cdlog.chdir): Call cdlog.update instead of cdlog.nicks. This is also how the variables $x to $w will be set the first time when recovering state, because cdlog.init calls cdlog.args to install the FIFO, and cdlog.args will call cdlog.update. * README.md: Documented.
-rw-r--r--README.md11
-rw-r--r--cdlog.sh59
2 files changed, 56 insertions, 14 deletions
diff --git a/README.md b/README.md
index c25ef67..db9a0a1 100644
--- a/README.md
+++ b/README.md
@@ -52,6 +52,15 @@ from a variable name will expand that variable into the command line.
The directory changing commands do nothing if you attempt to change into the
same directory you are already in. There is no effect on the FIFO.
+## Persistence
+
+Whenever the nine-element FIFO changes, the current directory and the
+contents of the FIFO are written to the file `~/.cslog.dirs`,
+one path per line. When `cdlog` initializes, it reads the contents of that
+file. It changes to the directory indicated in the first line, and
+stuffs the remaining nine lines into the FIFO, thereby recovering
+your session.
+
## How is this better?
* It's not hard-coded C code inside the shell; it consists of a small amount of
@@ -82,6 +91,8 @@ The rotating command `cs` is usefully different from `pushd [+/-]n`,
taking only positive integer arguments with no sign, and rotating among
specified places, rather than a contiguous block of top entries.
+* The persistence feature recovers the directories from a previous session.
+
## License
This is under distributed under a modified two-clause BSD license.
diff --git a/cdlog.sh b/cdlog.sh
index cf10868..ea585ad 100644
--- a/cdlog.sh
+++ b/cdlog.sh
@@ -1,22 +1,27 @@
# License at bottom.
-# Initialize cdlog the first time.
-
-c1=${c1-}
-
-if [ -z "$c1" ] ; then
- c9=; c8=; c7=
- c6=; c5=; c4=
- c3=; c2=
-fi
-
-# One-letter nicknames for most recent four dirs.
-cdlog.nicks()
+# Update after FIFO change.
+cdlog.update()
{
+ # Update the one-letter nicknames for most recent four dirs.
x=$c1
y=$c2
z=$c3
w=$c4
+
+ # Persist to disk.
+ cat > ~/.cdlog.dirs <<!
+$PWD
+$c1
+$c2
+$c3
+$c4
+$c5
+$c6
+$c7
+$c8
+$c9
+!
}
# Set state from args
@@ -26,7 +31,24 @@ cdlog.args()
c4=$4; c5=$5; c6=$6
c7=$7; c8=$8; c9=$8
- cdlog.nicks
+ cdlog.update
+}
+
+# Read state from ~/.cdlog.dirs
+cdlog.init()
+{
+ local -a d
+
+ c9=; c8=; c7=
+ c6=; c5=; c4=
+ c3=; c2=
+
+ if [ -f ~/.cdlog.dirs ] ; then
+ mapfile -t d < ~/.cdlog.dirs
+ command cd "${d[0]}"
+ unset d[0]
+ cdlog.args "${d[@]}"
+ fi
}
# Change to directory: this is aliased to cd command.
@@ -44,7 +66,7 @@ cdlog.chdir()
c1=$cur
- cdlog.nicks
+ cdlog.update
fi
}
@@ -187,6 +209,15 @@ alias mcs='cdlog.mcd -s'
# Better completion for $x[Tab]
shopt -s direxpand
+# Initialize cdlog the first time.
+
+c1=${c1-}
+
+if [ -z "$c1" ] ; then
+ cdlog.init
+fi
+
+
# Copyright 2024
# Kaz Kylheku <kaz@kylheku.com>
# Vancouver, Canada