diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2025-01-14 23:22:29 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2025-01-14 23:22:29 -0800 |
commit | de071dc3ac00a2504ce238efd86fff34a0338051 (patch) | |
tree | de0e1db4fcb6f8de52bb2a26d6a8514dee2352f8 | |
parent | 0bbe75fd3b73c309df0f1c76b38b7692f983cd19 (diff) | |
download | cdlog-de071dc3ac00a2504ce238efd86fff34a0338051.tar.gz cdlog-de071dc3ac00a2504ce238efd86fff34a0338051.tar.bz2 cdlog-de071dc3ac00a2504ce238efd86fff34a0338051.zip |
Make the directory holding session files configurable
* cdlog.sh (cdlog_sess_dir): New variable.
(cdlog.init, cdlog.recover): Create the session directory, if
necessary. Interpolate the variable into all the places where
we previously referenced the home directory ~.
In cdlog.recover print that directory so the user knows
where the sessions are coming from.
* README.md: Documented.
-rw-r--r-- | README.md | 14 | ||||
-rw-r--r-- | cdlog.sh | 23 |
2 files changed, 29 insertions, 8 deletions
@@ -157,6 +157,20 @@ the history. LRU mode keeps duplicate directories out of the history and while promoting recently used directories toward the top. +## Alternative Session Directory + +By default, `cdlog` keeps the session recovery files in your home +directory. They have numbered names like `.cdlog.1.dirs`. +The variable `cdlog_sess_dir` may be assigned a directory path +to specify an alternative directory. This path should omit the +trailing slash. If the variable is empty, it denotes the +root directory. If the directory doesn't exist, `cdlog` will try +to create it. The main use for this configuration is to support +the situation when a home directory is shared among multiple +host machines with different environments. You can interpolate the +value of `$HOSTNAME` into `cdlog_sess_dir` to have sessions specific +to a host. + ## Completion `cdlog` provides its own Tab completion for the `cd` command, overriding @@ -11,6 +11,7 @@ unset cdlog_alias[0] # history is 1-based # Configuration variables cdlog_lru=${cdlog_lru-} +cdlog_sess_dir=${cdlog_sess_dir-~} # Set state from args cdlog.args() @@ -53,8 +54,11 @@ cdlog.init() local i local oldest local context + local csd=$cdlog_sess_dir declare -n d=cdlog_hist + mkdir -p "$csd" + for ((i = 0; i < ${#d[@]}; i++)); do unset d[$i] done @@ -63,7 +67,7 @@ cdlog.init() context= for i in {1..10}; do - if ! [ -f ~/.cdlog.$i.dirs ] ; then + if ! [ -f "$csd"/.cdlog.$i.dirs ] ; then context=$i break fi @@ -72,19 +76,19 @@ cdlog.init() if ! [ $context ] ; then oldest=1 for i in {2..10}; do - if [ ~/.cdlog.$i.dirs -ot ~/.cdlog.$oldest.dirs ] ; then + if [ "$csd"/.cdlog.$i.dirs -ot "$csd"/.cdlog.$oldest.dirs ] ; then oldest=$i fi done context=$oldest fi - cdlog_dirs=~/.cdlog.$context.dirs + cdlog_dirs="$csd"/.cdlog.$context.dirs cdlog_new_dirs=$cdlog_dirs rm -f $cdlog_dirs - set -- ~/.cdlog.*.dirs - if [ $# -gt 0 -a $1 != ~/.cdlog.'*'.dirs ] ; then + 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" fi @@ -97,11 +101,14 @@ cdlog.recover() local dirs local i local sel + local csd=$cdlog_sess_dir + + mkdir -p "$csd" - set -- ~/.cdlog.*.dirs + set -- "$csd"/.cdlog.*.dirs - if [ $# -gt 0 -a $1 != ~/.cdlog.'*'.dirs ] ; then - printf "These cdlog contexts exist:\n" + if [ $# -gt 0 -a $1 != "$csd"/.cdlog.'*'.dirs ] ; then + printf "These cdlog contexts exist in %s:\n" "$csd" i=0 for dirs in "$@"; do printf "[%d]: %s%s\n" $((++i)) \ |