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.

195 lines
5.8 KiB

#!/usr/bin/env bash
declare -r esc=$(printf '\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 track_format='[[[%artist% / ][[(%date%) ]%album% / ][[%track% - ][%title%]]]|%file%]'
declare filter='filterAllSongs'
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'
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
}
isRunning() {
pgrep "$1" &> /dev/null
}
err() {
printf "${c_red}%s${c_reset}\n" "$*" >&2
}
die() {
[[ -n "$1" ]] && err "$*"
exit 1
}
has() {
local verbose=false
if [[ $1 == '-v' ]]; then
verbose=true
shift
fi
for c in "$@"; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
[[ "$verbose" == true ]] && err "$c not found"
return 1
fi
done
}
fzf() {
command fzf ${FZMP_FZF_OPTIONS:-+s -e -i --reverse --cycle} --inline-info --ansi "$@"
}
parseConfigFile() {
local default_view format fzf_options
source "$config_file" || die 'error reading configuration file'
case "$default_view" in
playlist) filter='filterPlaylist' ;;
songs) filter='filterAllSongs' ;;
artists) filter='filterByArtist' ;;
esac
[[ -n "$format" ]] && track_format="$format"
[[ ! -v FZMP_FZF_OPTIONS && -n "$fzf_options" ]] && FZMP_FZF_OPTIONS="$fzf_options"
}
filterAllSongs() {
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') filterByArtist ;;
'f3') filterPlaylist ;;
'enter') printf '%s\n' "${choice[@]:1}" | playSongs ;;
esac
}
filterByArtist() {
local choice
mapfile -t choice < <(mpc list artist | sort -h | fzf --expect='f1,f3,enter')
(( "${#choice[@]}" > 0 )) || die
case "${choice[0]}" in
'f1') filterAllSongs ;;
'f3') filterPlaylist ;;
'enter') filterByAlbumFromArtist "${choice[1]}" ;;
esac
}
filterByAlbumFromArtist() {
local album artist choice
[[ -z "$1" ]] && filterByArtist
artist="$1"
mapfile -t choice < <(mpc search -f '[(%date%)\t][%album%]' artist "${artist}" |
sort -h | uniq |
fzf --prompt="$artist > " \
--expect='f1,f3,enter' \
--bind='Ctrl-A:select-all' |
cut -f2)
case "${choice[0]}" in
'f1') filterAllSongs ;;
'f3') filterPlaylist ;;
'enter') filterSongsFromAlbum "$artist" "${choice[1]}" ;;
*) filterByArtist ;;
esac
}
filterSongsFromAlbum() {
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') filterAllSongs ;;
'f3') filterPlaylist ;;
'enter') printf '%s\n' "${choice[@]:1}" | playSongs ;;
*) filterByAlbumFromArtist "$artist" ;;
esac
}
filterPlaylist() {
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') filterAllSongs ;;
'f2') filterByArtist ;;
'>') mpc -q next ; filterPlaylist ;;
'<') mpc -q prev ; filterPlaylist ;;
'ctrl-d') [[ -n "${choice[1]}" ]] && mpc -q del "${choice[@]:1}" ; filterPlaylist ;;
'enter') [[ -n "${choice[1]}" ]] && mpc -q play "${choice[@]:1}" ; filterPlaylist ;;
esac
}
playSongs() {
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"
filterPlaylist
}
[[ -s "$config_file" ]] && parseConfigFile
while :; do
case "$1" in
-A|--all) filter='filterAllSongs' ; shift ;;
-a|--artist) filter='filterByArtist' ; shift ;;
-p|--playlist) filter='filterPlaylist' ; shift ;;
-h|--help) usage ; exit ;;
*) break
esac
done
has -v fzf mpc || die
isRunning mpd || [[ -n "$MPD_HOST" ]] || die "can't connect to mpd"
$filter