You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
6.1 KiB

#!/usr/bin/env bash
declare -r esc=$'\033'
declare -r c_reset="${esc}[0m"
declare -r c_red="${esc}[31m"
declare -r config_file="${XDG_CONFIG_DIR:-$HOME/.config}/fzmp/conf"
declare default_filter='filter_all_songs'
declare track_format='[[[%artist% / ][[(%date%) ]%album% / ][[%track% - ][%title%]]]|%file%]'
declare
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
C-d delete the selected songs from the playlist
-h --help print this help
CONFIGURATION:
a configuration file can be defined at $XDG_CONFIG_DIR (defaults to ~/.config)
configuration options must be defined in the format of key=value
the configuration file reads the following variables:
default_view= must be 'artists' 'songs' or 'playlist'
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%]'
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'
this can also be overridden with the environment variable FZMP_FZF_OPTIONS
HELP
}
is_running() {
pgrep "$1" &> /dev/null
}
err() {
printf "${c_red}%s${c_reset}\n" "$*" >&2
}
die() {
[[ -n "$1" ]] && err "$*"
exit 1
}
has() {
local verbose=0
if [[ $1 == '-v' ]]; then
verbose=1
shift
fi
for c; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
(( verbose > 0 )) && err "$c not found"
return 1
fi
done
}
fzf() {
command fzf ${FZMP_FZF_OPTIONS:-+s -e -i --reverse --cycle} --inline-info --ansi "$@"
}
parse_config_file() {
local default_view full_song_format fzf_options
source "$config_file"
[[ -v default_view ]] && case "$default_view" in
playlist) default_filter='filter_playlist' ;;
songs) default_filter='filter_all_songs' ;;
artists) default_filter='filter_by_artist' ;;
esac
[[ -v full_song_format ]] && track_format="$full_song_format"
[[ ! -v FZMP_FZF_OPTIONS && -v fzf_options ]] && FZMP_FZF_OPTIONS="$fzf_options"
}
filter_all_songs() {
local choice
mapfile -t choice < <( mpc search -f "%file%\t$track_format" filename '' |
fzf --multi \
--with-nth='2..' \
--delimiter='\t' \
--expect='f2,f3,enter' |
cut -f1)
case "${choice[0]}" in
'f2') filter_by_artist ;;
'f3') filter_playlist ;;
'enter') printf '%s\n' "${choice[@]:1}" | play_songs ;;
esac
}
filter_by_artist() {
local choice
mapfile -t choice < <(mpc list artist |
sort -h |
fzf \
--preview='mpc list album artist {}' \
--expect='f1,f3,enter')
(( "${#choice[@]}" > 0 )) || die
case "${choice[0]}" in
'f1') filter_all_songs ;;
'f3') filter_playlist ;;
'enter') filter_by_album_from_artist "${choice[1]}" ;;
esac
}
filter_by_album_from_artist() {
local album artist choice
[[ -z "$1" ]] && filter_by_artist
artist="$1"
mapfile -t choice < <(mpc search -f '[(%date%)\t][%album%]' artist "$artist" |
sort -h | uniq |
fzf --prompt="$artist > " \
--preview="album=\$(cut -f2 <<< {}); mpc search -f '[[[%track% - ][%title%]]|%file%]' artist \"$artist\" album \"\$album\"" \
--expect='f1,f3,enter' \
--bind='Ctrl-A:select-all' |
cut -f2)
case "${choice[0]}" in
'f1') filter_all_songs ;;
'f3') filter_playlist ;;
'enter') filter_songs_from_album "$artist" "${choice[1]}" ;;
*) filter_by_artist ;;
esac
}
filter_songs_from_album() {
local album artist choice
[[ -z "$1" || -z "$2" ]] && die
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='f1,f3,enter' \
--bind='Ctrl-A:select-all' |
cut -f1)
case "${choice[0]}" in
'f1') filter_all_songs ;;
'f3') filter_playlist ;;
'enter') printf '%s\n' "${choice[@]:1}" | play_songs ;;
*) filter_by_album_from_artist "$artist" ;;
esac
}
filter_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='f1,f2,>,<,ctrl-d,enter' |
cut -f1) || die
case "${choice[0]}" in
'f1') filter_all_songs ;;
'f2') filter_by_artist ;;
'>') mpc -q next ; filter_playlist ;;
'<') mpc -q prev ; filter_playlist ;;
'ctrl-d') [[ -n "${choice[1]}" ]] && mpc -q del "${choice[@]:1}" ; filter_playlist ;;
'enter') [[ -n "${choice[1]}" ]] && mpc -q play "${choice[@]:1}" ; filter_playlist ;;
esac
}
play_songs() {
mapfile -t songs
(( "${#songs[@]}" > 0 )) || die
printf '%s\n' "${songs[@]}" | mpc -q add
index=$(mpc playlist | wc -l)
(( ${#songs[@]} > 1 )) &&
index=$(( index - ${#songs[@]} + 1))
mpc -q play "$index"
filter_playlist
}
[[ -s "$config_file" ]] && parse_config_file
while :; do
case "$1" in
-A|--all) default_filter='filter_all_songs' ; shift ;;
-a|--artist) default_filter='filter_by_artist' ; shift ;;
-p|--playlist) default_filter='filter_playlist' ; 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"
$default_filter