aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-02-23 11:53:25 -0800
committerKaz Kylheku <kaz@kylheku.com>2024-02-23 11:53:25 -0800
commit8629957d6c727d3db36123dcc5675c4fd661f680 (patch)
tree2f7b2ba7963a1fb848b08a457ec4534869a24448
parent519518a70a7cab03672f897e366c188a68b5c63f (diff)
downloadcdlog-8629957d6c727d3db36123dcc5675c4fd661f680.tar.gz
cdlog-8629957d6c727d3db36123dcc5675c4fd661f680.tar.bz2
cdlog-8629957d6c727d3db36123dcc5675c4fd661f680.zip
New, LRU mode; better than removed autocs feature.
* cdlog.sh (cdlog_lru): New Boolean configuration variable, blank/false by default. (cdlog.chdir): If cdlog_lru is true, perform the rotation through to the most recent occurrence of the new directory. * README.md: Documented.
-rw-r--r--README.md18
-rw-r--r--cdlog.sh33
2 files changed, 45 insertions, 6 deletions
diff --git a/README.md b/README.md
index 248ff71..8c54ac4 100644
--- a/README.md
+++ b/README.md
@@ -87,6 +87,24 @@ 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.
+## LRU Mode
+
+When the configuration variable `cdlog_lru` is set to the value `y`, LRU mode
+is enabled in the `cdlog.chdir` function, affecting the `cd` and `mcd`
+commands. LRU mode modifies the FIFO algorithm in the following way.
+
+If the directory change successfully takes place to a directory which
+already exists in the FIFO, then the most recent (topmost) occurrence
+of that directory is removed from the FIFO before the previous
+directory is pushed into the FIFO. Thus, in effect, a rotation is
+taking place: the existing entry in the FIFO becomes the current
+directory, and is removed; the entries above it shift down to take
+its place, and the previous current directory becomes the top of
+the FIFO.
+
+LRU mode keeps duplicate directories out of the FIFO and while
+promoting recently used directories toward the top.
+
## How is this better?
* It's not hard-coded C code inside the shell; it consists of a small amount of
diff --git a/cdlog.sh b/cdlog.sh
index b712b2e..b7d814d 100644
--- a/cdlog.sh
+++ b/cdlog.sh
@@ -3,6 +3,9 @@
# Globals
declare -A cdlog_alias # cd aliases
+# Configuration variables
+cdlog_lru=${cdlog_lru-}
+
# Update after FIFO change.
cdlog.update()
{
@@ -145,7 +148,6 @@ cdlog.recover()
cdlog.chdir()
{
local cur=$PWD
- local i
local def
if [ $# -eq 2 -a "$1" = -P ] ; then
@@ -165,13 +167,32 @@ cdlog.chdir()
# only if we successfully change to a different
# directory do the following
- c9=$c8; c8=$c7; c7=$c6
- c6=$c5; c5=$c4; c4=$c3
- c3=$c2; c2=$c1
+ if [ $cdlog_lru ] ; then
+ local nx=
+ local pv=$cur
+ local i
+ local d=("" "$c1" "$c2" "$c3" "$c4" "$c5" "$c6" "$c7" "$c8" "$c9")
+ unset d[0]
+
+ for ((i = 1; i <= 9; i++)); do
+ nx=${d[$i]}
+ d[$i]=$pv
+ if [ "$nx" = "$PWD" ] ; then
+ break
+ fi
+ pv=$nx
+ done
- c1=$cur
+ cdlog.args "${d[@]}"
+ else
+ c9=$c8; c8=$c7; c7=$c6
+ c6=$c5; c5=$c4; c4=$c3
+ c3=$c2; c2=$c1
- cdlog.update
+ c1=$cur
+
+ cdlog.update
+ fi
fi
}