aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-02-25 17:42:06 -0800
committerKaz Kylheku <kaz@kylheku.com>2024-02-25 17:42:06 -0800
commitfabb4c15215546fd36a461c8956dc310a4f35ac0 (patch)
treea7ec46c91ff415c2d26df2fc02a350b53f9a51f5
parent12fc4e20184060d3344b72b047dc31dc44065610 (diff)
downloadcdlog-fabb4c15215546fd36a461c8956dc310a4f35ac0.tar.gz
cdlog-fabb4c15215546fd36a461c8956dc310a4f35ac0.tar.bz2
cdlog-fabb4c15215546fd36a461c8956dc310a4f35ac0.zip
Use global array for FIFO.
The $c1 to $c9 variables are now just derived items. * cdlog.sh (cdlog_fifo): New array variable. (cdlog.args): Now all this function does is copy from the array to the short variables. (cdlog.update): Now calls cdlog.args to copy ${cdlog_fifo[@]} to $c1 to $c9. (cdlog.init): Initialize dclog_fifo via ${d[]} alias, and call cdlog.args to also init the short variables. (cdlog.recover): Map from file directly into cdlog_fifo array, and call cdlog.update instead of cdlog.args. (cdlog.chdir, cdlog.rot, cdlog.pop): Local array d is alias for cdlog_fifo. Use cdlog.update instead of cdlog.args. (cdlog.mcd): Local array d is alias for cdlog_fifo. * README.md: Don't say that the FIFO is stored in variables c1 through c9 but in in the cdlog_fifo array. The variables are now for convenience only.
-rw-r--r--README.md8
-rw-r--r--cdlog.sh79
2 files changed, 43 insertions, 44 deletions
diff --git a/README.md b/README.md
index 2a248f1..99a1fd8 100644
--- a/README.md
+++ b/README.md
@@ -14,10 +14,10 @@ The user interface consists of three commands:
* `cd` is now an alias which calls the function `cdlog.chdir`.
Every time you change directory, it pushes the previous directory
-into a FIFO log. The log maintains nine entries. The ninth entry
-is erased. The entries are stored in the variables `c1` through `c9`.
-The first four entries are also copied into the variables `x`, `y`,
-`z` and `w` for shorter access.
+into a FIFO log which is stored in the `cdlog_fifo` array. The log maintains
+nine entries. The ninth entry is erased. The entries are copied into
+the variables `c1` through `c9`. The first four entries are also
+copied into the variables `x`, `y`, `z` and `w` for shorter access.
* `cs` (cd swap) is an alias for a command which exchanges
the current directory with a selected `cslog` entry selected
diff --git a/cdlog.sh b/cdlog.sh
index fd1b220..4c9fb91 100644
--- a/cdlog.sh
+++ b/cdlog.sh
@@ -1,19 +1,31 @@
# License at bottom.
# Globals
+declare -a cdlog_fifo # directory FIFO
declare -A cdlog_alias # cd aliases
+unset cdlog_alias[0] # FIFO is 1-based
+
# Configuration variables
cdlog_lru=${cdlog_lru-}
-# Update after FIFO change.
-cdlog.update()
+# Set state from args
+cdlog.args()
{
- # Update the one-letter nicknames for most recent four dirs.
+ c1=$1; c2=$2; c3=$3
+ c4=$4; c5=$5; c6=$6
+ c7=$7; c8=$8; c9=$9
+
x=$c1
y=$c2
z=$c3
w=$c4
+}
+
+# Update after FIFO change.
+cdlog.update()
+{
+ cdlog.args "${cdlog_fifo[@]}"
# Persist to disk.
cat > "$cdlog_dirs" <<!
@@ -30,16 +42,6 @@ $c9
!
}
-# Set state from args
-cdlog.args()
-{
- c1=$1; c2=$2; c3=$3
- c4=$4; c5=$5; c6=$6
- c7=$7; c8=$8; c9=$9
-
- cdlog.update
-}
-
# Utility for nth positional param
cdlog.get_param()
{
@@ -55,10 +57,11 @@ cdlog.init()
local i
local oldest
local context
+ declare -n d=cdlog_fifo
- c9=; c8=; c7=
- c6=; c5=; c4=
- c3=; c2=
+ d[9]=; d[8]=; d[7]=;
+ d[6]=; d[5]=; d[4]=;
+ d[3]=; d[2]=
cdlog_dirs=
context=
@@ -87,11 +90,13 @@ cdlog.init()
if [ $# -gt 0 -a $1 != ~/.cdlog.'*'.dirs ] ; then
printf "Use 'cdr' or 'cdlog.recover' to switch to prior cdlog context.\n"
fi
+
+ # Also init var nicknames, but don't save context
+ cdlog.args "${d[@]}"
}
cdlog.recover()
{
- local -a d
local dir
local i
local sel
@@ -137,10 +142,10 @@ cdlog.recover()
fi
if [ -f "$cdlog_dirs" ] ; then
- mapfile -t d < "$cdlog_dirs"
- command cd "${d[0]}"
- unset d[0]
- cdlog.args "${d[@]}"
+ mapfile -t cdlog_fifo < "$cdlog_dirs"
+ command cd "${cdlog_fifo[0]}"
+ unset cdlog_fifo[0]
+ cdlog.update
fi
}
@@ -167,12 +172,12 @@ cdlog.chdir()
# only if we successfully change to a different
# directory do the following
+ declare -n d=cdlog_fifo
+
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]}
@@ -182,17 +187,15 @@ cdlog.chdir()
fi
pv=$nx
done
-
- cdlog.args "${d[@]}"
else
- c9=$c8; c8=$c7; c7=$c6
- c6=$c5; c5=$c4; c4=$c3
- c3=$c2; c2=$c1
-
- c1=$cur
+ for ((i = 8; i >= 2; i--)); do
+ d[$((i+1))]=d[$i]
+ done
- cdlog.update
+ d[1]=$cur
fi
+
+ cdlog.update
fi
}
@@ -202,11 +205,9 @@ cdlog.rot()
local cur=$PWD
local p=$cur
local n=$cur
- local d=("" "$c1" "$c2" "$c3" "$c4" "$c5" "$c6" "$c7" "$c8" "$c9")
+ declare -n d=cdlog_fifo
local first=y
- unset d[0]
-
[ $# -gt 0 ] || set 1
while [ $# -gt 0 ] ; do
@@ -231,7 +232,7 @@ cdlog.rot()
d[$p]=$cur
- cdlog.args "${d[@]}"
+ cdlog.update
}
@@ -239,12 +240,10 @@ cdlog.rot()
# from the log.
cdlog.pop()
{
- local d=("" "$c1" "$c2" "$c3" "$c4" "$c5" "$c6" "$c7" "$c8" "$c9")
+ declare -n d=cdlog_fifo
local n=1
local i
- unset d[0]
-
if [ $# -gt 0 ] ; then
n=$1; shift
fi
@@ -256,7 +255,7 @@ cdlog.pop()
d[$i]=${d[$((i + 1))]}
done
d[9]=
- cdlog.args "${d[@]}"
+ cdlog.update
printf "%s\n" "$PWD"
fi
;;
@@ -291,7 +290,7 @@ cdlog.mcd()
local swap=
local res=1
local i
- local d=("" "$c1" "$c2" "$c3" "$c4" "$c5" "$c6" "$c7" "$c8" "$c9")
+ declare -n d=cdlog_fifo
if [ $# -gt 0 ] && [ "$1" = -s ] ; then
swap=y