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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 find -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=( --reverse --cycle --height=100% )
  136. [[ -v FZMP_FZF_OPTIONS ]] && opts=( $FZMP_FZF_OPTIONS )
  137. if has fzf; then
  138. command fzf \
  139. --inline-info \
  140. --ansi \
  141. --no-clear \
  142. "${opts[@]}" "$@"
  143. elif has sk; then
  144. command sk \
  145. --inline-info \
  146. --ansi \
  147. "${opts[@]}" "$@"
  148. else
  149. err 'no filter found'
  150. fi
  151. }
  152. parse_config_file() {
  153. local line key val nr=0 e
  154. while IFS= read -r line; do
  155. (( ++nr ))
  156. [[ -z "$line" || "$line" = '#'* ]] && continue
  157. read -r key <<< "${line%% *}"
  158. read -r val <<< "${line#* }"
  159. if [[ -z "$val" ]]; then
  160. config_err+=( "missing value for \"$key\" in config file on line $nr" )
  161. continue
  162. fi
  163. case "$key" in
  164. full_song_format) track_format="$val" ;;
  165. fzf_options) FZMP_FZF_OPTIONS="$val" ;;
  166. default_view)
  167. if [[ "$val" =~ ^playlist$|^songs$|^artists$|^genres$|^playlists$ ]]; then
  168. default_filter="filter_by_$val"
  169. else
  170. config_err+=( "unknown format \"$val\" in config file on line $nr" )
  171. config_err+=( "default_view must be 'playlist' 'songs' 'artists' 'genres' or 'playlists'" )
  172. fi ;;
  173. playlist_view_key) bindings[playlist]="$val" ;;
  174. artist_view_key) bindings[artist]="$val" ;;
  175. track_view_key) bindings[track]="$val" ;;
  176. genre_view_key) bindings[genre]="$val" ;;
  177. playlists_view_key) bindings[playlists]="$val" ;;
  178. findadd_key) bindings[findadd]="$val" ;;
  179. *) config_err+=( "unknown key \"$key\" in config file on line $nr" )
  180. esac
  181. done < "$config_file"
  182. if (( ${#config_err[@]} > 0 )); then
  183. err 'there were errors parsing config file:'
  184. for e in "${config_err[@]}"; do
  185. err " $e"
  186. done
  187. fi
  188. }
  189. filter_by_songs() {
  190. local choice
  191. mapfile -t choice < <(mpc search -f "%file%\t$track_format" filename '' |
  192. fzf --prompt='songs > ' \
  193. --multi \
  194. --with-nth='2..' \
  195. --delimiter='\t' \
  196. --bind='ctrl-space:execute-silent:printf "%s\n" {+} | cut -f1 | mpc add' \
  197. --expect="${key_bindings},enter" |
  198. cut -f1)
  199. case "${choice[0]}" in
  200. 'enter') printf '%s\n' "${choice[@]:1}" | add_songs play ;;
  201. *) do_binding "${choice[0]}" || exit
  202. esac
  203. }
  204. filter_by_genres() {
  205. local choice
  206. mapfile -t choice < <(mpc search -f '%genre%' genre '' |
  207. awk 'NF' | sort | uniq -c | sort -rn |
  208. fzf --prompt='genres > ' \
  209. --preview='mpc find -f "%artist%" genre {2..} | sort -u' \
  210. --bind="${bindings[findadd]}:execute-silent:mpc findadd genre {2..}" \
  211. --expect="${key_bindings},enter" |
  212. sed -r 's/^\s*[0-9]+\s*//')
  213. (( ${#choice[@]} > 0 )) || die
  214. case "${choice[0]}" in
  215. enter) filter_by_artist_from_genre "${choice[1]}" ;;
  216. *) do_binding "${choice[0]}" || "$default_filter" ;;
  217. esac
  218. }
  219. filter_by_artist_from_genre() {
  220. local artist genre choice
  221. genre="$1"
  222. mapfile -t choice < <(mpc find -f '%artist%' genre "$genre" |
  223. sort -u | awk 'NF' | sort -u |
  224. fzf --prompt="$genre > " \
  225. --preview="$album_listing" \
  226. --expect="${key_bindings},enter" \
  227. --bind="${bindings[findadd]}:execute-silent:mpc findadd artist {}")
  228. (( ${#choice[@]} > 0 )) || filter_by_genres
  229. case "${choice[0]}" in
  230. enter) filter_by_album_from_artist "${choice[1]}" ;;
  231. *) do_binding "${choice[0]}" || "$filter_by_genres" ;;
  232. esac
  233. }
  234. filter_by_artists() {
  235. local choice
  236. mapfile -t choice < <(mpc list artist |
  237. fzf --prompt='artists > ' \
  238. --preview="$album_listing" \
  239. --bind="${bindings[findadd]}:execute-silent:mpc findadd artist {}" \
  240. --expect="${key_bindings},enter")
  241. (( ${#choice[@]} > 0 )) || die
  242. case "${choice[0]}" in
  243. 'enter') filter_by_album_from_artist "${choice[1]}" ;;
  244. *) do_binding "${choice[0]}" || "$default_filter" ;;
  245. esac
  246. }
  247. filter_by_album_from_artist() {
  248. local album artist choice
  249. [[ -z "$1" ]] && filter_by_artists
  250. artist="$1"
  251. printf -v qartist '%q' "$1"
  252. mapfile -t choice < <(mpc find -f '[(%date%)]\t[%album%]' artist "$artist" |
  253. sort -h | uniq |
  254. fzf --prompt="$artist > " \
  255. --preview="mpc find -f '[[[%track% - ][%title%]]|%file%]' artist ${qartist} album {2}" \
  256. --expect="${key_bindings},enter" \
  257. --bind="${bindings[findadd]}:execute-silent:mpc findadd album {2..} artist ${qartist}" \
  258. --delimiter='\t' |
  259. cut -f2)
  260. case "${choice[0]}" in
  261. 'enter') filter_songs_from_album "$artist" "${choice[1]}" ;;
  262. *) do_binding "${choice[0]}" || filter_by_artists ;;
  263. esac
  264. }
  265. filter_songs_from_album() {
  266. local album artist choice
  267. [[ -z "$1" || -z "$2" ]] && exit 255
  268. artist="$1"
  269. album="$2"
  270. mapfile -t choice < <(mpc find -f '%file%\t[[[%track% - ][%title%]]|%file%]' artist "${artist}" album "${album}" |
  271. fzf --prompt="$artist - $album > " \
  272. --multi \
  273. --with-nth='2..' \
  274. --delimiter='\t' \
  275. --expect="${key_bindings},enter" |
  276. cut -f1)
  277. case "${choice[0]}" in
  278. 'enter') printf '%s\n' "${choice[@]:1}" | add_songs play ;;
  279. *) do_binding "${choice[0]}" || filter_by_album_from_artist "$artist" ;;
  280. esac
  281. }
  282. filter_by_playlist() {
  283. local choice
  284. current_song=$(mpc current -f "$track_format")
  285. mapfile -t choice < <(mpc playlist -f "%position%\t$track_format" |
  286. fzf --prompt='playlist > ' \
  287. --multi \
  288. ${current_song:+--header="now playing: ${current_song}"} \
  289. --delimiter='\t' \
  290. --with-nth='2..' \
  291. --bind='ctrl-p:execute-silent:mpc toggle' \
  292. --expect="${key_bindings},>,<,ctrl-d,enter,ctrl-s" |
  293. cut -f1) || die
  294. case "${choice[0]}" in
  295. '>') mpc -q next; filter_by_playlist ;;
  296. '<') mpc -q prev; filter_by_playlist ;;
  297. 'enter') [[ -n "${choice[1]}" ]] && mpc -q play "${choice[@]:1}" && filter_by_playlist ;;
  298. 'ctrl-d') [[ -n "${choice[1]}" ]] && mpc -q del "${choice[@]:1}" && filter_by_playlist ;;
  299. 'ctrl-s') save_playlist; filter_by_playlist ;;
  300. *) do_binding "${choice[0]}" || exit ;;
  301. esac
  302. }
  303. save_playlist() {
  304. local name playlists confirm
  305. tput clear
  306. # if [[ -z $(mpc playlist) ]]; then
  307. # color red 'cannot save empty playlist'
  308. # sleep 0.7
  309. # return 1
  310. # fi
  311. read -r -e -p 'Enter playlist name: ' name
  312. [[ -z $name ]] && return 1
  313. playlists=$(mpc lsplaylists)
  314. if [[ $playlists = *"$name"* ]]; then
  315. diff -s --suppress-common-lines --color=always --label="$name" --label='playlist' <(mpc playlist -f '%file%' "$name") <(mpc playlist -f '%file%')
  316. read -r -n 1 -p 'Are you sure you want to overwrite this playlist? (press y) ' confirm || return
  317. if [[ ${confirm,} = y ]]; then
  318. mpc rm "$name"
  319. else
  320. return
  321. fi
  322. fi
  323. mpc save "$name"
  324. }
  325. pick_playlist() {
  326. local choice
  327. mapfile -t choice < <(mpc lsplaylists |
  328. fzf --prompt='playlists > ' \
  329. --multi \
  330. --preview='mpc playlist {1}' \
  331. --delimiter='\t' \
  332. --bind='ctrl-space:execute-silent:mpc load {1}' \
  333. --expect="${key_bindings},ctrl-d,enter")
  334. case "${choice[0]}" in
  335. 'enter') mpc playlist -f '%file%' "${choice[1]}" | add_songs play && filter_by_playlist ;;
  336. 'ctrl-d')
  337. tput clear
  338. read -r -n 1 -p 'Are you sure you want to delete this playlist? (press y) ' confirm
  339. if [[ ${confirm} = y ]]; then
  340. mpc rm "${choice[1]}"
  341. pick_playlist
  342. fi ;;
  343. *) do_binding "${choice[0]}" || exit
  344. esac
  345. }
  346. add_songs() {
  347. local songs index
  348. mapfile -t songs
  349. (( "${#songs[@]}" > 0 )) || die
  350. printf '%s\n' "${songs[@]}" | mpc -q add
  351. [[ $1 == play ]] || return
  352. index=$(mpc playlist | wc -l)
  353. if (( ${#songs[@]} > 1 )); then
  354. index=$(( index - ${#songs[@]} + 1))
  355. fi
  356. mpc -q play "$index"
  357. filter_by_playlist
  358. }
  359. finish() {
  360. tput rmcup
  361. }
  362. trap finish EXIT SIGINT SIGTERM
  363. parse_config_file
  364. IFS=',' key_bindings="${bindings[*]}"
  365. key_bindings="${key_bindings/,${bindings[findadd]}}"
  366. findadd_key="${bindings[findadd]}"
  367. while :; do
  368. case "$1" in
  369. -A|--all) default_filter='filter_by_songs'; shift ;;
  370. -a|--artist) default_filter='filter_by_artists'; shift ;;
  371. -p|--playlist) default_filter='filter_by_playlist'; shift ;;
  372. -P|--playlists) default_filter='pick_playlist'; shift ;;
  373. -g|--genre) default_filter='filter_by_genres'; shift ;;
  374. -h|--help) usage; exit ;;
  375. *) break
  376. esac
  377. done
  378. has -v fzf mpc || die
  379. is_running mpd || [[ -v MPD_HOST ]] || die "can't connect to mpd"
  380. tput smcup
  381. "$default_filter"