faster but quite buggy; no disc state storage

This commit is contained in:
2025-09-04 15:10:16 +02:00
parent b15848887b
commit 8c37cb2fea
7 changed files with 368 additions and 81 deletions

View File

@@ -2,6 +2,21 @@
set -eu
VIEW_ARTIST="artist"
VIEW_RELEASEGROUP="rg"
VIEW_RELEASE="release"
VIEW_SEARCH_ARTIST="search-artist"
VIEW_SEARCH_ALBUM="search-album"
VIEW_LIST_ARTISTS="list-artists"
VIEW_LIST_ALBUMS="list-albums"
VIEW_SELECT_ARTIST="select-artist"
VIEW_PLAYLIST="playlist"
MODE_NORMAL="hidden"
MODE_INSERT="show"
export VIEW_ARTIST VIEW_RELEASEGROUP VIEW_RELEASE VIEW_SEARCH_ARTIST \
VIEW_SEARCH_ALBUM VIEW_LIST_ARTISTS VIEW_LIST_ALBUMS VIEW_SELECT_ARTIST \
VIEW_PLAYLIST MODE_NORMAL MODE_INSERT
# Load helper methods
. "sh/info.sh"
@@ -51,6 +66,110 @@ set -eu
. "sh/fzf.sh"
case "${1:-}" in
"--lines")
view=${2:-}
mbid=${3:-}
case "$view" in
"$VIEW_ARTIST")
list_releasegroups "$mbid"
;;
"$VIEW_RELEASEGROUP")
list_releases "$mbid"
;;
"$VIEW_RELEASE")
list_recordings "$mbid"
;;
"$VIEW_LIST_ARTISTS")
list_local_artists
;;
"$VIEW_LIST_ALBUMS")
list_local_releasegroups
;;
esac
exit 0
;;
"--draw")
debug "call to $*"
# Generate fzf command to draw screen.
#
# @argument $2: mode (default `normal`)
# @argument $3: view (default list artists)
# @argument $4: MusicBrainz ID (optional)
# @argument $5: level (optional)
#
# The argument `level` specifies the view relative to `view`: If `level` is
# set to +1, then the specified MusicBrainz ID is an ID of an object one level
# deeper than `view`. Alternatively, the argument `level` may be set to `-1`.
# Anything else is interpreted as "on the level of `view`".
#
# The choice of possible arguments ($5) depends on the view.
# These views are independent of the MusicBrainz ID ($4) and of the argument
# ($5):
# - VIEW_SEARCH_ARTIST: Get ready to query MusicBrainz for artists
# - VIEW_SEARCH_ALBUM: Get ready to query MusicBrainz for albums
# - VIEW_LIST_ARTISTS: List all locally available artists
# - VIEW_LIST_ALBUMS: List al locally available albums
#
# If no argument ($5) is specified, then the remaining views act as follows:
# - VIEW_ARTIST: Display all release groups of that artist
# - VIEW_RELEASEGROUP: Display all releases within that release group
# - VIEW_RELEASE: Display track list of specified release
#
# Here, if the argument is set to `-1`, then the parent entry is displayed:
# - VIEW_ARTIST: Divert view to VIEW_LIST_ARTISTS
# - VIEW_RELEASEGROUP: For single-artist release groups, divert to
# VIEW_ARTIST of that artist, else display the artist selection.
# - VIEW_RELEASE: Divert view to VIEW_LIST_RELEASEGROUP.
#
# Alternatively, if the argument is set to `+1`, then the child entry is
# displayed:
# - VIEW_ARTIST: Divert view to VIEW_LIST_ARTISTS
# - VIEW_RELEASEGROUP: For single-artist release groups, divert to
# VIEW_ARTIST of that artist, else display the artist selection.
# - VIEW_RELEASE: Divert view to VIEW_LIST_RELEASEGROUP.
#
# Hence, the view is only diverted in this last case.
mode="${2:-"$MODE_NORMAL"}"
view="${3:-"$VIEW_LIST_ARTISTS"}"
mbid="${4:-}"
level="${5:-}"
# Change state, if we are being diverted.
case "$level" in
"-1")
case "$view" in
"$VIEW_ARTIST")
view="$VIEW_LIST_ARTISTS"
mbid=""
;;
"$VIEW_RELEASEGROUP")
view="$VIEW_ARTIST"
mbid="$(mb_releasegroup "$mbid" | $JQ -r --compact-output '."artist-credit"[0].artist.id')"
;;
"$VIEW_RELEASE")
view="$VIEW_RELEASEGROUP"
mbid="$(mb_release "$mbid" | $JQ -r --compact-output '."release-group".id')"
;;
esac
;;
"+1")
case "$view" in
"$VIEW_SEARCH_ARTIST" | "$VIEW_LIST_ARTISTS") view="$VIEW_ARTIST" ;;
"$VIEW_ARTIST" | "$VIEW_SEARCH_ALBUM" | "$VIEW_LIST_ALBUMS") view="$VIEW_RELEASEGROUP" ;;
"$VIEW_RELEASEGROUP") view="$VIEW_RELEASE" ;;
esac
;;
*) ;;
esac
# Store current state
printf "change-border-label(%s)+change-list-label(%s)" "$view" "$mbid"
debug "calling: $(printf "change-border-label(%s)+change-list-label(%s)" "$view" "$mbid")"
[ "$mode" = "$MODE_NORMAL" ] && printf "+hide-input" || printf "+show-input"
# Set header
fzf_command_set_header "$view" "$mbid"
# Load lines
printf "+reload($0 --lines %s %s)" "$view" "$mbid"
exit 0
;;
"--fzf-reload")
fzf_handle_reload
exit 0
@@ -148,38 +267,47 @@ touch "$STATEFILE" "$ARGSFILE" "$STATEFILE_LAST" "$ARGSFILE_LAST"
export STATEFILE ARGSFILE STATEFILE_LAST ARGSFILE_LAST
# Views and modes
VIEW_ARTIST="artist"
VIEW_RELEASEGROUP="rg"
VIEW_RELEASE="release"
VIEW_SEARCH_ARTIST="search-artist"
VIEW_SEARCH_ALBUM="search-album"
VIEW_LIST_ARTISTS="list-artists"
VIEW_LIST_ALBUMS="list-albums"
VIEW_SELECT_ARTIST="select-artist"
VIEW_PLAYLIST="playlist"
MODE_NORMAL="normal"
MODE_INSERT="insert"
export VIEW_ARTIST VIEW_RELEASEGROUP VIEW_RELEASE VIEW_SEARCH_ARTIST \
VIEW_SEARCH_ALBUM VIEW_LIST_ARTISTS VIEW_LIST_ALBUMS VIEW_SELECT_ARTIST \
VIEW_PLAYLIST MODE_NORMAL MODE_INSERT
case "${1:-}" in
"--artist")
[ ! "${2:-}" ] && err "MusicBrainz Artist ID not specified (see --help)" && exit 1
state_init "$VIEW_ARTIST" "$MODE_NORMAL" "$2"
VIEW="$VIEW_ARTIST"
MODE="$MODE_NORMAL"
MBID="$2"
;;
"--releasegroup")
[ ! "${2:-}" ] && err "MusicBrainz Release-Group ID not specified (see --help)" && exit 1
state_init "$VIEW_RELEASEGROUP" "$MODE_NORMAL" "$2"
VIEW="$VIEW_RELEASEGROUP"
MODE="$MODE_NORMAL"
MBID="$2"
;;
"--release")
[ ! "${2:-}" ] && err "MusicBrainz Release ID not specified (see --help)" && exit 1
state_init "$VIEW_RELEASE" "$MODE_NORMAL" "$2"
VIEW="$VIEW_RELEASE"
MODE="$MODE_NORMAL"
MBID="$2"
;;
"--search-artist")
state_init "$VIEW_SEARCH_ARTIST" "$MODE_INSERT" "${2:-}"
VIEW="$VIEW_SEARCH_ARTIST"
MODE="$MODE_INSERT"
MBID=""
;;
"--search-album")
VIEW="$VIEW_SEARCH_ALBUM"
MODE="$MODE_INSERT"
MBID=""
;;
"--artists" | "")
VIEW="$VIEW_LIST_ARTISTS"
MODE="$MODE_NORMAL"
MBID=""
;;
"--albums")
VIEW="$VIEW_LIST_ALBUMS"
MODE="$MODE_NORMAL"
MBID=""
;;
"--search-artist") state_init "$VIEW_SEARCH_ARTIST" "$MODE_INSERT" "${2:-}" ;;
"--search-album") state_init "$VIEW_SEARCH_ALBUM" "$MODE_INSERT" "${2:-}" ;;
"--artists" | "") state_init "$VIEW_LIST_ARTISTS" "$MODE_NORMAL" "" ;;
"--albums") state_init "$VIEW_LIST_ALBUMS" "$MODE_NORMAL" "" ;;
*)
err "Unknown option $1 (see --help)"
exit 1
@@ -189,12 +317,27 @@ esac
# Start mpv
mpv_start
# $KEYS_PLAY main loop
# main loop
# states are stored in (in)visible labels
#
# mode: [$MODE_NORMAL, $MODE_INSERT]
# The mode is reflected on the input visibility. The variable $FZF_INPUT_STATE
# is set to "hidden" if and only if the mode is `normal`. To swtich to `normal`
# mode, we call `hide-input`. To switch to insert mode, we call `show-input`.
#
# view: [$VIEW_*]
# The view is stored in $FZF_BORDER_LABEL. To set the view, call
# `change-border-label($VIEW)`.
#
# argument: string
# The argument is stored in $FZF_LIST_LABEL. To set the argument, call
# `change-list-label($arg)`.
IN_NORMAL_MODE="[ \"\$FZF_INPUT_STATE\" = \"hidden\" ]"
PUT_FZF_KEY_LOGIC="case \"\$FZF_KEY\" in \"space\") echo \"put( )\";; \"backspace\"|\"bspace\"|\"bs\") echo \"backward-delete-char\";; \"delete\"|\"del\") echo \"delete-char\";; *) echo \"put(\$FZF_KEY)\";; esac"
while true; do
view=$(state_get_view)
mode=$(state_get_mode)
args=$(state_get_args)
case "$view" in
case "$VIEW" in
"$VIEW_SELECT_ARTIST")
sel=$(
echo "$args" |
@@ -230,19 +373,89 @@ $KEYS_FILTER_LOCAL:transform:$0 --fzf-key {2} {3} {4}" \
--delimiter="\t" \
--with-nth="{1}" >/dev/null
;;
*) # Main instance
*)
# Main instance
#
# KEY-BINDINGS:
# Key variables contain comma-delimited sequences of keys. Every key
# variable starts with `KEYS_`. Key variables with the prefix `KEYS_I_` are
# exclusive to the `insert` mode. Key variables with the prefix `KEYS_N_`
# are exclusive to the `normal` mode. All other keys are bound to both
# modes. It is important that the keys used in `KEYS_N_` variables are
# naturally printable or modifications of the input string. See
# `$PUT_FZF_KEY_LOGIC` for details.
#
# Here is a list of all keys grouped by type.
#
# Mode selection:
# - $KEYS_I_NORMAL: Switch to normal mode (insert mode)
# - $KEYS_N_INSERT: Switch to insert mode (normal mode)
#
# Vertical navigation:
# - $KEYS_DOWN: Move cursor to the next line
# - $KEYS_UP: Move cursor to the previous line
# - $KEYS_HALFPAGE_UP: Move cursor half a page up
# - $KEYS_HALFPAGE_DOWN: Move cursor half a page up
# - $KEYS_N_DOWN: Move cursor to the next line (normal mode)
# - $KEYS_N_UP: Move cursor to the previous line (normal mode)
# - $KEYS_N_BOT: Move cursor to the last line (normal mode)
# - $KEYS_N_TOP: Move cursor to the first line (normal mode)
#
# Horizontal navigation:
# - $KEYS_IN: Enter into selected item, down the hierarchy
# - $KEYS_OUT: Leave current item, up the hierarchy
#
# Filtering:
#
# Playback:
#
# Extras:
# - $KEYS_BROWSE: Open MusicBrainz webpage of the selected item
# - $KEYS_OPEN: Open file manager in the directory of the selected item
#--bind="start:change-border-label($VIEW)+change-list-label($MBID)+$MODE-input+transform:$0 --display" \
sel=$(
$FZF \
printf "" | $FZF \
--reverse \
--bind="start:reload:$0 --fzf-reload" \
--bind="load:transform:$0 --fzf-load" \
--bind="change:execute-silent($0 --fzf-change &)+reload:$0 --fzf-change-reload" \
--bind="$KEYS_ALL:transform:$0 --fzf-key {2} {3} {4}" \
--expect="ctrl-c" \
--info="inline-right" \
--info-command="$0 --fzf-info" \
--preview-window="right,25%,border-left,wrap,<30(hidden)" \
--preview="$0 --internal-preview-artist {3}" \
--header-first \
--header-border="bottom" \
--bind="start:transform:$0 --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" \
--bind="$KEYS_UP:up" \
--bind="$KEYS_HALFPAGE_DOWN:half-page-down" \
--bind="$KEYS_HALFPAGE_UP:half-page-up" \
--bind="$KEYS_N_DOWN:transform:$IN_NORMAL_MODE && echo \"down\" || $PUT_FZF_KEY_LOGIC" \
--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_INPUT_STATE \$FZF_BORDER_LABEL {3} \"+1\"" \
--bind="$KEYS_OUT:transform:echo {2} >> /tmp/foo; echo {} >> /tmp/foo; [ {2} ] && $0 --draw \$FZF_INPUT_STATE \$FZF_BORDER_LABEL {2} \"-1\"" \
--bind="$KEYS_BROWSE:execute-silent:
[ {3} ] || exit 0
case \"\$FZF_BORDER_LABEL\" in
\"$VIEW_LIST_ARTISTS\" | \"$VIEW_SEARCH_ARTIST\") t=\"artist\" ;;
\"$VIEW_ARTIST\" | \"$VIEW_SEARCH_ALBUM\" | \"$VIEW_LIST_ALBUMS\") t=\"release-group\" ;;
\"$VIEW_RELEASEGROUP\") t=\"release\" ;;
\"$VIEW_RELEASE\") t=\"track\" ;;
esac
open \"https://musicbrainz.org/\$t/{r3}\"" \
--bind="$KEYS_OPEN:execute-silent:
[ {4} ] || exit 0
open \"\$(dirname {4})\"" \
--bind="$KEYS_SELECT_ARTIST:" \
--bind="$KEYS_FILTER_LOCAL:" \
--bind="$KEYS_FILTER_1:" \
--bind="$KEYS_FILTER_2:" \
--bind="$KEYS_FILTER_3:" \
--bind="$KEYS_FILTER_4:" \
--bind="$KEYS_SWITCH_ARTIST_ALBUM:" \
--bind="$KEYS_SWITCH_LOCAL_REMOTE:" \
--bind="$KEYS_PLAY:" \
--bind="$KEYS_QUEUE:" \
--bind="$KEYS_SHOW_PLAYLIST:" \
--expect="ctrl-c" \
--delimiter="\t" \
--with-nth="{1}" || true
)
@@ -250,3 +463,9 @@ $KEYS_FILTER_LOCAL:transform:$0 --fzf-key {2} {3} {4}" \
;;
esac
done
#[ \"$MODE\" = \"$MODE_NORMAL\" ] && echo \"+hide-input\" || echo \"+show-input\"
# --bind="load:transform:$0 --fzf-load" \
# --bind="change:execute-silent($0 --fzf-change &)+reload:$0 --fzf-change-reload" \
# --bind="$KEYS_ALL:transform:$0 --fzf-key {2} {3} {4}" \
# --preview-window="right,25%,border-left,wrap,<30(hidden)" \
# --preview="$0 --internal-preview-artist {3}" \