diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2024-02-25 18:18:51 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2024-02-25 18:29:28 -0800 |
commit | aaaf88bf43bfc6730cd11d43e088b72391a8c968 (patch) | |
tree | 7706de26d10fa8496e35ee31e26236cbd7647beb | |
parent | fabb4c15215546fd36a461c8956dc310a4f35ac0 (diff) | |
download | cdlog-aaaf88bf43bfc6730cd11d43e088b72391a8c968.tar.gz cdlog-aaaf88bf43bfc6730cd11d43e088b72391a8c968.tar.bz2 cdlog-aaaf88bf43bfc6730cd11d43e088b72391a8c968.zip |
Allow unlimited internal history.
* cdlog.sh (cdlog_fifo): Renamed to cdlog_hist.
* README.md: All references to FIFO replaced with "history".
Claims that the FIFO is nine elements long are removed.
(cdlog.update): Save the entire array, not relying on
the nine $c1 through $c9 variables.
(cdlog.init): Reinitialize by deleting the elements of
the cdlog_hist array.
(cdlog.recover): Print only up to 10 entries from the
files.
(cdlog.chdir): Remove array limits; let it grow.
(cdlog.pop): Pop the entire array, not just nine
entries. Unset the array entry rather than blanking it.
-rw-r--r-- | README.md | 24 | ||||
-rw-r--r-- | cdlog.sh | 52 |
2 files changed, 35 insertions, 41 deletions
@@ -14,7 +14,7 @@ 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 which is stored in the `cdlog_fifo` array. The log maintains +into a history 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. @@ -40,7 +40,7 @@ log, or all nine if given the `-l` argument. The `cl` command is an alias for for `cdlog` with no arguments and `cll` for `cdlog -l`. * The `mcd` and `mcs` are menu-selection-based analogs of `cd` and `mcs`. -They print the contents of the FIFO (all nine entries), and you can +They print the contents of the history (first nine entries), and you can pick which directory to change to or swap with. The terminal cursor is then retraced back to the top of the menu, and the screen is erased from that point to the bottom. @@ -57,7 +57,7 @@ With the `direxpand` option, Tab completion on a directory coming from a variable name will expand that variable into the command line. The directory changing commands do nothing if you attempt to change into the -same directory you are already in. There is no effect on the FIFO. +same directory you are already in. There is no effect on the history. ## Aliases @@ -74,8 +74,8 @@ it is invoked as `cdlog.chdir -p <alias>`, which is the way ## Persistence -Whenever the nine-element FIFO changes, the current directory and the -contents of the FIFO are written to the file `~/.cslog.N.dirs`, +Whenever the history changes, the current directory and the +contents of the history are written to the file `~/.cslog.N.dirs`, one path per line, where `N` is an internal session number. When `cdlog` initializes, it allocates a session by finding a free @@ -91,18 +91,18 @@ to one of the sessions. 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. +commands. LRU mode modifies the history 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 +already exists in the history, then the most recent (topmost) occurrence +of that directory is removed from the history before the previous +directory is pushed into the history. Thus, in effect, a rotation is +taking place: the existing entry in the history 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. +the history. -LRU mode keeps duplicate directories out of the FIFO and while +LRU mode keeps duplicate directories out of the history and while promoting recently used directories toward the top. ## Completion @@ -1,10 +1,10 @@ # License at bottom. # Globals -declare -a cdlog_fifo # directory FIFO +declare -a cdlog_hist # directory history declare -A cdlog_alias # cd aliases -unset cdlog_alias[0] # FIFO is 1-based +unset cdlog_alias[0] # history is 1-based # Configuration variables cdlog_lru=${cdlog_lru-} @@ -22,23 +22,16 @@ cdlog.args() w=$c4 } -# Update after FIFO change. +# Update after history change. cdlog.update() { - cdlog.args "${cdlog_fifo[@]}" + declare -n d=cdlog_hist + cdlog.args "${d[@]}" # Persist to disk. cat > "$cdlog_dirs" <<! $PWD -$c1 -$c2 -$c3 -$c4 -$c5 -$c6 -$c7 -$c8 -$c9 +$(printf "%s\n" "${d[@]}") ! } @@ -57,11 +50,11 @@ cdlog.init() local i local oldest local context - declare -n d=cdlog_fifo + declare -n d=cdlog_hist - d[9]=; d[8]=; d[7]=; - d[6]=; d[5]=; d[4]=; - d[3]=; d[2]= + for ((i = 0; i < ${#d[@]}; i++)); do + unset d[$i] + done cdlog_dirs= context= @@ -108,7 +101,7 @@ cdlog.recover() i=0 for dir in "$@"; do printf "[%d]: %s\n" $((++i)) "$(date -r "$dir")" - sed -e '/^$/d' "$dir" + sed -e '/^$/d' "$dir" | head -9 done while true; do @@ -142,9 +135,9 @@ cdlog.recover() fi if [ -f "$cdlog_dirs" ] ; then - mapfile -t cdlog_fifo < "$cdlog_dirs" - command cd "${cdlog_fifo[0]}" - unset cdlog_fifo[0] + mapfile -t cdlog_hist < "$cdlog_dirs" + command cd "${cdlog_hist[0]}" + unset cdlog_hist[0] cdlog.update fi } @@ -172,14 +165,15 @@ cdlog.chdir() # only if we successfully change to a different # directory do the following - declare -n d=cdlog_fifo + declare -n d=cdlog_hist if [ $cdlog_lru ] ; then local nx= local pv=$cur local i + local n1=$((${#d[@]} + 1)) - for ((i = 1; i <= 9; i++)); do + for ((i = 1; i <= n1; i++)); do nx=${d[$i]} d[$i]=$pv if [ "$nx" = "$PWD" ] ; then @@ -188,7 +182,7 @@ cdlog.chdir() pv=$nx done else - for ((i = 8; i >= 2; i--)); do + for ((i = ${#d[@}}; i >= 2; i--)); do d[$((i+1))]=d[$i] done @@ -205,7 +199,7 @@ cdlog.rot() local cur=$PWD local p=$cur local n=$cur - declare -n d=cdlog_fifo + declare -n d=cdlog_hist local first=y [ $# -gt 0 ] || set 1 @@ -240,7 +234,7 @@ cdlog.rot() # from the log. cdlog.pop() { - declare -n d=cdlog_fifo + declare -n d=cdlog_hist local n=1 local i @@ -251,10 +245,10 @@ cdlog.pop() case $n in ( [1-9] ) if command cd "${d[$n]}" ; then - for ((i = n; i <= 8; i++)); do + for ((i = n; i < ${#d[@]}; i++)); do d[$i]=${d[$((i + 1))]} done - d[9]= + unset d[$i] cdlog.update printf "%s\n" "$PWD" fi @@ -290,7 +284,7 @@ cdlog.mcd() local swap= local res=1 local i - declare -n d=cdlog_fifo + declare -n d=cdlog_hist if [ $# -gt 0 ] && [ "$1" = -s ] ; then swap=y |