No Description
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.9KB

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. 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 also 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. 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. key_bindings="${key_bindings/,${bindings[findadd]}}"
  168. findadd_key="${bindings[findadd]}"
  169. if (( ${#config_err[@]} > 0 )); then
  170. err 'there were errors parsing config file:'
  171. for e in "${config_err[@]}"; do
  172. err " $e"
  173. done
  174. fi
  175. }
  176. filter_by_songs() {
  177. local choice
  178. mapfile -t choice < <(mpc search -f "%file%\t$track_format" filename '' |
  179. fzf --prompt='songs > ' \
  180. --multi \
  181. --with-nth='2..' \
  182. --delimiter='\t' \
  183. --expect="${key_bindings},enter" |
  184. cut -f1)
  185. case "${choice[0]}" in
  186. 'enter') printf '%s\n' "${choice[@]:1}" | add_songs play ;;
  187. *) do_binding "${choice[0]}" || exit
  188. esac
  189. }
  190. filter_by_genres() {
  191. local choice
  192. mapfile -t choice < <(mpc search -f '%genre%' genre '' |
  193. awk 'NF' | sort | uniq -c | sort -rn |
  194. fzf --prompt='genres > ' \
  195. --preview='mpc search -f "%artist%" genre {2..} | sort -u' \
  196. --bind="${bindings[findadd]}:execute-silent:mpc findadd genre {2..}" \
  197. --expect="${key_bindings},enter" |
  198. sed -r 's/^\s*[0-9]+\s*//')
  199. (( ${#choice[@]} > 0 )) || die
  200. case "${choice[0]}" in
  201. enter) filter_by_artist_from_genre "${choice[1]}" ;;
  202. *) do_binding "${choice[0]}" || "$default_filter" ;;
  203. esac
  204. }
  205. filter_by_artist_from_genre() {
  206. local artist genre choice
  207. genre="$1"
  208. mapfile -t choice < <(mpc search -f '%artist%' genre "$genre" |
  209. sort -u | awk 'NF' | sort -u |
  210. fzf --prompt="$genre > " \
  211. --preview="$album_listing" \
  212. --expect="${key_bindings},enter" \
  213. --bind="${bindings[findadd]}:execute-silent:mpc findadd artist {}")
  214. (( ${#choice[@]} > 0 )) || filter_by_genres
  215. case "${choice[0]}" in
  216. enter) filter_by_album_from_artist "${choice[1]}" ;;
  217. *) do_binding "${choice[0]}" || "$filter_by_genres" ;;
  218. esac
  219. }
  220. filter_by_artists() {
  221. local choice
  222. mapfile -t choice < <(mpc list artist |
  223. fzf --prompt='artists > ' \
  224. --preview="$album_listing" \
  225. --bind="${bindings[findadd]}:execute-silent:mpc findadd artist {}" \
  226. --expect="${key_bindings},enter")
  227. (( ${#choice[@]} > 0 )) || die
  228. case "${choice[0]}" in
  229. 'enter') filter_by_album_from_artist "${choice[1]}" ;;
  230. *) do_binding "${choice[0]}" || "$default_filter" ;;
  231. esac
  232. }
  233. filter_by_album_from_artist() {
  234. local album artist choice
  235. [[ -z "$1" ]] && filter_by_artists
  236. artist="$1"
  237. mapfile -t choice < <(mpc search -f '[(%date%)]\t[%album%]' artist "$artist" |
  238. sort -h | uniq |
  239. fzf --prompt="$artist > " \
  240. --preview="mpc search -f '[[[%track% - ][%title%]]|%file%]' artist '$artist' album {2}" \
  241. --expect="${key_bindings},enter" \
  242. --bind="${bindings[findadd]}:execute-silent:mpc findadd album {2..}" \
  243. --delimiter='\t' |
  244. cut -f2)
  245. case "${choice[0]}" in
  246. 'enter') filter_songs_from_album "$artist" "${choice[1]}" ;;
  247. *) do_binding "${choice[0]}" || filter_by_artists ;;
  248. esac
  249. }
  250. filter_songs_from_album() {
  251. local album artist choice
  252. [[ -z "$1" || -z "$2" ]] && exit 999
  253. artist="$1"
  254. album="$2"
  255. mapfile -t choice < <(mpc search -f '%file%\t[[[%track% - ][%title%]]|%file%]' artist "${artist}" album "${album}" |
  256. fzf --prompt="$artist - $album > " \
  257. --multi \
  258. --with-nth='2..' \
  259. --delimiter='\t' \
  260. --expect="${key_bindings},enter" |
  261. cut -f1)
  262. case "${choice[0]}" in
  263. 'enter') printf '%s\n' "${choice[@]:1}" | add_songs play ;;
  264. *) do_binding "${choice[0]}" || filter_by_album_from_artist "$artist" ;;
  265. esac
  266. }
  267. filter_by_playlist() {
  268. local choice
  269. current_song=$(mpc current -f "$track_format")
  270. mapfile -t choice < <(mpc playlist -f "%position%\t$track_format" |
  271. fzf --prompt='playlist > ' \
  272. --multi \
  273. ${current_song:+--header="now playing: ${current_song}"} \
  274. --delimiter='\t' \
  275. --with-nth='2..' \
  276. --expect="${key_bindings},>,<,ctrl-d,enter,ctrl-z" |
  277. cut -f1) || die
  278. case "${choice[0]}" in
  279. '>') mpc -q next; filter_by_playlist ;;
  280. '<') mpc -q prev; filter_by_playlist ;;
  281. 'ctrl-d') [[ -n "${choice[1]}" ]] && mpc -q del "${choice[@]:1}"& filter_by_playlist ;;
  282. 'enter') [[ -n "${choice[1]}" ]] && mpc -q play "${choice[@]:1}"& filter_by_playlist ;;
  283. 'ctrl-z') mpc clear; filter_by_artists ;;
  284. *) do_binding "${choice[0]}" || exit ;;
  285. esac
  286. }
  287. add_songs() {
  288. mapfile -t songs
  289. (( "${#songs[@]}" > 0 )) || die
  290. printf '%s\n' "${songs[@]}" | mpc -q add
  291. [[ $1 == play ]] || return
  292. index=$(mpc playlist | wc -l)
  293. (( ${#songs[@]} > 1 )) &&
  294. index=$(( index - ${#songs[@]} + 1))
  295. mpc -q play "$index"
  296. filter_by_playlist
  297. }
  298. [[ -s "$config_file" ]] && parse_config_file
  299. while :; do
  300. case "$1" in
  301. -A|--all) default_filter='filter_by_songs'; shift ;;
  302. -a|--artist) default_filter='filter_by_artists'; shift ;;
  303. -p|--playlist) default_filter='filter_by_playlist'; shift ;;
  304. -g|--genre) default_filter='filter_by_genres'; shift ;;
  305. -h|--help) usage; exit ;;
  306. *) break
  307. esac
  308. done
  309. has -v fzf mpc || die
  310. is_running mpd || [[ -v MPD_HOST ]] || die "can't connect to mpd"
  311. finish() {
  312. tput rmcup
  313. }
  314. trap finish EXIT SIGINT SIGTERM
  315. $default_filter