Skip to content

Commit

Permalink
Merge pull request #26 from s-hamann/configurable_history
Browse files Browse the repository at this point in the history
Make history file configurable
  • Loading branch information
Biont authored Sep 22, 2020
2 parents 889c3b6 + 343ab8c commit 9859390
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ Note: Pass the environment variable `PROVIDERS_FILE` to read custom providers fr
The path in `PROVIDERS_FILE` can either be absolute or relative to `${HOME}/.config/sway-launcher-desktop/`.


## Launcher history file

By default, `sway-launcher-desktop` stores a history of commands to make frequently used entries available more quickly.
This history is stored in a file in `~/.cache/` (or `$XDG_CACHE_HOME`, if that environment variable is set).
You may change the file path and name by setting the environment variable `HIST_FILE` to the desired path.
Setting the variable to an empty value disables the history feature entirely.

## Troubleshooting

Debug information is directed to file descriptor `3` and can be dumped using `./sway-launcher-desktop.sh 3>> ~/sway-launcher-desktop.log`
40 changes: 24 additions & 16 deletions sway-launcher-desktop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,21 @@ if [ -f "${PROVIDERS_FILE}" ]; then
print "PROVIDERS[\x27" key "\x27]=\x27" providers[key]["list_cmd"] "\034" providers[key]["preview_cmd"] "\034" providers[key]["launch_cmd"] "\x27\n"
}
}' "${PROVIDERS_FILE}")"
HIST_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/${0##*/}-${PROVIDERS_FILE##*/}-history.txt"
if [[ ! -v HIST_FILE ]]; then
HIST_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/${0##*/}-${PROVIDERS_FILE##*/}-history.txt"
fi
else
PROVIDERS['desktop']="${0} list-entries${DEL}${0} describe-desktop \"{1}\"${DEL}${0} run-desktop '{1}' {2}"
PROVIDERS['command']="${0} list-commands${DEL}${0} describe-command \"{1}\"${DEL}${TERMINAL_COMMAND} {1}"
HIST_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/${0##*/}-history.txt"
if [[ ! -v HIST_FILE ]]; then
HIST_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/${0##*/}-history.txt"
fi
fi

mkdir -p "${HIST_FILE%/*}" && touch "$HIST_FILE"
readarray HIST_LINES <"$HIST_FILE"
if [[ -n "${HIST_FILE}" ]]; then
mkdir -p "${HIST_FILE%/*}" && touch "$HIST_FILE"
readarray HIST_LINES <"$HIST_FILE"
fi

function describe() {
# shellcheck disable=SC2086
Expand Down Expand Up @@ -258,20 +264,22 @@ COMMAND_STR=$(

[ -z "$COMMAND_STR" ] && exit 1

# update history
for i in "${!HIST_LINES[@]}"; do
if [[ "${HIST_LINES[i]}" == *" $COMMAND_STR"$'\n' ]]; then
HIST_COUNT=${HIST_LINES[i]%% *}
HIST_LINES[$i]="$((HIST_COUNT + 1)) $COMMAND_STR"$'\n'
match=1
break
if [[ -n "${HIST_FILE}" ]]; then
# update history
for i in "${!HIST_LINES[@]}"; do
if [[ "${HIST_LINES[i]}" == *" $COMMAND_STR"$'\n' ]]; then
HIST_COUNT=${HIST_LINES[i]%% *}
HIST_LINES[$i]="$((HIST_COUNT + 1)) $COMMAND_STR"$'\n'
match=1
break
fi
done
if ! ((match)); then
HIST_LINES+=("1 $COMMAND_STR"$'\n')
fi
done
if ! ((match)); then
HIST_LINES+=("1 $COMMAND_STR"$'\n')
fi

printf '%s' "${HIST_LINES[@]}" | sort -nr >"$HIST_FILE"
printf '%s' "${HIST_LINES[@]}" | sort -nr >"$HIST_FILE"
fi

# shellcheck disable=SC2086
readarray -d $'\034' -t PARAMS <<<${COMMAND_STR}
Expand Down

0 comments on commit 9859390

Please sign in to comment.