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.

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