From df9fa32632c954425fb1c32a6078baa3cc995cab Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 8 Feb 2024 13:04:38 -0800 Subject: 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. --- README.md | 11 +++++++++++ cdlog.sh | 59 +++++++++++++++++++++++++++++++++++++++++++++-------------- 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 < # Vancouver, Canada -- cgit v1.2.3