|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- #!/usr/bin/env bash
-
- usage() {
- LESS=-FEXR less <<'HELP'
- fzmp [OPTIONS]
-
- OPTIONS:
- -A --all
- search all songs in the library (or F1 when running)
- -a --artist
- search artist then filter by album (or F2 when running)
- -p --playlist
- search the current playlist (or F3 when running)
- playlist view has the following keybinds:
- > go to the next song in the playlist
- < go to the previous song in the playlist
- Ctrl-d delete the selected songs from the playlist
- -g --genre
- list genres (or F4 when running)
- -h --help
- print this help
-
- CONFIGURATION:
- A configuration file can be defined at $XDG_CONFIG_DIR (defaults to ~/.config)
- If a line begins with '#' it is treated as a comment and ignored
- The configuration file reads the following options:
-
- default_view
- Must be 'artists' 'songs' 'playlist' or 'genres'
- full_song_format
- A format string to be passed directly to `mpc format -f` in
- 'playlist' and 'all' views. Defaults to:
- [[[%artist% / ][[(%date%) ]%album% / ][[%track% - ][%title%]]]|%file%]
- For colorized output try:
- [[[\e\[32m%artist%\e\[0m / ][\e\[31m[(%date%) ]%album%\e\[0m / ][\e\[34m[%track% - ][%title%]\e\[0m]]|%file%]
- playlist_view_key (default F1)
- track_view_key (default F2)
- artist_view_key (default F3)
- genre_view_key (default F4)
- allows customizing which keys fire the different views
- findadd_key
- adds all songs under the cursor by artist/genre/album
- defaults to ctrl-space
- fzf_options
- Command line options to be passed directly to fzf.
- Changing this will override the default options: +s -e -i --reverse --cycle
- To use the jump feature of fzf you can try:
- +s -e -i --reverse --cycle --bind=`:jump
- It also helps to have a bind for toggle-all, e.g.
- +s -e -i --reverse --cycle --bind=`:jump --bind="ctrl-t:toggle-all"
- individual sessions can override with the environment variable FZMP_FZF_OPTIONS
- fzmp will also inherit options from FZF_DEFAULT_OPTS
- HELP
- }
-
- declare -r config_file="${XDG_CONFIG_DIR:-$HOME/.config}/fzmp/conf"
- declare default_filter='filter_by_playlist'
- declare track_format='[[[%artist% / ][[(%date%) ]%album% / ][[%track% - ][%title%]]]|%file%]'
- declare -r album_listing="mpc search -f '%album%\t%title%' artist {} | awk -F'\t' '{ if(album != \$1) { album=\$1; print album } printf \" %s\n\", \$2 }'"
- declare -a config_err
-
- declare key_bindings
- declare -A bindings
- bindings=(
- [playlist]='f1'
- [track]='f2'
- [artist]='f3'
- [genre]='f4'
- [findadd]='ctrl-space'
- )
-
- do_binding() {
- local b
- b=$(action_from_keybind "$1")
- shift
- case "$b" in
- playlist) filter_by_playlist ;;
- track) filter_by_songs ;;
- artist) filter_by_artists ;;
- genre) filter_by_genres ;;
- *) [[ -n $1 ]] && { "$@"; return 0; } ;;
- esac
- return 1
- }
-
- action_from_keybind() {
- for a in "${!bindings[@]}"; do
- if [[ $1 == "${bindings[$a]}" ]]; then
- printf "$a"
- return 0
- fi
- done
- return 1
- }
-
- declare -A colors
- colors[red]=$(tput setaf 1)
- colors[green]=$(tput setaf 2)
- colors[blue]=$(tput setaf 4)
- colors[reset]=$(tput sgr0)
-
- info() {
- color green "$@" >&2
- }
-
- color() {
- local c
- c="$1"
- shift
- printf '%s' "${colors[$c]}"
- printf '%s\n' "$@"
- printf '%s' "${colors[reset]}"
- }
-
- err() {
- color red "$@" >&2
- }
-
- die() {
- [[ -n "$1" ]] && err "$*"
- exit 1
- }
-
- has() {
- local loud=0
- if [[ $1 == '-v' ]]; then
- loud=1
- shift
- fi
- for c; do c="${c%% *}"
- if ! command -v "$c" &> /dev/null; then
- (( loud > 0 )) && err "$c not found"
- return 1
- fi
- done
- }
-
- is_running() {
- pgrep "$1" &> /dev/null
- }
-
- fzf() {
- local opts
- opts=( +s -e -i --reverse --cycle )
- [[ -v FZMP_FZF_OPTIONS ]] && opts=( $FZMP_FZF_OPTIONS )
- command fzf "${opts[@]}" \
- --inline-info \
- --ansi \
- --no-clear \
- "$@"
- }
-
- parse_config_file() {
- local line key val nr=0 e
- while IFS= read -r line; do
- (( ++nr ))
- [[ -z "$line" || "$line" = '#'* ]] && continue
- read -r key <<< "${line%% *}"
- read -r val <<< "${line#* }"
- if [[ -z "$val" ]]; then
- config_err+=( "missing value for \"$key\" in config file on line $nr" )
- continue
- fi
- case "$key" in
- full_song_format) track_format="$val" ;;
- fzf_options) [[ -z FZMP_FZF_OPTIONS ]] && FZMP_FZF_OPTIONS="$val" ;;
- default_view)
- if [[ "$val" =~ ^playlist$|^songs$|^artists$|^genres$ ]]; then
- default_filter="filter_by_$val"
- else
- config_err+=( "unknown format \"$val\" in config file on line $nr" )
- config_err+=( "default_view must be 'playlist' 'songs' 'artists' or 'genres'" )
- fi ;;
- playlist_view_key) bindings[playlist]="$val" ;;
- artist_view_key) bindings[artist]="$val" ;;
- track_view_key) bindings[track]="$val" ;;
- genre_view_key) bindings[genre]="$val" ;;
- findadd_key) bindings[findadd]="$val" ;;
- *) config_err+=( "unknown key \"$key\" in config file on line $nr" )
- esac
- done < "$config_file"
- if (( ${#config_err[@]} > 0 )); then
- err 'there were errors parsing config file:'
- for e in "${config_err[@]}"; do
- err " $e"
- done
- fi
- }
-
- filter_by_songs() {
- local choice
- mapfile -t choice < <(mpc search -f "%file%\t$track_format" filename '' |
- fzf --prompt='songs > ' \
- --multi \
- --with-nth='2..' \
- --delimiter='\t' \
- --expect="${key_bindings},enter" |
- cut -f1)
- case "${choice[0]}" in
- 'enter') printf '%s\n' "${choice[@]:1}" | add_songs play ;;
- *) do_binding "${choice[0]}" || exit
- esac
- }
-
- filter_by_genres() {
- local choice
- mapfile -t choice < <(mpc search -f '%genre%' genre '' |
- awk 'NF' | sort | uniq -c | sort -rn |
- fzf --prompt='genres > ' \
- --preview='mpc search -f "%artist%" genre {2..} | sort -u' \
- --bind="${bindings[findadd]}:execute-silent:mpc findadd genre {2..}" \
- --expect="${key_bindings},enter" |
- sed -r 's/^\s*[0-9]+\s*//')
- (( ${#choice[@]} > 0 )) || die
- case "${choice[0]}" in
- enter) filter_by_artist_from_genre "${choice[1]}" ;;
- *) do_binding "${choice[0]}" || "$default_filter" ;;
- esac
- }
-
- filter_by_artist_from_genre() {
- local artist genre choice
- genre="$1"
- mapfile -t choice < <(mpc search -f '%artist%' genre "$genre" |
- sort -u | awk 'NF' | sort -u |
- fzf --prompt="$genre > " \
- --preview="$album_listing" \
- --expect="${key_bindings},enter" \
- --bind="${bindings[findadd]}:execute-silent:mpc findadd artist {}")
- (( ${#choice[@]} > 0 )) || filter_by_genres
- case "${choice[0]}" in
- enter) filter_by_album_from_artist "${choice[1]}" ;;
- *) do_binding "${choice[0]}" || "$filter_by_genres" ;;
- esac
- }
-
- filter_by_artists() {
- local choice
- mapfile -t choice < <(mpc list artist |
- fzf --prompt='artists > ' \
- --preview="$album_listing" \
- --bind="${bindings[findadd]}:execute-silent:mpc findadd artist {}" \
- --expect="${key_bindings},enter")
- (( ${#choice[@]} > 0 )) || die
- case "${choice[0]}" in
- 'enter') filter_by_album_from_artist "${choice[1]}" ;;
- *) do_binding "${choice[0]}" || "$default_filter" ;;
- esac
- }
-
- filter_by_album_from_artist() {
- local album artist choice
- [[ -z "$1" ]] && filter_by_artists
- artist="$1"
- mapfile -t choice < <(mpc search -f '[(%date%)]\t[%album%]' artist "$artist" |
- sort -h | uniq |
- fzf --prompt="$artist > " \
- --preview="mpc search -f '[[[%track% - ][%title%]]|%file%]' artist '$artist' album {2}" \
- --expect="${key_bindings},enter" \
- --bind="${bindings[findadd]}:execute-silent:mpc findadd album {2..}" \
- --delimiter='\t' |
- cut -f2)
- case "${choice[0]}" in
- 'enter') filter_songs_from_album "$artist" "${choice[1]}" ;;
- *) do_binding "${choice[0]}" || filter_by_artists ;;
- esac
- }
-
- filter_songs_from_album() {
- local album artist choice
- [[ -z "$1" || -z "$2" ]] && exit 999
- artist="$1"
- album="$2"
- mapfile -t choice < <(mpc search -f '%file%\t[[[%track% - ][%title%]]|%file%]' artist "${artist}" album "${album}" |
- fzf --prompt="$artist - $album > " \
- --multi \
- --with-nth='2..' \
- --delimiter='\t' \
- --expect="${key_bindings},enter" |
- cut -f1)
- case "${choice[0]}" in
- 'enter') printf '%s\n' "${choice[@]:1}" | add_songs play ;;
- *) do_binding "${choice[0]}" || filter_by_album_from_artist "$artist" ;;
- esac
- }
-
- filter_by_playlist() {
- local choice
- current_song=$(mpc current -f "$track_format")
- mapfile -t choice < <(mpc playlist -f "%position%\t$track_format" |
- fzf --prompt='playlist > ' \
- --multi \
- ${current_song:+--header="now playing: ${current_song}"} \
- --delimiter='\t' \
- --with-nth='2..' \
- --expect="${key_bindings},>,<,ctrl-d,enter,ctrl-z" |
- cut -f1) || die
- case "${choice[0]}" in
- '>') mpc -q next; filter_by_playlist ;;
- '<') mpc -q prev; filter_by_playlist ;;
- 'ctrl-d') [[ -n "${choice[1]}" ]] && mpc -q del "${choice[@]:1}"& filter_by_playlist ;;
- 'enter') [[ -n "${choice[1]}" ]] && mpc -q play "${choice[@]:1}"& filter_by_playlist ;;
- 'ctrl-z') mpc clear; filter_by_artists ;;
- *) do_binding "${choice[0]}" || exit ;;
- esac
- }
-
- add_songs() {
- mapfile -t songs
- (( "${#songs[@]}" > 0 )) || die
- printf '%s\n' "${songs[@]}" | mpc -q add
- [[ $1 == play ]] || return
- index=$(mpc playlist | wc -l)
- (( ${#songs[@]} > 1 )) &&
- index=$(( index - ${#songs[@]} + 1))
- mpc -q play "$index"
- filter_by_playlist
- }
-
- parse_config_file
- IFS=',' key_bindings="${bindings[*]}"
- key_bindings="${key_bindings/,${bindings[findadd]}}"
- findadd_key="${bindings[findadd]}"
-
- while :; do
- case "$1" in
- -A|--all) default_filter='filter_by_songs'; shift ;;
- -a|--artist) default_filter='filter_by_artists'; shift ;;
- -p|--playlist) default_filter='filter_by_playlist'; shift ;;
- -g|--genre) default_filter='filter_by_genres'; shift ;;
- -h|--help) usage; exit ;;
- *) break
- esac
- done
-
- has -v fzf mpc || die
- is_running mpd || [[ -v MPD_HOST ]] || die "can't connect to mpd"
-
- finish() {
- tput rmcup
- }
-
- trap finish EXIT SIGINT SIGTERM
- $default_filter
|