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.

fzmp 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #!/usr/bin/env bash
  2. usage() {
  3. LESS=-FEXR less <<'HELP'
  4. fzmp [OPTIONS]
  5. OPTIONS:
  6. -A --all
  7. search all songs in the library (or F1 when running)
  8. -a --artist
  9. search artist then filter by album (or F2 when running)
  10. -p --playlist
  11. search the current playlist (or F3 when running)
  12. playlist view has the following keybinds:
  13. > go to the next song in the playlist
  14. < go to the previous song in the playlist
  15. Ctrl-d delete the selected songs from the playlist
  16. -g --genre
  17. list genres (or F4 when running)
  18. -h --help
  19. print this help
  20. CONFIGURATION:
  21. A configuration file can be defined at $XDG_CONFIG_DIR (defaults to ~/.config)
  22. If a line begins with '#' it is treated as a comment and ignored
  23. The configuration file reads the following options:
  24. default_view
  25. Must be 'artists' 'songs' 'playlist' or 'genres'
  26. full_song_format
  27. A format string to be passed directly to `mpc format -f` in
  28. 'playlist' and 'all' views. Defaults to:
  29. [[[%artist% / ][[(%date%) ]%album% / ][[%track% - ][%title%]]]|%file%]
  30. For colorized output try:
  31. [[[\e\[32m%artist%\e\[0m / ][\e\[31m[(%date%) ]%album%\e\[0m / ][\e\[34m[%track% - ][%title%]\e\[0m]]|%file%]
  32. playlist_view_key (default F1)
  33. track_view_key (default F2)
  34. artist_view_key (default F3)
  35. genre_view_key (default F4)
  36. allows customizing which keys fire the different views
  37. findadd_key
  38. adds all songs under the cursor by artist/genre/album
  39. defaults to ctrl-space
  40. fzf_options
  41. Command line options to be passed directly to fzf.
  42. Changing this will override the default options: +s -e -i --reverse --cycle
  43. To use the jump feature of fzf you can try:
  44. +s -e -i --reverse --cycle --bind=`:jump
  45. It also helps to have a bind for toggle-all, e.g.
  46. +s -e -i --reverse --cycle --bind=`:jump --bind="ctrl-t:toggle-all"
  47. individual sessions can override with the environment variable FZMP_FZF_OPTIONS
  48. fzmp will also inherit options from FZF_DEFAULT_OPTS
  49. HELP
  50. }
  51. declare -r config_file="${XDG_CONFIG_DIR:-$HOME/.config}/fzmp/conf"
  52. declare default_filter='filter_by_playlist'
  53. declare track_format='[[[%artist% / ][[(%date%) ]%album% / ][[%track% - ][%title%]]]|%file%]'
  54. 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 }'"
  55. declare -a config_err
  56. declare key_bindings
  57. declare -A bindings
  58. bindings=(
  59. [playlist]='f1'
  60. [track]='f2'
  61. [artist]='f3'
  62. [genre]='f4'
  63. [findadd]='ctrl-space'
  64. )
  65. do_binding() {
  66. local b
  67. b=$(action_from_keybind "$1")
  68. shift
  69. case "$b" in
  70. playlist) filter_by_playlist ;;
  71. track) filter_by_songs ;;
  72. artist) filter_by_artists ;;
  73. genre) filter_by_genres ;;
  74. *) [[ -n $1 ]] && { "$@"; return 0; } ;;
  75. esac
  76. return 1
  77. }
  78. action_from_keybind() {
  79. for a in "${!bindings[@]}"; do
  80. if [[ $1 == "${bindings[$a]}" ]]; then
  81. printf "$a"
  82. return 0
  83. fi
  84. done
  85. return 1
  86. }
  87. declare -A colors
  88. colors[red]=$(tput setaf 1)
  89. colors[green]=$(tput setaf 2)
  90. colors[blue]=$(tput setaf 4)
  91. colors[reset]=$(tput sgr0)
  92. info() {
  93. color green "$@" >&2
  94. }
  95. color() {
  96. local c
  97. c="$1"
  98. shift
  99. printf '%s' "${colors[$c]}"
  100. printf '%s\n' "$@"
  101. printf '%s' "${colors[reset]}"
  102. }
  103. err() {
  104. color red "$@" >&2
  105. }
  106. die() {
  107. [[ -n "$1" ]] && err "$*"
  108. exit 1
  109. }
  110. has() {
  111. local loud=0
  112. if [[ $1 == '-v' ]]; then
  113. loud=1
  114. shift
  115. fi
  116. for c; do c="${c%% *}"
  117. if ! command -v "$c" &> /dev/null; then
  118. (( loud > 0 )) && err "$c not found"
  119. return 1
  120. fi
  121. done
  122. }
  123. is_running() {
  124. pgrep "$1" &> /dev/null
  125. }
  126. fzf() {
  127. local opts
  128. opts=( +s -e -i --reverse --cycle )
  129. [[ -v FZMP_FZF_OPTIONS ]] && opts=( $FZMP_FZF_OPTIONS )
  130. command fzf "${opts[@]}" \
  131. --inline-info \
  132. --ansi \
  133. --no-clear \
  134. "$@"
  135. }
  136. parse_config_file() {
  137. local line key val nr=0 e
  138. while IFS= read -r line; do
  139. (( ++nr ))
  140. [[ -z "$line" || "$line" = '#'* ]] && continue
  141. read -r key <<< "${line%% *}"
  142. read -r val <<< "${line#* }"
  143. if [[ -z "$val" ]]; then
  144. config_err+=( "missing value for \"$key\" in config file on line $nr" )
  145. continue
  146. fi
  147. case "$key" in
  148. full_song_format) track_format="$val" ;;
  149. fzf_options) [[ -z FZMP_FZF_OPTIONS ]] && FZMP_FZF_OPTIONS="$val" ;;
  150. default_view)
  151. if [[ "$val" =~ ^playlist$|^songs$|^artists$|^genres$ ]]; then
  152. default_filter="filter_by_$val"
  153. else
  154. config_err+=( "unknown format \"$val\" in config file on line $nr" )
  155. config_err+=( "default_view must be 'playlist' 'songs' 'artists' or 'genres'" )
  156. fi ;;
  157. playlist_view_key) bindings[playlist]="$val" ;;
  158. artist_view_key) bindings[artist]="$val" ;;
  159. track_view_key) bindings[track]="$val" ;;
  160. genre_view_key) bindings[genre]="$val" ;;
  161. findadd_key) bindings[findadd]="$val" ;;
  162. *) config_err+=( "unknown key \"$key\" in config file on line $nr" )
  163. esac
  164. done < "$config_file"
  165. if (( ${#config_err[@]} > 0 )); then
  166. err 'there were errors parsing config file:'
  167. for e in "${config_err[@]}"; do
  168. err " $e"
  169. done
  170. fi
  171. }
  172. filter_by_songs() {
  173. local choice
  174. mapfile -t choice < <(mpc search -f "%file%\t$track_format" filename '' |
  175. fzf --prompt='songs > ' \
  176. --multi \
  177. --with-nth='2..' \
  178. --delimiter='\t' \
  179. --expect="${key_bindings},enter" |
  180. cut -f1)
  181. case "${choice[0]}" in
  182. 'enter') printf '%s\n' "${choice[@]:1}" | add_songs play ;;
  183. *) do_binding "${choice[0]}" || exit
  184. esac
  185. }
  186. filter_by_genres() {
  187. local choice
  188. mapfile -t choice < <(mpc search -f '%genre%' genre '' |
  189. awk 'NF' | sort | uniq -c | sort -rn |
  190. fzf --prompt='genres > ' \
  191. --preview='mpc search -f "%artist%" genre {2..} | sort -u' \
  192. --bind="${bindings[findadd]}:execute-silent:mpc findadd genre {2..}" \
  193. --expect="${key_bindings},enter" |
  194. sed -r 's/^\s*[0-9]+\s*//')
  195. (( ${#choice[@]} > 0 )) || die
  196. case "${choice[0]}" in
  197. enter) filter_by_artist_from_genre "${choice[1]}" ;;
  198. *) do_binding "${choice[0]}" || "$default_filter" ;;
  199. esac
  200. }
  201. filter_by_artist_from_genre() {
  202. local artist genre choice
  203. genre="$1"
  204. mapfile -t choice < <(mpc search -f '%artist%' genre "$genre" |
  205. sort -u | awk 'NF' | sort -u |
  206. fzf --prompt="$genre > " \
  207. --preview="$album_listing" \
  208. --expect="${key_bindings},enter" \
  209. --bind="${bindings[findadd]}:execute-silent:mpc findadd artist {}")
  210. (( ${#choice[@]} > 0 )) || filter_by_genres
  211. case "${choice[0]}" in
  212. enter) filter_by_album_from_artist "${choice[1]}" ;;
  213. *) do_binding "${choice[0]}" || "$filter_by_genres" ;;
  214. esac
  215. }
  216. filter_by_artists() {
  217. local choice
  218. mapfile -t choice < <(mpc list artist |
  219. fzf --prompt='artists > ' \
  220. --preview="$album_listing" \
  221. --bind="${bindings[findadd]}:execute-silent:mpc findadd artist {}" \
  222. --expect="${key_bindings},enter")
  223. (( ${#choice[@]} > 0 )) || die
  224. case "${choice[0]}" in
  225. 'enter') filter_by_album_from_artist "${choice[1]}" ;;
  226. *) do_binding "${choice[0]}" || "$default_filter" ;;
  227. esac
  228. }
  229. filter_by_album_from_artist() {
  230. local album artist choice
  231. [[ -z "$1" ]] && filter_by_artists
  232. artist="$1"
  233. mapfile -t choice < <(mpc search -f '[(%date%)]\t[%album%]' artist "$artist" |
  234. sort -h | uniq |
  235. fzf --prompt="$artist > " \
  236. --preview="mpc search -f '[[[%track% - ][%title%]]|%file%]' artist '$artist' album {2}" \
  237. --expect="${key_bindings},enter" \
  238. --bind="${bindings[findadd]}:execute-silent:mpc findadd album {2..}" \
  239. --delimiter='\t' |
  240. cut -f2)
  241. case "${choice[0]}" in
  242. 'enter') filter_songs_from_album "$artist" "${choice[1]}" ;;
  243. *) do_binding "${choice[0]}" || filter_by_artists ;;
  244. esac
  245. }
  246. filter_songs_from_album() {
  247. local album artist choice
  248. [[ -z "$1" || -z "$2" ]] && exit 999
  249. artist="$1"
  250. album="$2"
  251. mapfile -t choice < <(mpc search -f '%file%\t[[[%track% - ][%title%]]|%file%]' artist "${artist}" album "${album}" |
  252. fzf --prompt="$artist - $album > " \
  253. --multi \
  254. --with-nth='2..' \
  255. --delimiter='\t' \
  256. --expect="${key_bindings},enter" |
  257. cut -f1)
  258. case "${choice[0]}" in
  259. 'enter') printf '%s\n' "${choice[@]:1}" | add_songs play ;;
  260. *) do_binding "${choice[0]}" || filter_by_album_from_artist "$artist" ;;
  261. esac
  262. }
  263. filter_by_playlist() {
  264. local choice
  265. current_song=$(mpc current -f "$track_format")
  266. mapfile -t choice < <(mpc playlist -f "%position%\t$track_format" |
  267. fzf --prompt='playlist > ' \
  268. --multi \
  269. ${current_song:+--header="now playing: ${current_song}"} \
  270. --delimiter='\t' \
  271. --with-nth='2..' \
  272. --expect="${key_bindings},>,<,ctrl-d,enter,ctrl-z" |
  273. cut -f1) || die
  274. case "${choice[0]}" in
  275. '>') mpc -q next; filter_by_playlist ;;
  276. '<') mpc -q prev; filter_by_playlist ;;
  277. 'ctrl-d') [[ -n "${choice[1]}" ]] && mpc -q del "${choice[@]:1}"& filter_by_playlist ;;
  278. 'enter') [[ -n "${choice[1]}" ]] && mpc -q play "${choice[@]:1}"& filter_by_playlist ;;
  279. 'ctrl-z') mpc clear; filter_by_artists ;;
  280. *) do_binding "${choice[0]}" || exit ;;
  281. esac
  282. }
  283. add_songs() {
  284. mapfile -t songs
  285. (( "${#songs[@]}" > 0 )) || die
  286. printf '%s\n' "${songs[@]}" | mpc -q add
  287. [[ $1 == play ]] || return
  288. index=$(mpc playlist | wc -l)
  289. (( ${#songs[@]} > 1 )) &&
  290. index=$(( index - ${#songs[@]} + 1))
  291. mpc -q play "$index"
  292. filter_by_playlist
  293. }
  294. parse_config_file
  295. IFS=',' key_bindings="${bindings[*]}"
  296. key_bindings="${key_bindings/,${bindings[findadd]}}"
  297. findadd_key="${bindings[findadd]}"
  298. while :; do
  299. case "$1" in
  300. -A|--all) default_filter='filter_by_songs'; shift ;;
  301. -a|--artist) default_filter='filter_by_artists'; shift ;;
  302. -p|--playlist) default_filter='filter_by_playlist'; shift ;;
  303. -g|--genre) default_filter='filter_by_genres'; shift ;;
  304. -h|--help) usage; exit ;;
  305. *) break
  306. esac
  307. done
  308. has -v fzf mpc || die
  309. is_running mpd || [[ -v MPD_HOST ]] || die "can't connect to mpd"
  310. finish() {
  311. tput rmcup
  312. }
  313. trap finish EXIT SIGINT SIGTERM
  314. $default_filter