-
Notifications
You must be signed in to change notification settings - Fork 4
/
p
executable file
·22 lines (19 loc) · 956 Bytes
/
p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env bash
# Select a song from the current MPD playlist with fzf and start playing it. If only one
# matches "$*", bypass fzf. Based on <https://github.com/junegunn/fzf/wiki/Examples#mpd>
# but this version shows the next song to be played first. TODO: accept as soon as there
# is only one match?
set -o nounset -o pipefail
playlist=$(mpc -f '%position% %artist% - %title%' playlist) &&
# If the current MPD playlist is empty, exit.
[[ $playlist ]] &&
length=$(wc -l <<< "$playlist") &&
current_song=$(mpc -f "%position%" current) &&
current_song=${current_song:-0} &&
new_song=$(awk -v l="$length" '{$1 = ($1 - 1 + l - '"$current_song"') % l; print}' \
<<< "$playlist" \
| sort -k1 -V \
| awk -v l="$length" '{$1 = ($1 + '"$current_song"') % l + 1":"; print}' \
| fzf --query="$*" --select-1 --exit-0 \
| sed -n 's/^\([0-9]\+\):.*/\1/p') &&
[[ -n $new_song ]] && mpc -q play "$new_song"