diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2024-02-08 17:49:53 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2024-02-08 17:49:53 -0800 |
commit | 82bdc9e8ce75c44979701ef052f038e2a9a4448e (patch) | |
tree | f005cdbd866e5c3320e38140fb2ade08e56c0ae3 | |
parent | df9fa32632c954425fb1c32a6078baa3cc995cab (diff) | |
download | cdlog-82bdc9e8ce75c44979701ef052f038e2a9a4448e.tar.gz cdlog-82bdc9e8ce75c44979701ef052f038e2a9a4448e.tar.bz2 cdlog-82bdc9e8ce75c44979701ef052f038e2a9a4448e.zip |
Implement multiple persisted sessions.
* cdlog.sh (cdlog.update): Name of file is now in $cdlog_dirs.
(cdlog.get_param): New function.
(cdlog.init): Implement the LRU session selection and optional
recovery. The variable $cdlog_dirs is established, pointing
to the selected session file.
(mainline): Run cdlog.init if $cdlog_dirs is empty.
* README.md: Documented.
-rw-r--r-- | README.md | 16 | ||||
-rw-r--r-- | cdlog.sh | 72 |
2 files changed, 79 insertions, 9 deletions
@@ -55,11 +55,17 @@ 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. +contents of the FIFO are written to the file `~/.cslog.N.dirs`, +one path per line, where `N` is an internal session number. + +When `cdlog` initializes, it allocates a session by finding a free +value `N` in the range 1 to 10. If it cannot find one due to the +session store being full, it erases the one with the oldest time +stamp and chooses its index. + +When one or more persisted session are present, `cdlog`'s initialization +presents you with a numbered list of these sessions. You can recover to one of +the sessions or press Enter to go with the newly allocated one. ## How is this better? @@ -10,7 +10,7 @@ cdlog.update() w=$c4 # Persist to disk. - cat > ~/.cdlog.dirs <<! + cat > "$cdlog_dirs" <<! $PWD $c1 $c2 @@ -34,17 +34,81 @@ cdlog.args() cdlog.update } +# Utility for nth positional param +cdlog.get_param() +{ + shift $1 + echo "$1" +} + # Read state from ~/.cdlog.dirs cdlog.init() { local -a d + local dir + local i + local oldest + local context + local sel c9=; c8=; c7= c6=; c5=; c4= c3=; c2= - if [ -f ~/.cdlog.dirs ] ; then - mapfile -t d < ~/.cdlog.dirs + cdlog_dirs= + context= + + for i in {1..10}; do + if ! [ -f ~/.cdlog.$i.dirs ] ; then + context=$i + break + fi + done + + if ! [ $context ] ; then + oldest=1 + for i in {2..10}; do + if [ ~/.cdlog.$i.dirs -ot ~/.cdlog.$oldest.dirs ] ; then + oldest=$i + fi + done + context=$oldest + fi + + cdlog_dirs=~/.cdlog.$context.dirs + + rm -f $cdlog_dirs + + set -- ~/.cdlog.*.dirs + + if [ $# -gt 0 -a $1 != ~/.cdlog.'*'.dirs ] ; then + printf "These cdlog contexts exist:\n" + i=1 + for dir in "$@"; do + printf "[%d]: %s\n" $((i++)) "$(date -r "$dir")" + sed -e '/^$/d' "$dir" + done + + while true; do + printf "Make a selection or press Enter for new context: " + read sel + case $sel in + ( [1-9] ) + sel=$(cdlog.get_param $sel "$@") + if [ -n "$sel" ] ; then + cdlog_dirs=$sel + break + fi + ;; + ( "" ) + break + ;; + esac + done + fi + + if [ -f "$cdlog_dirs" ] ; then + mapfile -t d < "$cdlog_dirs" command cd "${d[0]}" unset d[0] cdlog.args "${d[@]}" @@ -213,7 +277,7 @@ shopt -s direxpand c1=${c1-} -if [ -z "$c1" ] ; then +if [ -z "$c1" -o -z "$cdlog_dirs" ] ; then cdlog.init fi |