! Use new XDG Base specification for state

Switched the history from using the XDG_CACHE_HOME directory by default
to use XDG_STATE_HOME by default.

This makes sense since cache can (and should be prepared to) be wiped at
any moment and the program functionality should not be hindered by this.

Since we need to retain history through such wipes the newly introduced
state directory is the perfect match for keeping the history file in.

This does constitute a small breaking change for existing histories
which need to be moved to the new directory if they made use of the old
cache directory.

Fixes #5.
This commit is contained in:
Marty Oehme 2022-10-12 16:56:15 +02:00
parent 16039fe676
commit 250fb36146
Signed by: Marty
GPG Key ID: 73BA40D5AFAF49C9
3 changed files with 11 additions and 6 deletions

View File

@ -14,6 +14,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Multiple command options can be combined
- History uses `XDG_STATE_HOME` directory by default:
This constitutes a slight break in behavior if you relied a lot on your pick history in the
default location. To retain your old history file, simply move it from the old cache directory
(`~/.cache/bemoji-history.txt` by default) to the new one (`~/.local/state/bemoji-history.txt`
by default).
<!-- ### Deprecated -->

8
bemoji
View File

@ -7,8 +7,8 @@ bm_db_location=${BEMOJI_DB_LOCATION:-"${XDG_DATA_HOME:-$HOME/.local/share}/bemoj
# BEMOJI_CUSTOM_LIST=/my/location/emojis.txt
# Setting custom recent emoji cache:
# BEMOJI_CACHE_LOCATION=/path/to/my/recents/directory
bm_cache_dir="${BEMOJI_CACHE_LOCATION:-${XDG_CACHE_HOME:-$HOME/.cache}}"
bm_history_file="${bm_cache_dir}/bemoji-history.txt"
bm_state_dir="${BEMOJI_CACHE_LOCATION:-${XDG_STATE_HOME:-$HOME/.local/state}}"
bm_history_file="${bm_state_dir}/bemoji-history.txt"
# Command to run after user chooses an emoji
bm_default_cmd="$BEMOJI_DEFAULT_CMD"
@ -135,8 +135,8 @@ get_most_recent() {
add_to_recent() {
if [ -z "$1" ]; then return; fi
if [ ! -d "$bm_cache_dir" ]; then
mkdir -p "$bm_cache_dir"
if [ ! -d "$bm_state_dir" ]; then
mkdir -p "$bm_state_dir"
fi
echo "$1" >>"$bm_history_file"
}

View File

@ -39,7 +39,7 @@ database=$BATS_TEST_TMPDIR/xdb-db/bemoji
@test "sets XDG directory for history by default" {
unset BEMOJI_CACHE_LOCATION
export XDG_CACHE_HOME="$BATS_TEST_TMPDIR/xdb-cache"
export XDG_STATE_HOME="$BATS_TEST_TMPDIR/xdb-cache"
run bemoji -v
assert_output --regexp "
history=$BATS_TEST_TMPDIR/xdb-cache/bemoji-history.txt$"
@ -57,7 +57,7 @@ database=$HOME/.local/share/bemoji
unset BEMOJI_CACHE_LOCATION
run bemoji -v
assert_output --regexp "
history=$HOME/.cache/bemoji-history.txt$"
history=$HOME/.local/state/bemoji-history.txt$"
}
@test "BEMOJI_DB_LOCATION sets correct db directory" {