comments, renamed and rearranged commands

This commit is contained in:
2025-09-08 10:36:52 +02:00
parent 49411a2b22
commit a2fd70e096

View File

@@ -95,6 +95,15 @@ MODE_INSERT="show"
. "sh/fzf.sh"
# Command-line options that may only be used internally.
# --lines
# --playback
# --playlist
# --action-playlistcursor
# --action-filter
# --action-gotoartist
# --action-draw
# --mbsearch
# --preview-artist
case "${1:-}" in
"--lines")
# Print lines that are fed into fzf.
@@ -172,7 +181,42 @@ case "${1:-}" in
esac
exit 0
;;
"--filter")
"--playlist")
# Run playback commands
#
# @argument $2: playback command (see `src/sh/playback.sh`)
#
# This is a wrapper to execute mpv commands.
case "$2" in
"$PLAYLIST_CMD_REMOVE") mpv_rm_index $((FZF_POS - 1)) ;;
"$PLAYLIST_CMD_UP") mpv_playlist_move $((FZF_POS - 1)) $((FZF_POS - 2)) ;;
"$PLAYLIST_CMD_DOWN") mpv_playlist_move $((FZF_POS - 0)) $((FZF_POS - 1)) ;;
"$PLAYLIST_CMD_CLEAR") mpv_playlist_clear ;;
"$PLAYLIST_CMD_CLEAR_ABOVE")
for i in $(seq "$FZF_POS"); do
mpv_rm_index 0
done
;;
"$PLAYLIST_CMD_CLEAR_BELOW")
cnt=$(mpv_playlist_count)
rem=$((cnt - FZF_POS + 1))
for i in $(seq "$rem"); do
mpv_rm_index $((FZF_POS - 1))
done
;;
esac
exit 0
;;
"--action-playlistcursor")
# Print fzf command to replace cursor in playlist
#
# This prints the command read by a `transform` fzf binding, with which the
# cursor is placed on the currently played track in the playlist view.
pos=$(mpv_playlist_position)
printf "pos(%s)" $((pos + 1))
exit 0
;;
"--action-filter")
# fzf instructions to invoke filters
#
# @argument #2: mode
@@ -189,7 +233,7 @@ case "${1:-}" in
[ "$mode" = "$MODE_NORMAL" ] && printf "+hide-input"
exit 0
;;
"--jumpto-artist")
"--action-gotoartist")
# fzf instructions to go to artist
#
# @argument $2: mode
@@ -223,10 +267,10 @@ case "${1:-}" in
cnt=$(echo "$j" | $JQ 'length')
[ "$cnt" -eq 1 ] && aid="$(echo "$j" | $JQ -r '.[0].artist.id')"
fi
[ "${aid:-}" ] && $0 --draw "$mode" "$VIEW_ARTIST" "$aid" || printf "print(%s)+print(%s)+print(%s)+print(%s)+accept" "$VIEW_SELECT_ARTIST" "$j" "$view" "$mbid_cur"
[ "${aid:-}" ] && $0 --action-draw "$mode" "$VIEW_ARTIST" "$aid" || printf "print(%s)+print(%s)+print(%s)+print(%s)+accept" "$VIEW_SELECT_ARTIST" "$j" "$view" "$mbid_cur"
exit 0
;;
"--draw")
"--action-draw")
# Generate fzf command to draw screen.
#
# @argument $2: mode (default `normal`)
@@ -320,7 +364,7 @@ case "${1:-}" in
[ "$mode" = "$MODE_NORMAL" ] && printf "+hide-input"
exit 0
;;
"--fzf-change")
"--mbsearch")
# Trigger search on MusicBrainz
#
# @argument $2: view
@@ -331,6 +375,21 @@ case "${1:-}" in
fzf_handle_change "$2"
exit 0
;;
"--preview-artist")
# Generate content for artist preview
#
# @argument $2: MusicBrainz Artist ID
#
# This prints the text to be displayed in the preview window for the
# specified artist.
__preview_artist "$2"
exit 0
;;
esac
# Non-interactive user commands intended to the user. These commands do not
# require temporary files, fzf, nor the mpv instance.
case "${1:-}" in
"--decorate")
# Decorate directory with tagged audio files
#
@@ -362,51 +421,6 @@ case "${1:-}" in
info "Done"
exit 0
;;
"--preview-artist")
# Generate content for artist preview
#
# @argument $2: MusicBrainz Artist ID
#
# This prints the text to be displayed in the preview window for the
# specified artist.
__preview_artist "$2"
exit 0
;;
"--playlistcmd")
# Run playback commands
#
# @argument $2: playback command (see `src/sh/playback.sh`)
#
# This is a wrapper to execute mpv commands.
case "$2" in
"$PLAYLIST_CMD_REMOVE") mpv_rm_index $((FZF_POS - 1)) ;;
"$PLAYLIST_CMD_UP") mpv_playlist_move $((FZF_POS - 1)) $((FZF_POS - 2)) ;;
"$PLAYLIST_CMD_DOWN") mpv_playlist_move $((FZF_POS - 0)) $((FZF_POS - 1)) ;;
"$PLAYLIST_CMD_CLEAR") mpv_playlist_clear ;;
"$PLAYLIST_CMD_CLEAR_ABOVE")
for i in $(seq "$FZF_POS"); do
mpv_rm_index 0
done
;;
"$PLAYLIST_CMD_CLEAR_BELOW")
cnt=$(mpv_playlist_count)
rem=$((cnt - FZF_POS + 1))
for i in $(seq "$rem"); do
mpv_rm_index $((FZF_POS - 1))
done
;;
esac
exit 0
;;
"--playlist-place-cursor")
# Print fzf command to replace cursor in playlist
#
# This prints the command read by a `transform` fzf binding, with which the
# cursor is placed on the currently played track in the playlist view.
pos=$(mpv_playlist_position)
printf "pos(%s)" $((pos + 1))
exit 0
;;
"--help")
# Print help string
cat <<EOF
@@ -433,26 +447,15 @@ EOF
"--refresh-view")
# Recompute main views
#
# With this method, the content for the views VIEW_LIST_ARTISTS and
# VIEW_LIST_ALBUMS are recomputed.
# With this method, the content for the views VIEW_LIST_ARTISTS and VIEW_LIST_ALBUMS are recomputed.
precompute_view
exit 0
;;
esac
# Set window title
printf '\033]0;%s\007' "$WINDOW_TITLE"
# Generate filenames for temporary files
tmpdir=$(mktemp -d)
LOCKFILE="$tmpdir/lock"
RESULTS="$tmpdir/results"
PIDFILE="$tmpdir/pid"
trap 'rm -rf "$tmpdir"' EXIT INT
export LOCKFILE RESULTS PIDFILE
# Views and modes
# Interactive user commands
# If no unknown command is passed, then this will continue to starting the mpv
# instance and fzf.
case "${1:-}" in
"--artist")
[ ! "${2:-}" ] && err "MusicBrainz Artist ID not specified (see --help)" && exit 1
@@ -498,6 +501,17 @@ case "${1:-}" in
;;
esac
# Start application: Set title, get temporary directory, start mpv daemon, and
# enter main fzf loop.
# Set window title
printf '\033]0;%s\007' "$WINDOW_TITLE"
# Generate filenames for temporary files
tmpdir=$(mktemp -d)
LOCKFILE="$tmpdir/lock"
RESULTS="$tmpdir/results"
PIDFILE="$tmpdir/pid"
trap 'rm -rf "$tmpdir"' EXIT INT
export LOCKFILE RESULTS PIDFILE
# Start mpv
mpv_start
@@ -519,13 +533,10 @@ mpv_start
IN_NORMAL_MODE="[ \$FZF_INPUT_STATE = hidden ]"
IN_VIEW_PATTERN="[ \$FZF_BORDER_LABEL = %s ]"
IN_LIST_ARTISTS_VIEW="$(printf "$IN_VIEW_PATTERN" "$VIEW_LIST_ARTISTS")"
IN_LIST_ALBUMS_VIEW="$(printf "$IN_VIEW_PATTERN" "$VIEW_LIST_ALBUMS")"
IN_SEARCH_ARTIST_VIEW="$(printf "$IN_VIEW_PATTERN" "$VIEW_SEARCH_ARTIST")"
IN_SEARCH_ALBUM_VIEW="$(printf "$IN_VIEW_PATTERN" "$VIEW_SEARCH_ALBUM")"
FZF_CURRENT_MODE="\$FZF_INPUT_STATE"
FZF_CURRENT_VIEW="\$FZF_BORDER_LABEL"
FZF_RELOAD_PLAYLIST="reload-sync($0 --lines $VIEW_PLAYLIST)"
FZF_POS_PLAYLIST="transform:$0 --playlist-place-cursor"
FZF_POS_PLAYLIST="transform:$0 --action-playlistcursor"
PUT_FZF_KEY_LOGIC="case \$FZF_KEY in space) echo \"put( )\";; left) echo backward-char;; right) echo forward-char;; backspace|bspace|bs) echo backward-delete-char;; delete|del) echo delete-char;; *) echo \"put(\$FZF_KEY)\";; esac"
while true; do
case "$VIEW" in
@@ -576,7 +587,7 @@ while true; do
--bind="$KEYS_N_BOT:last" \
--bind="$KEYS_N_TOP:first" \
--bind="$KEYS_OUT,$KEYS_N_OUT,$KEYS_QUIT,$KEYS_N_QUIT:print($LASTVIEW)+print($LASTARG)+print($VIEW_PLAYLIST)+print()+accept" \
--bind="$KEYS_SELECT_ARTIST:transform:$0 --jumpto-artist $MODE_NORMAL $VIEW_PLAYLIST {2} {3}" \
--bind="$KEYS_SELECT_ARTIST:transform:$0 --action-gotoartist $MODE_NORMAL $VIEW_PLAYLIST {2} {3}" \
--bind="$KEYS_LIST_ARTISTS:print($VIEW_LIST_ARTISTS)+accept" \
--bind="$KEYS_LIST_ALBUMS:print($VIEW_LIST_ALBUMS)+accept" \
--bind="$KEYS_SEARCH_ARTIST:print($VIEW_SEARCH_ARTIST)+accept" \
@@ -585,12 +596,12 @@ while true; do
--bind="$KEYS_OPEN:execute-silent:open \"\$(dirname {4})\"" \
--bind="$KEYS_PLAYBACK,$KEYS_N_PLAYBACK:transform($0 --playback $VIEW_PLAYLIST {2} {3} {4})+$FZF_RELOAD_PLAYLIST+$FZF_POS_PLAYLIST" \
--bind="$KEYS_PLAYLIST_RELOAD:$FZF_RELOAD_PLAYLIST+$FZF_POS_PLAYLIST" \
--bind="$KEYS_PLAYLIST_REMOVE:execute-silent($0 --playlistcmd $PLAYLIST_CMD_REMOVE)+$FZF_RELOAD_PLAYLIST" \
--bind="$KEYS_PLAYLIST_UP:execute-silent($0 --playlistcmd $PLAYLIST_CMD_UP)+up+$FZF_RELOAD_PLAYLIST" \
--bind="$KEYS_PLAYLIST_DOWN:execute-silent($0 --playlistcmd $PLAYLIST_CMD_DOWN)+down+$FZF_RELOAD_PLAYLIST" \
--bind="$KEYS_PLAYLIST_CLEAR:execute-silent($0 --playlistcmd $PLAYLIST_CMD_CLEAR)+$FZF_RELOAD_PLAYLIST" \
--bind="$KEYS_PLAYLIST_CLEAR_ABOVE:execute-silent($0 --playlistcmd $PLAYLIST_CMD_CLEAR_ABOVE)+$FZF_RELOAD_PLAYLIST" \
--bind="$KEYS_PLAYLIST_CLEAR_BELOW:execute-silent($0 --playlistcmd $PLAYLIST_CMD_CLEAR_BELOW)+$FZF_RELOAD_PLAYLIST" \
--bind="$KEYS_PLAYLIST_REMOVE:execute-silent($0 --playlist $PLAYLIST_CMD_REMOVE)+$FZF_RELOAD_PLAYLIST" \
--bind="$KEYS_PLAYLIST_UP:execute-silent($0 --playlist $PLAYLIST_CMD_UP)+up+$FZF_RELOAD_PLAYLIST" \
--bind="$KEYS_PLAYLIST_DOWN:execute-silent($0 --playlist $PLAYLIST_CMD_DOWN)+down+$FZF_RELOAD_PLAYLIST" \
--bind="$KEYS_PLAYLIST_CLEAR:execute-silent($0 --playlist $PLAYLIST_CMD_CLEAR)+$FZF_RELOAD_PLAYLIST" \
--bind="$KEYS_PLAYLIST_CLEAR_ABOVE:execute-silent($0 --playlist $PLAYLIST_CMD_CLEAR_ABOVE)+$FZF_RELOAD_PLAYLIST" \
--bind="$KEYS_PLAYLIST_CLEAR_BELOW:execute-silent($0 --playlist $PLAYLIST_CMD_CLEAR_BELOW)+$FZF_RELOAD_PLAYLIST" \
--bind="$KEYS_PLAYLIST_GOTO_RELEASE:print($VIEW_RELEASE)+accept" \
--delimiter="\t" \
--with-nth="{1}" \
@@ -625,7 +636,7 @@ while true; do
--info="inline-right" \
--header-first \
--header-border="bottom" \
--bind="start:transform:$0 --draw $MODE $VIEW $MBID" \
--bind="start:transform:$0 --action-draw $MODE $VIEW $MBID" \
--bind="$KEYS_I_NORMAL:transform:$IN_NORMAL_MODE || echo hide-input" \
--bind="$KEYS_N_INSERT:transform:$IN_NORMAL_MODE && echo show-input || $PUT_FZF_KEY_LOGIC" \
--bind="$KEYS_DOWN:down" \
@@ -636,28 +647,28 @@ while true; do
--bind="$KEYS_N_UP:transform:$IN_NORMAL_MODE && echo up || $PUT_FZF_KEY_LOGIC" \
--bind="$KEYS_N_BOT:transform:$IN_NORMAL_MODE && echo last || $PUT_FZF_KEY_LOGIC" \
--bind="$KEYS_N_TOP:transform:$IN_NORMAL_MODE && echo first || $PUT_FZF_KEY_LOGIC" \
--bind="$KEYS_IN:transform:[ {3} ] && $0 --draw $FZF_CURRENT_MODE $FZF_CURRENT_VIEW {3} \"+1\"" \
--bind="$KEYS_OUT:transform:[ {2} ] && $0 --draw $FZF_CURRENT_MODE $FZF_CURRENT_VIEW {2} \"-1\"" \
--bind="$KEYS_N_IN:transform:$IN_NORMAL_MODE && ([ {3} ] && $0 --draw $FZF_CURRENT_MODE $FZF_CURRENT_VIEW {3} \"+1\") || $PUT_FZF_KEY_LOGIC" \
--bind="$KEYS_N_OUT:transform:$IN_NORMAL_MODE && ([ {2} ] && $0 --draw $FZF_CURRENT_MODE $FZF_CURRENT_VIEW {2} \"-1\") || $PUT_FZF_KEY_LOGIC" \
--bind="$KEYS_SELECT_ARTIST:transform:$0 --jumpto-artist $FZF_CURRENT_MODE $FZF_CURRENT_VIEW {2} {3}" \
--bind="$KEYS_LIST_ARTISTS:transform:$0 --draw \$FZF_INPUT_STATE $VIEW_LIST_ARTISTS" \
--bind="$KEYS_LIST_ALBUMS:transform:$0 --draw \$FZF_INPUT_STATE $VIEW_LIST_ALBUMS" \
--bind="$KEYS_SEARCH_ARTIST:transform:$0 --draw $MODE_INSERT $VIEW_SEARCH_ARTIST" \
--bind="$KEYS_SEARCH_ALBUM:transform:$0 --draw $MODE_INSERT $VIEW_SEARCH_ALBUM" \
--bind="$KEYS_IN:transform:[ {3} ] && $0 --action-draw $FZF_CURRENT_MODE $FZF_CURRENT_VIEW {3} \"+1\"" \
--bind="$KEYS_OUT:transform:[ {2} ] && $0 --action-draw $FZF_CURRENT_MODE $FZF_CURRENT_VIEW {2} \"-1\"" \
--bind="$KEYS_N_IN:transform:$IN_NORMAL_MODE && ([ {3} ] && $0 --action-draw $FZF_CURRENT_MODE $FZF_CURRENT_VIEW {3} \"+1\") || $PUT_FZF_KEY_LOGIC" \
--bind="$KEYS_N_OUT:transform:$IN_NORMAL_MODE && ([ {2} ] && $0 --action-draw $FZF_CURRENT_MODE $FZF_CURRENT_VIEW {2} \"-1\") || $PUT_FZF_KEY_LOGIC" \
--bind="$KEYS_SELECT_ARTIST:transform:$0 --action-gotoartist $FZF_CURRENT_MODE $FZF_CURRENT_VIEW {2} {3}" \
--bind="$KEYS_LIST_ARTISTS:transform:$0 --action-draw \$FZF_INPUT_STATE $VIEW_LIST_ARTISTS" \
--bind="$KEYS_LIST_ALBUMS:transform:$0 --action-draw \$FZF_INPUT_STATE $VIEW_LIST_ALBUMS" \
--bind="$KEYS_SEARCH_ARTIST:transform:$0 --action-draw $MODE_INSERT $VIEW_SEARCH_ARTIST" \
--bind="$KEYS_SEARCH_ALBUM:transform:$0 --action-draw $MODE_INSERT $VIEW_SEARCH_ALBUM" \
--bind="$KEYS_SWITCH_ARTIST_ALBUM:transform:case $FZF_CURRENT_VIEW in
$VIEW_LIST_ARTISTS) $0 --draw $FZF_CURRENT_MODE $VIEW_LIST_ALBUMS ;;
$VIEW_LIST_ALBUMS) $0 --draw $FZF_CURRENT_MODE $VIEW_LIST_ARTISTS ;;
$VIEW_SEARCH_ARTIST) $0 --draw $MODE_INSERT $VIEW_SEARCH_ALBUM ;;
$VIEW_SEARCH_ALBUM) $0 --draw $MODE_INSERT $VIEW_SEARCH_ARTIST ;;
$VIEW_LIST_ARTISTS) $0 --action-draw $FZF_CURRENT_MODE $VIEW_LIST_ALBUMS ;;
$VIEW_LIST_ALBUMS) $0 --action-draw $FZF_CURRENT_MODE $VIEW_LIST_ARTISTS ;;
$VIEW_SEARCH_ARTIST) $0 --action-draw $MODE_INSERT $VIEW_SEARCH_ALBUM ;;
$VIEW_SEARCH_ALBUM) $0 --action-draw $MODE_INSERT $VIEW_SEARCH_ARTIST ;;
esac" \
--bind="$KEYS_SWITCH_LOCAL_REMOTE:transform:case $FZF_CURRENT_VIEW in
$VIEW_LIST_ARTISTS) $0 --draw $MODE_INSERT $VIEW_SEARCH_ARTIST ;;
$VIEW_LIST_ALBUMS) $0 --draw $MODE_INSERT $VIEW_SEARCH_ALBUM ;;
$VIEW_SEARCH_ARTIST) $0 --draw $MODE_NORMAL $VIEW_LIST_ARTISTS ;;
$VIEW_SEARCH_ALBUM) $0 --draw $MODE_NORMAL $VIEW_LIST_ALBUMS ;;
$VIEW_LIST_ARTISTS) $0 --action-draw $MODE_INSERT $VIEW_SEARCH_ARTIST ;;
$VIEW_LIST_ALBUMS) $0 --action-draw $MODE_INSERT $VIEW_SEARCH_ALBUM ;;
$VIEW_SEARCH_ARTIST) $0 --action-draw $MODE_NORMAL $VIEW_LIST_ARTISTS ;;
$VIEW_SEARCH_ALBUM) $0 --action-draw $MODE_NORMAL $VIEW_LIST_ALBUMS ;;
esac" \
--bind="$KEYS_FILTER:transform:$0 --filter $FZF_CURRENT_MODE $FZF_CURRENT_VIEW" \
--bind="$KEYS_FILTER:transform:$0 --action-filter $FZF_CURRENT_MODE $FZF_CURRENT_VIEW" \
--bind="$KEYS_BROWSE:execute-silent:
[ {3} ] || exit 0
case \$FZF_BORDER_LABEL in
@@ -672,10 +683,10 @@ open \"https://musicbrainz.org/\$t/{r3}\"" \
open \"\$(dirname {4})\"" \
--bind="$KEYS_SHOW_PLAYLIST:transform:echo \"print($VIEW_PLAYLIST)+print()+print($FZF_CURRENT_VIEW)+print({2})+accept\"" \
--bind="$KEYS_QUIT:print($VIEW_QUIT)+accept" \
--bind="$KEYS_N_QUIT:transform:$IN_NORMAL_MODE && ($IN_LIST_ARTISTS_VIEW && echo \"print($VIEW_QUIT)+accept\" || $0 --draw $MODE_NORMAL $VIEW_LIST_ARTISTS) || $PUT_FZF_KEY_LOGIC" \
--bind="$KEYS_N_QUIT:transform:$IN_NORMAL_MODE && ($IN_LIST_ARTISTS_VIEW && echo \"print($VIEW_QUIT)+accept\" || $0 --action-draw $MODE_NORMAL $VIEW_LIST_ARTISTS) || $PUT_FZF_KEY_LOGIC" \
--bind="$KEYS_PLAYBACK:transform:$0 --playback $FZF_CURRENT_VIEW {2} {3} {4}" \
--bind="$KEYS_N_PLAYBACK:transform:$IN_NORMAL_MODE && $0 --playback $FZF_CURRENT_VIEW {2} {3} {4} || $PUT_FZF_KEY_LOGIC" \
--bind="change:execute-silent($0 --fzf-change $FZF_CURRENT_VIEW &)+reload:$0 --lines $FZF_CURRENT_VIEW" \
--bind="change:execute-silent($0 --mbsearch $FZF_CURRENT_VIEW &)+reload:$0 --lines $FZF_CURRENT_VIEW" \
--preview-window="right,25%,border-left,wrap,<30(hidden)" \
--preview="$0 --preview-artist {3}" \
--delimiter="\t" \