aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2025-02-04 11:12:34 -0800
committerKaz Kylheku <kaz@kylheku.com>2025-02-04 11:12:34 -0800
commitb3c0288a9b245993c8b220f61ded7b2c964709a8 (patch)
tree3551e8e2807f351ec8fac295b16d001fdcae2b41
parentec997272d4402ce8437bdae3bc64f8bad7fd83cb (diff)
downloadcdlog-b3c0288a9b245993c8b220f61ded7b2c964709a8.tar.gz
cdlog-b3c0288a9b245993c8b220f61ded7b2c964709a8.tar.bz2
cdlog-b3c0288a9b245993c8b220f61ded7b2c964709a8.zip
New feature: auto recovery.
* README.md: Describe the cdlog_autorecovery variable. * cdlog.sh (cdlog_autorecover): New variable. (cdlog.init): When there is only one saved session, then call cdlog.recover with the secret new __auto argument. (cdlog.recover): When there is only one saved session and the __auto argument has been given, then make the selection 1 for the user. In this case we print the current working directory.
-rw-r--r--README.md24
-rw-r--r--cdlog.sh22
2 files changed, 42 insertions, 4 deletions
diff --git a/README.md b/README.md
index 5382ddb..4857d22 100644
--- a/README.md
+++ b/README.md
@@ -140,7 +140,7 @@ stamp and chooses its index.
When one or more persisted session are present, `cdlog`'s initialization
mentions this, suggesting that the `cdr` command may be used to recover
-to one of the sessions.
+to one of the sessions. See also the Auto Recovery section below.
## LRU Mode
@@ -157,6 +157,9 @@ directory, and is removed; the entries above it shift down to take
its place, and the previous current directory becomes the top of
the history.
+LRU mode is disabled when the `cdlog_lru` variable is empty,
+which is its initial value.
+
LRU mode keeps duplicate directories out of the history and while
promoting recently used directories toward the top.
@@ -174,6 +177,25 @@ host machines with different environments. You can interpolate the
value of `$HOSTNAME` into `cdlog_sess_dir` to have sessions specific
to a host.
+## Auto Recovery
+
+When the `cdlog_autorecover` variable is set to `y`, `cdlog` will
+automatically recover the session without the need for using the
+`cdr` command. This only happens when these two conditions hold:
+
+1. Bash is either a login shell, or the `SHLVL` variable is `1`.
+
+2. There is only one saved session.
+
+This feature exists because it is extremely common to use `cdr`
+upon logging in and to choose the only available session, by
+selecting `1`. However, automatic recovery may navigate away from
+the home directory, which may surprise the user who has not logged
+into that host in a long time. Thus it is not default behavior.
+
+When `cdlog_autorecover` is empty, which is the initial value,
+the feature is disabled.
+
## Completion
`cdlog` provides its own Tab completion for the `cd` command, overriding
diff --git a/cdlog.sh b/cdlog.sh
index 2e2fae9..4a3b92b 100644
--- a/cdlog.sh
+++ b/cdlog.sh
@@ -12,6 +12,7 @@ unset cdlog_alias[0] # history is 1-based
# Configuration variables
cdlog_lru=${cdlog_lru-}
cdlog_sess_dir=${cdlog_sess_dir-~}
+cdlog_autorecover=${cdlog_autorecover-}
# Set state from args
cdlog.args()
@@ -89,7 +90,13 @@ cdlog.init()
set -- "$csd"/.cdlog.*.dirs
if [ $# -gt 0 -a $1 != "$csd"/.cdlog.'*'.dirs ] ; then
- printf "Use 'cdr' or 'cdlog.recover' to switch to prior cdlog context.\n"
+ if [ "$cdlog_autorecover" -a $# -eq 1 -a \
+ \( "$SHLVL" == 1 -o "${0%%[a-z]*}" == "-" \) ]
+ then
+ cdlog.recover __auto
+ else
+ printf "Use 'cdr' or 'cdlog.recover' to switch to prior cdlog context.\n"
+ fi
fi
# Also init var nicknames, but don't save context
@@ -102,12 +109,19 @@ cdlog.recover()
local i
local sel
local csd=$cdlog_sess_dir
+ local auto=$1
+ local print=
mkdir -p "$csd"
set -- "$csd"/.cdlog.*.dirs
-
- if [ $# -gt 0 -a $1 != "$csd"/.cdlog.'*'.dirs ] ; then
+ if [ $# -eq 1 -a $1 == "$csd"/.cdlog.'*'.dirs ] ; then
+ : # nothing
+ elif [ $# -eq 1 -a "$auto" == __auto ] ; then
+ printf "Auto-recovering cdlog session 1.\n"
+ cdlog_dirs=$(cdlog.get_param 1 "$@")
+ print=y
+ else
printf "These cdlog contexts exist in %s:\n" "$csd"
i=0
for dirs in "$@"; do
@@ -166,6 +180,8 @@ cdlog.recover()
unset cdlog_hist[0]
cdlog.update
fi
+
+ [ $print ] && printf "%s\n" "$PWD"
}
# Resolve @ search item to number