music.sh (1909B)
1 #!/bin/sh 2 # 3 # music 4 5 MUSIC_ICON='' 6 PLAYING_ICON='' 7 PAUSED_ICON='' 8 9 open() { "${TERMINAL}" -c "${MUSIC_PLAYER}" -e "${MUSIC_PLAYER}" ; } 10 11 get_song_and_state() { 12 # get current song name 13 song="$(mpc -f %title% | head -n 1)" 14 15 # get state of song 16 state="$(mpc -f %title% | sed '2q;d' | awk '{ print $1 }' | tr -d '[:punct:]')" 17 } 18 19 toggle() { 20 # pause if playing and play if paused 21 mpc -q toggle 22 23 get_song_and_state 24 25 # no songs playing so don't do anything 26 if printf '%s\n' "${song}" | awk '{ print $1,$2 }' | grep -iq '^volume:\s[0-9][0-9]*%$'; then return 1; fi 27 28 # convert first letter of state from lowercase to uppercase 29 first_letter="$(printf '%s\n' "${state}" | cut -c 1 | tr '[:lower:]' '[:upper:]')" 30 rest="$(printf '%s\n' "${state}" | cut -c 2-)" 31 state="${first_letter}${rest}" 32 33 herbe "${state} ${song}" & 34 } 35 36 37 bar() { 38 get_song_and_state 39 40 # print ellipses if song name cuts off in bar 41 if [ "${#song}" -ge 20 ]; then 42 song="$(printf '%.17s' "${song}")" 43 song="${song}..." 44 fi 45 46 # no songs 47 [ -z "${state}" ] && printf '%s\n' "${MUSIC_ICON}" 48 # song is playing 49 [ "${state}" = 'playing' ] && printf '%s\n' "${PLAYING_ICON} ${song}" 50 # paused 51 [ "${state}" = 'paused' ] && printf '%s\n' "${PAUSED_ICON} ${song}" 52 } 53 54 main() { 55 # called from bar 56 [ ${#} -eq 0 ] && bar 57 58 # bar usage 59 case ${BLOCK_BUTTON} in 60 1) open ;; 61 2) toggle ;; 62 4) mpc -q prev ;; 63 5) [ -n "$( mpc -f %title% queue)" ] && mpc -q next ;; 64 esac 65 66 while getopts 'ot' opt; do 67 case "${opt}" in 68 # open music player if called with o flag 69 o) open ;; 70 t) toggle ;; 71 *) return ;; 72 esac 73 done 74 } 75 76 main "${@}"