From fabb4c15215546fd36a461c8956dc310a4f35ac0 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 25 Feb 2024 17:42:06 -0800 Subject: 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. --- README.md | 8 +++---- cdlog.sh | 79 +++++++++++++++++++++++++++++++-------------------------------- 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" <= 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 -- cgit v1.2.3