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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. C-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
  33. track_view_key
  34. artist_view_key
  35. genre_view_key
  36. allows customizing which keys fire the different views
  37. findadd_key
  38. adds all songs by artist/genre/album
  39. defaults to Ctrl-x
  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. or add this to your FZF_DEFAULT_OPTS
  46. It also helps to have a bind for toggle-all, e.g.
  47. +s -e -i --reverse --cycle --bind=`:jump --bind="Ctrl-A:toggle-all"
  48. This can also be overridden with the environment variable FZMP_FZF_OPTIONS
  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-x'
  64. )
  65. do_binding() {
  66. local b
  67. b=$(action_from_keybind "$1")
  68. shift
  69. echo "$b"
  70. case "$b" in
  71. playlist) filter_by_playlist ;;
  72. track) filter_by_songs ;;
  73. artist) filter_by_artists ;;
  74. genre) filter_by_genres ;;
  75. *) [[ -n $1 ]] && { "$@"; return 0; } ;;
  76. esac
  77. return 1
  78. }
  79. action_from_keybind() {
  80. for a in "${!bindings[@]}"; do
  81. if [[ $1 == "${bindings[$a]}" ]]; then
  82. printf "$a"
  83. return 0
  84. fi
  85. done
  86. return 1
  87. }
  88. declare -A colors
  89. colors[red]=$(tput setaf 1)
  90. colors[green]=$(tput setaf 2)
  91. colors[blue]=$(tput setaf 4)
  92. colors[reset]=$(tput sgr0)
  93. info() {
  94. color green "$@" >&2
  95. }
  96. color() {
  97. local c
  98. c="$1"
  99. shift
  100. printf '%s' "${colors[$c]}"
  101. printf '%s\n' "$@"
  102. printf '%s' "${colors[reset]}"
  103. }
  104. err() {
  105. color red "$@" >&2
  106. }
  107. die() {
  108. [[ -n "$1" ]] && err "$*"
  109. exit 1
  110. }
  111. has() {
  112. local loud=0
  113. if [[ $1 == '-v' ]]; then
  114. loud=1
  115. shift
  116. fi
  117. for c; do c="${c%% *}"
  118. if ! command -v "$c" &> /dev/null; then
  119. (( loud > 0 )) && err "$c not found"
  120. return 1
  121. fi
  122. done
  123. }
  124. is_running() {
  125. pgrep "$1" &> /dev/null
  126. }
  127. fzf() {
  128. local opts
  129. opts=( +s -e -i --reverse --cycle )
  130. [[ -v FZMP_FZF_OPTIONS ]] && opts=( $FZMP_FZF_OPTIONS )
  131. command fzf "${opts[@]}" \
  132. --inline-info \
  133. --ansi \
  134. --no-clear \
  135. "$@"
  136. }
  137. parse_config_file() {
  138. local line key val nr=0 e
  139. while IFS= read -r line; do
  140. (( ++nr ))
  141. [[ -z "$line" || "$line" = '#'* ]] && continue
  142. read -r key <<< "${line%% *}"
  143. read -r val <<< "${line#* }"
  144. if [[ -z "$val" ]]; then
  145. config_err+=( "missing value for \"$key\" in config file on line $nr" )
  146. continue
  147. fi
  148. case "$key" in
  149. full_song_format) track_format="$val" ;;
  150. fzf_options) [[ -z FZMP_FZF_OPTIONS ]] && FZMP_FZF_OPTIONS="$val" ;;
  151. default_view)
  152. if [[ "$val" =~ ^playlist$|^songs$|^artists$|^genres$ ]]; then
  153. default_filter="filter_by_$val"
  154. else
  155. config_err+=( "unknown format \"$val\" in config file on line $nr" )
  156. config_err+=( "default_view must be 'playlist' 'songs' 'artists' or 'genres'" )
  157. fi ;;
  158. playlist_view_key) bindings[playlist]="$val" ;;
  159. artist_view_key) bindings[artist]="$val" ;;
  160. track_view_key) bindings[track]="$val" ;;
  161. genre_view_key) bindings[genre]="$val" ;;
  162. findadd_key) bindings[findadd]="$val" ;;
  163. *) config_err+=( "unknown key \"$key\" in config file on line $nr" )
  164. esac
  165. done < "$config_file"
  166. IFS=',' key_bindings="${bindings[*]}"
  167. findadd_key="${bindings[findadd]}"
  168. if (( ${#config_err[@]} > 0 )); then
  169. err 'there were errors parsing config file:'
  170. for e in "${config_err[@]}"; do
  171. err " $e"
  172. done
  173. fi
  174. }
  175. filter_by_songs() {
  176. local choice
  177. mapfile -t choice < <(mpc search -f "%file%\t$track_format" filename '' |
  178. fzf --prompt='songs > ' \
  179. --multi \
  180. --with-nth='2..' \
  181. --delimiter='\t' \
  182. --expect="${key_bindings},enter" |
  183. cut -f1)
  184. case "${choice[0]}" in
  185. 'enter') printf '%s\n' "${choice[@]:1}" | add_songs play ;;
  186. *) do_binding "${choice[0]}" || exit
  187. esac
  188. }
  189. filter_by_genres() {
  190. local choice
  191. mapfile -t choice < <(mpc search -f '%genre%' genre '' |
  192. awk 'NF' | sort | uniq -ic | sort -rn |
  193. fzf --prompt='genres > ' \
  194. --preview='mpc search -f "%artist%" genre {2..} | sort -u' \
  195. --bind="$findadd_key:execute-silent:mpc findadd genre {2..}" \
  196. --expect="${key_bindings},enter" |
  197. sed -r 's/^\s*[0-9]+\s*//')
  198. (( ${#choice[@]} > 0 )) || die
  199. case "${choice[0]}" in
  200. enter) filter_by_artist_from_genre "${choice[1]}" ;;
  201. *) do_binding "${choice[0]}" || exit ;;
  202. esac
  203. }
  204. filter_by_artist_from_genre() {
  205. local artist genre choice
  206. genre="$1"
  207. mapfile -t choice < <(mpc search -f '%artist%' genre "$genre" |
  208. sort -u | awk 'NF' | sort -u |
  209. fzf --prompt="$genre > " \
  210. --preview="$album_listing" \
  211. --expect="${key_bindings},enter" \
  212. --bind="$findadd_key:execute-silent:mpc findadd artist {}")
  213. (( ${#choice[@]} > 0 )) || filter_by_genres
  214. case "${choice[0]}" in
  215. enter) filter_by_album_from_artist "${choice[1]}" ;;
  216. *) do_binding "${choice[0]}" || "$filter_by_genres" ;;
  217. esac
  218. }
  219. filter_by_artists() {
  220. local choice
  221. mapfile -t choice < <(mpc list artist |
  222. fzf --prompt='artists > ' \
  223. --preview="$album_listing" \
  224. --bind="$findadd_key:execute-silent:mpc findadd artist {}" \
  225. --expect="${key_bindings},enter")
  226. (( ${#choice[@]} > 0 )) || die
  227. case "${choice[0]}" in
  228. 'enter') filter_by_album_from_artist "${choice[1]}" ;;
  229. *) do_binding "${choice[0]}" || "$default_filter" ;;
  230. esac
  231. }
  232. filter_by_album_from_artist() {
  233. local album artist choice
  234. [[ -z "$1" ]] && filter_by_artists
  235. artist="$1"
  236. mapfile -t choice < <(mpc search -f '[(%date%)]\t[%album%]' artist "$artist" |
  237. sort -h | uniq |
  238. fzf --prompt="$artist > " \
  239. --preview="mpc search -f '[[[%track% - ][%title%]]|%file%]' artist '$artist' album {2}" \
  240. --expect="${key_bindings},enter" \
  241. --bind="$findadd_key:execute-silent:mpc findadd album {2..}" \
  242. --delimiter='\t' |
  243. cut -f2)
  244. case "${choice[0]}" in
  245. 'enter') filter_songs_from_album "$artist" "${choice[1]}" ;;
  246. *) do_binding "${choice[0]}" || filter_by_artists ;;
  247. esac
  248. }
  249. filter_songs_from_album() {
  250. local album artist choice
  251. [[ -z "$1" || -z "$2" ]] && die
  252. artist="$1"
  253. album="$2"
  254. mapfile -t choice < <(mpc search -f '%file%\t[[[%track% - ][%title%]]|%file%]' artist "${artist}" album "${album}" |
  255. fzf --prompt="$artist - $album > " \
  256. --multi \
  257. --with-nth='2..' \
  258. --delimiter='\t' \
  259. --expect='f1,f2,f3,enter' |
  260. cut -f1)
  261. case "${choice[0]}" in
  262. 'enter') printf '%s\n' "${choice[@]:1}" | add_songs play ;;
  263. *) do_binding "${choice[0]}" || filter_by_album_from_artist "$artist" ;;
  264. esac
  265. }
  266. filter_by_playlist() {
  267. local choice
  268. current_song=$(mpc current -f "$track_format")
  269. mapfile -t choice < <(mpc playlist -f "%position%\t$track_format" |
  270. fzf --prompt='playlist > ' \
  271. --multi \
  272. ${current_song:+--header="now playing: ${current_song}"} \
  273. --delimiter='\t' \
  274. --with-nth='2..' \
  275. --expect="${key_bindings},>,<,ctrl-d,enter,ctrl-z" |
  276. cut -f1) || die
  277. case "${choice[0]}" in
  278. '>') mpc -q next; filter_by_playlist ;;
  279. '<') mpc -q prev; filter_by_playlist ;;
  280. 'ctrl-d') [[ -n "${choice[1]}" ]] && mpc -q del "${choice[@]:1}"& filter_by_playlist ;;
  281. 'enter') [[ -n "${choice[1]}" ]] && mpc -q play "${choice[@]:1}"& filter_by_playlist ;;
  282. 'ctrl-z') mpc clear; filter_by_artists ;;
  283. *) do_binding "${choice[0]}" || exit ;;
  284. esac
  285. }
  286. add_songs() {
  287. mapfile -t songs
  288. (( "${#songs[@]}" > 0 )) || die
  289. printf '%s\n' "${songs[@]}" | mpc -q add
  290. [[ $1 == play ]] || return
  291. index=$(mpc playlist | wc -l)
  292. (( ${#songs[@]} > 1 )) &&
  293. index=$(( index - ${#songs[@]} + 1))
  294. mpc -q play "$index"
  295. filter_by_playlist
  296. }
  297. [[ -s "$config_file" ]] && parse_config_file
  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