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

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