From 54abe4cd5eba749f3150fa77c9845bedde8fddf1 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 13 Feb 2024 07:16:46 -0800 Subject: Split off recovery from initialization. Prompting the user on startup can be a nuisance. For instance, each time I make a new shell out of a text editor, I get the prompt. Let's just leave the user in a new context, and make it an explicit command to switch context. * cdlog.sh (cdlog.init): Remove logic for selecting a new context, leaving only the allocation of a new context. (cdlog.recover): New function, split off from cdlog.init. (cdr): New alias. * README.md: Documented. --- README.md | 9 +++++++-- cdlog.sh | 23 +++++++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 53d26c8..c6bf5d8 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,11 @@ pick which directory to change to or swap with. The terminal cursor is then retraced back to the top of the menu, and the screen is erased from that point to the bottom. +* The `cdr` command is used for recovering previous sessions. It +presents you with a numbered list of these sessions. You can recover to one of +the sessions, use a `c` prefix on the number to clone the session into +the current session, or press Enter to do nothing. + In addition, the `cdlog.sh` script sets the bash `direxpand` option. With the `direxpand` option, Tab completion on a directory coming from a variable name will expand that variable into the command line. @@ -64,8 +69,8 @@ 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. +mentions this, suggesting that the `cdr` command may be used to recover +to one of the sessions. ## How is this better? diff --git a/cdlog.sh b/cdlog.sh index 99c3338..5db4ad7 100644 --- a/cdlog.sh +++ b/cdlog.sh @@ -42,15 +42,13 @@ cdlog.get_param() fi } -# Read state from ~/.cdlog.dirs +# Initialize state, allocating new ~/.cdlog.N.dirs storage, +# with LRU replacement. cdlog.init() { - local -a d - local dir local i local oldest local context - local sel c9=; c8=; c7= c6=; c5=; c4= @@ -77,9 +75,21 @@ cdlog.init() fi cdlog_dirs=~/.cdlog.$context.dirs - rm -f $cdlog_dirs + set -- ~/.cdlog.*.dirs + if [ $# -gt 0 -a $1 != ~/.cdlog.'*'.dirs ] ; then + printf "Use 'cdr' or 'cdlog.recover' to switch to prior cdlog context.\n" + fi +} + +cdlog.recover() +{ + local -a d + local dir + local i + local sel + set -- ~/.cdlog.*.dirs if [ $# -gt 0 -a $1 != ~/.cdlog.'*'.dirs ] ; then @@ -96,7 +106,7 @@ cdlog.init() else printf "Use 1-%s to select context, c1-c%s to clone,\n" $i $i fi - printf "or Enter for new blank ontext: " + printf "or Enter to keep current context: " read sel case $sel in ( [1-9] ) @@ -282,6 +292,7 @@ alias cl='cdlog' alias cll='cdlog -l' alias mcd='cdlog.mcd' alias mcs='cdlog.mcd -s' +alias cdr='cdlog.recover' # Better completion for $x[Tab] shopt -s direxpand -- cgit v1.2.3