+## directory history tracking and navigation.
+#
+# cd becomes a function, also aliased to c. b to go back, f to go
+# forward, cl to list recent directories and choose one.
+#
+# The finer details you may want to skip:
+#
+# We also define bl to print the list of back and forward directories.
+#
+# We keep 2 stacks, forward and back. Unlike with a web browser, the
+# forward stack is not erased when going somewhere new.
+#
+# Recent directories are stored in ~/.cdirs.
+#
+declare -a _dir_forward _dir_back
+c() {
+ # normally, the top of _dir_back is our current dir. if it isn't,
+ # put it on there, except we don't want to do that when we
+ # just launched a shell
+ if [[ $OLDPWD ]]; then
+ if (( ${#_dir_back[@]} == 0 )) || [[ ${_dir_back[-1]} != "$PWD" ]]; then
+ _dir_back+=("$PWD")
+ fi
+ fi
+ command cd "$@"
+ if (( ${#_dir_back[@]} == 0 )) || [[ ${_dir_back[-1]} != "$PWD" ]]; then
+ _dir_back+=("$PWD")
+ fi
+ echo "$PWD" >> ~/.cdirs
+}
+ccomp cd c
+
+# back
+b() {
+ local top_back
+ if (( ${#_dir_back[@]} == 0 )); then
+ echo "nothing left to go back to" >&2
+ return 0
+ fi
+ top_back="${_dir_back[-1]}"
+
+ if [[ $top_back == "$PWD" ]] && (( ${#_dir_back[@]} == 1 )); then
+ echo "already on last back entry" >&2
+ return 0
+ fi
+
+
+ if [[ $top_back == "$PWD" ]]; then
+ # add to dirf if not already there
+ if (( ${#_dir_forward[@]} == 0 )) || [[ ${_dir_forward[-1]} != "$top_back" ]]; then
+ _dir_forward+=("$top_back")
+ fi
+ unset "_dir_back[-1]"
+ command cd "${_dir_back[-1]}"
+ else
+ if (( ${#_dir_forward[@]} == 0 )) || [[ ${_dir_forward[-1]} != "$PWD" ]]; then
+ _dir_forward+=("$PWD")
+ fi
+ command cd "$top_back"
+ fi
+
+ # Interesting feature, not sure I want it.
+ # give us a peek at what is next in the list
+ # if (( ${#_dir_back[@]} >= 2 )); then
+ # printf "%s\n" "${_dir_back[-2]}"
+ # fi
+ #
+
+ # c/b/f Implementation notes:
+ #
+ # The top of the back is $PWD
+ # as long as the last directory change was due to c,b,or cl.
+ #
+ # Example of stack changes:
+ #
+ # a b c (d)
+ ## back
+ # a b (c)
+ # d
+ #back
+ #a (b)
+ #d c
+ #back
+ #(a)
+ #d c b
+ #forward
+ #a (b)
+ #d c
+ #
+ # a b c
+ ## back
+ # a b
+ # (c)
+ ## forward
+
+}
+# forward
+f() {
+ local top_forward
+ if (( ${#_dir_forward[@]} == 0 )); then
+ echo "no forward dir left" >&2
+ return 0
+ fi
+ top_forward="${_dir_forward[-1]}"
+ unset "_dir_forward[-1]"
+ c "$top_forward"
+
+ # give us a peek at what is next in the list
+ # if (( ${#_dir_forward[@]} )); then
+ # printf "%s\n" "${_dir_forward[-1]}"
+ # fi
+}
+# cd list
+cl() {
+ local i line input start
+ local -A buttondirs alines
+ local -a buttons dirs lines
+ buttons=( {a..z} {2..9} )
+ if [[ ! -s ~/.cdirs ]]; then
+ echo nothing in ~/.cdirs
+ return 0
+ fi
+
+ i=0
+
+ mapfile -t lines <~/.cdirs
+ start=$(( ${#lines[@]} - 1 ))
+
+ # we have ~33 buttons as of this writing, so lets
+ # prune down the history every once in a while.
+ if (( start > 500 )); then
+ tac ~/.cdirs | awk '!seen[$0]++' | head -n 200 | tac | sponge ~/.cdirs || [[ $? == 141 ]]
+ fi
+
+ for (( j=start; j >= 0; j-- )); do
+ line="${lines[$j]}"
+ if [[ ! $line || ${alines[$line]} || ! -d "$line" || $line == "$PWD" || line == "$HOME" ]]; then
+ continue
+ fi
+ alines[$line]=t
+ buttondirs[${buttons[i]}]="$line"
+ printf "%s %s\n" ${buttons[i]} "$line"
+ if (( i == ${#buttons[@]} - 1 )); then
+ break
+ fi
+ i=$(( i + 1 ))
+ done
+
+ if (( i == 0 )); then
+ echo "no dirs in ~/.cdirs"
+ return 0
+ fi
+ read -r -N 1 input
+ if [[ $input != $'\n' ]]; then
+ c "${buttondirs[$input]}"
+ fi
+}
+# back list
+bl() {
+ local start i j max
+ max=10
+ start=$(( ${#_dir_back[@]} - 1 ))
+
+ # cleanup possible repeating of pwd
+ if (( start >= 0 )) && [[ ${_dir_back[$start]} == "$PWD" ]]; then
+ start=$(( start - 1 ))
+ fi
+ j=1
+ if (( start >= 0 )); then
+ for (( i=start; i >= 0 ; i-- )); do
+ printf "%s %s\n" $j ${_dir_back[i]}
+ j=$(( j + 1 ))
+ if (( j >= max )); then
+ break
+ fi
+ done
+ fi
+
+ max=10
+ start=$(( ${#_dir_forward[@]} - 1 ))
+
+ # cleanup possible repeating of pwd
+ if (( start >= 0 )) && [[ ${_dir_forward[$start]} == "$PWD" ]]; then
+ start=$(( start - 1 ))
+ fi
+ if (( start < 0 )); then
+ return 0
+ fi
+ echo --
+ j=1
+ for (( i=start; i >= 0 ; i-- )); do
+ printf "%s %s\n" $j ${_dir_forward[i]}
+ j=$(( j + 1 ))
+ if (( j >= max )); then
+ break
+ fi
+ done
+}
+
+# pee do. run args as a command with output copied to syslog.
+#
+# Usage: pd [-t TAG] COMMAND...
+#
+# -t TAG Override the tag in the syslog. The default is COMMAND with
+# any path part is removed, eg. for /bin/cat the tag is cat.
+#
+# You can view the log via "journalctl -t TAG"
+pd() {
+ local tag ret
+ ret=0
+ tag=${1##*/}
+ case $1 in
+ -t) tag="$2"; shift 2 ;;
+ esac
+ echo "PWD=$PWD command: $*" | logger -t $tag
+ "$@" |& pee cat "logger -t $tag" || ret=$?
+ echo "exited with status=$ret" | pee cat "logger -t $tag"
+ # this avoids any err-catch
+ (( ret == 0 )) || return $ret
+}
+ccomp time pd
+
+# jdo = journal do. Run command as transient systemd service, tailing
+# its output in the journal until it completes.
+#
+# Usage: jdo COMMAND...
+#
+# Compared to pd: commands recognize this is a non-interactive shell.
+# The service is unaffected if our ssh connection dies, no need to run
+# in screen or tmux.
+#
+# Note: The last few lines of any existing entries for a unit by that
+# name will be output first, and there will be a few second delay at the
+# start of the command, and a second or so at the end.
+#
+# Note: Functions and aliases obviously won't work, we resolve the
+# command to a file.
+#
+# Note: requires running as root.
+jdo() {
+ local cmd cmd_name jr_pid ret
+ ret=0
+ cmd="$1"
+ shift
+ if [[ $EUID != 0 ]]; then
+ echo "jdo: error: rerun as root"
+ return 1
+ fi
+ cmd_name=${cmd##*/}
+ if [[ $cmd != /* ]]; then
+ cmd=$(type -P "$cmd")
+ fi
+ # -q = quiet
+ journalctl -qn2 -f -u "$cmd_name" &
+ # Trial and error of time needed to avoid missing initial lines.
+ # .5 was not reliable. 1 was not reliable. 2 was not reliable
+ sleep 4
+ jr_pid=$!
+ systemd-run --unit "$cmd_name" --wait --collect "$cmd" "$@" || ret=$?
+ # The sleep lets the journal output its last line
+ # before the prompt comes up.
+ sleep .5
+ kill $jr_pid &>/dev/null ||:
+ unset jr_pid
+ fg &>/dev/null ||:
+ # this avoids any err-catch
+ (( ret == 0 )) || return $ret
+}
+ccomp time jdo
+#### end fsf section
+