Jaenbon's MPD widget

From awesome
Jump to: navigation, search

This script shows the current title in a scrolling textbox and the current time. You'll need mpc, of course

The lua configuration :

mpd_text_box = widget({ type = "textbox", align = "right" })
mpd_text_box.text = ""
mpd_time_box = widget({ type = "textbox", align = "right" })
mpd_time_box.text = ""

mpd_text_max_size = 30
mpd_text = ""

function mpd_text_rotate()
    if string.len(mpd_text) >= mpd_text_max_size then
        mpd_text = string.gsub(mpd_text, '^(.)(.+)$', '%2%1')
        mpd_text_box.text = string.sub(mpd_text, 1, mpd_text_max_size).."..."
    else
        mpd_text_box.text = mpd_text
    end
end

awful.hooks.timer.register(0.4, function()
    mpd_text_rotate()
end)

The bash script, to launch on startup (e.g. in .xinitrc)

#!/bin/bash

aw_song_var="mpd_text"
aw_song_box="mpd_text_box"
aw_time_var="mpd_time_box.text"
aw_time_box="mpd_time_box"
aw_song_width=180
aw_time_width=70

function set_width {
    echo "${aw_song_box}.width = $aw_song_width" | awesome-client
    #echo "${aw_time_box}.width = $aw_time_width" | awesome-client
}
    
function clean {
    echo "${aw_song_box}.width = 0" | awesome-client
    update_aw_text ""
    echo "${aw_time_box}.width = 0" | awesome-client
    update_aw_time ""
}

function get_current {
    current=`mpc --format "[%artist% - %title%]" | head -n 1`
    if [[ -z $current ]]; then
        current=`mpc --format "[%file%]" | head -n 1 | sed -r -e 's=^.*/([^/]*)$=\1=' -e 's/(.+)\..+/\1/'`
    fi
    echo $current | sed -e "s/'/\\\'/g" -e 's/"/\\\"/g' | sed 's/&/&amp\;/g'
}

function get_time {
    mpc | sed 1d | grep '\[.*\]' | sed 's/^.* \([0-9:/]\+\) .*/\1/'
}

function get_status {
    mpc | sed 1d | grep '\[.*\]' | sed -r 's/^\[(.+)\].*$/\1/'
}

function update_aw_text {
    echo "$aw_song_var = '$@'" | awesome-client
}

function update_aw_time {
    echo "$aw_time_var = '$@'" | awesome-client
}

function ctr_c {
    echo "You want me to stop..."
    the_response="youjustfailed"
}

if [[ "$1" == "-c" ]]; then
    #clean
    clean
    exit 0
fi

# Capture the keyboardinterrupt
# Ugly, I know
trap ctr_c INT

the_response=`echo NDIK | base64 -d`

set_width
old_song=""
old_status=""
while [[ "42" -eq "$the_response" ]]; do 
    if [[ -n `pgrep mpd` ]]; then
        status=`get_status`
        current=`get_current`
        ze_time=`get_time`
        if [[ "$status" == "playing" ]] ; then
            if [[ "$old_song" != "$current" || "$status" != "$old_status" ]]; then
                old_song=$current
                old_status=$status
                update_aw_text " [>] $current "
                set_width
            fi
            update_aw_time " $ze_time "
        elif [[ "$status" == "paused" ]] ; then
            if [[ "$old_song" != "$current" || "$status" != "$old_status" ]]; then
                old_song=$current
                old_status=$status
                update_aw_text " [~] $current "
                set_width
            fi
        else
            update_aw_text " [#] Stopped "
        fi
    else
        clean
    fi

    sleep 1
done
clean
Personal tools