zsh:alias: Turn tmsu into a smarter function

Instead of simply pointing the database to XDG_DATA_HOME, write a
wrapper function that searches for `.tmsu/db` in all parent directories
and fallbacks to XDG_DATA_HOME if not found.

Also create XDG_DATA_HOME/tmsu if necessary.
This commit is contained in:
2022-09-18 19:33:51 +02:00
parent 2177031c68
commit 553a2679cf
2 changed files with 25 additions and 1 deletions

View File

@@ -44,7 +44,6 @@ fi
# XDG Base Directory Specification
add_flags tmux -f "${XDG_CONFIG_HOME:-$HOME/.config}/tmux/tmux.conf"
add_flags tmsu -D "${XDG_DATA_HOME:-$HOME/.local/share}/tmsu/db"
add_flags yarn --use-yarnrc "${XDG_CONFIG_HOME:-$HOME/.config}"/yarn/config
add_flags bash --rcfile "${XDG_CONFIG_HOME:-$HOME/.config}"/bash/bashrc
add_flags mbsync -c "$XDG_CONFIG_HOME"/isync/mbsyncrc

View File

@@ -579,3 +579,28 @@ finddup() {
| uniq -w32 --all-repeated=separate \
| awk '{print $2}'
}
# Wrapper around tmsu that searches for .tmsu/db in all parent directories and
# fallbacks to XDG_DATA_HOME if not found.
tag() {
if (( ! $+commands[tmsu] )); then
printf >&2 "tmsu not installed.\n"
return 1
fi
local db dir="$PWD" std=".tmsu/db"
# Go up directories until root to find .tmsu/db
until [[ -e $dir/$std || $dir = / ]]; do
dir="${dir:h}"
done
db="$dir/$std"
# Fallback to XDG_DATA if .tmsu/db was not found in one of the parent dirs.
if [[ ! -e $db ]]; then
db="${XDG_DATA_HOME:-$HOME/.local/share}"/tmsu/db
mkdir -p "${db:h}"
fi
env TMSU_DB="$db" tmsu "$@"
}