Skip to content

Instantly share code, notes, and snippets.

@shanness
Last active June 26, 2021 15:21
Show Gist options
  • Save shanness/a4855a397ed6568b946f46fbc6fdbb09 to your computer and use it in GitHub Desktop.
Save shanness/a4855a397ed6568b946f46fbc6fdbb09 to your computer and use it in GitHub Desktop.
Bash type ahead completion for tmsu
# This belongs to /etc/bash_completion.d/tmsu or ~/.bash_completion
# Original from tomassedovic (https://github.com/oniony/TMSU/issues/78) and Master-jim
# TODO: handle spaces in values.
# Use this to control if values are displayed or just summarized, they will be expanded after the = is typed/selected
#VALUES="display"
VALUES="summarize"
COMP_WORDBREAKS="${COMP_WORDBREAKS}="
have tmsu &&
_tmsu()
{
local cur prev prev_prev tmsu_command
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
prev_prev=${COMP_WORDS[COMP_CWORD-2]}
tmsu_command="${COMP_WORDS[1]}"
if [ "$COMP_CWORD" -eq 1 ]; then
COMPREPLY=( $(compgen -W "config copy delete dupes files help imply init merge mount rename repair stats status tag tags unmount untag untagged values version vfs" -- ${cur}) );
return 0
else
case "${tmsu_command}" in
tag|files|untag)
local IFS=$'\n'
if [ "$cur" = "=" ]; then
# Complete values for a tag (ex: year=), so no need for filenames
COMPREPLY=( $(compgen -W "$(tmsu values ${prev} 2> /dev/null)" -- "") )
elif [ "$prev" = "=" ]; then
# If previous "word" is symbol "=", used only when the "=" is not escaped
COMPREPLY=( $(compgen -f -W "$(tmsu values ${prev_prev})" -- $cur) )
else
# NORMAL case: let's print the tags, then filenames with the separator between.
# The if is used to show only the separator if there are filenames
if [[ ${tmsu_command} == files ]]; then # Don't show filenames for the files command (which only takes tags)
COMPREPLY=( $(compgen -W "$(tags)" -- $cur))
else
COMPREPLY=( $(compgen -W "$(tags)" -- $cur) $( if [ "" != "$(compgen -f -- $cur)" ]; then echo "##########__FILES__##########"; fi) $(compgen -f -- $cur))
fi
fi
return 0
;;
values)
COMPREPLY=( $(compgen -W "$(tags | grep '=' | cut -f1 -d'=')" -- $cur))
return 0
;;
help)
COMPREPLY=( $(compgen -W "config copy delete dupes files help imply init merge mount rename repair stats status tag tags unmount untag untagged values version vfs" -- ${cur}) );
return 0
;;
*)
_filedir
return 0
;;
esac
fi
}
function tags()
{
DB=$(tmsu info 2>&1 | sed -n -e "/Database/{s/Database: //;p}")
if [ -e "${DB}" ]; then
# Select all tag/value pairs for the completion.
# NOTE: there is no tag/value association without it being applied to a file.
# So some values don't show up because there's no file for them yet (or anymore).
if [[ $VALUES == summarize ]]; then
local TAG_VALUE_QUERY='SELECT tag.name || "= " || count(distinct value.name) || " values" FROM file_tag JOIN tag ON tag_id=tag.id JOIN value ON value_id=value.id group by tag.name order by tag.name;';
else
local TAG_VALUE_QUERY='SELECT DISTINCT tag.name || "=" || value.name FROM file_tag JOIN tag ON tag_id=tag.id JOIN value ON value_id=value.id order by tag.name;';
fi
sqlite3 "${DB}" "$TAG_VALUE_QUERY"
# Select the remaining tags (i.e. all tags without any value associated with them)
# (we don't want to offer a "value tag" again in its empty form)
local STANDALONE_TAGS='SELECT DISTINCT name FROM tag LEFT OUTER JOIN file_tag ON tag_id=id WHERE (value_id IS NULL) OR (value_id IS 0) order by name;'
sqlite3 "${DB}" "$STANDALONE_TAGS"
fi;
};
# Use of option "nosort" because it is handled in the SQL requests and permits to show the filenames at the end
complete -o nosort -F _tmsu tmsu
@shanness
Copy link
Author

shanness commented Jun 26, 2021

You can control if values are displayed or just summarized, they will be expanded after the = is typed/selected, by swapping which of these is commented out. Note you need to relaunch your bash shell after changing the script.

#VALUES="display"
VALUES="summarize"

Consider using this custom wrapper for tmsu as well, it uses a fuzzy search popup for selecting tags and files, and works nicely with this bash completion (hit enter instead of tab to get a popup).

https://github.com/kvngvikram/custom_tmsu#custom_tmsu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment