summaryrefslogtreecommitdiff
path: root/herbstluftwm/panel.bar.sh
blob: 88f7ab19299d4f9d72ef22a9a7f278fd6f8ad933 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash

# todo: fontello icons

# disable path name expansion or * will be expanded in the line
# cmd=( $line )
set -f

function uniq_linebuffered() {
    awk -W interactive '$0 != l { print ; l=$0 ; fflush(); }' "$@"
}
function get_mpd_song() {
    # use mpc to get currently playing song, uppercase it
    song=$(mpc current -f %title%)
    # let's skip ft. parts, etc. to get some more space
    echo $song
}

monitor=${1:-0}

separator="\f3 | \fr"
song=$(get_mpd_song)
windowtitle=""

herbstclient pad $monitor 18
{
    # events:
    # mpc events
    mpc idleloop player &
    mpc_pid=$!

    # date
    while true ; do
        date +'date_day %A %e.  '
        date +'date_min %H:%M  '
        sleep 1 || break
    done > >(uniq_linebuffered) &
    date_pid=$!

    while true
    do
        $HOME/scripts/battery-parse
        sleep 2m || break
    done &
    battery_pid=$!

    while true
    do
        $HOME/scripts/nm-connected-device
        sleep 3m || break
    done &
    nm_status_pid=$!

    # hlwm events
    herbstclient --idle

    # exiting; kill stray event-emitting processes
    kill $date_pid $mpd_pid $battery_pid $nm_status_pid
} 2> /dev/null | {
    TAGS=( $(herbstclient tag_status $monitor) )
    #unset TAGS[${#TAGS[@]}-1]
    date_day=""
    date_min=""
    Battery=""
    network_status=""
    visible=true

    while true ; do
        # align left
        echo -n "\l"
        if [[ $windowtitle ]]
        then
            if [ ${#windowtitle} -gt 55 ]
            then
                windowtitle="$(echo -n $windowtitle | cut -c1-55)..."
            fi
            echo -n "\ur\fr$windowtitle$separator"
        fi
        
        # Align center
        echo -n "\c"
        for i in "${TAGS[@]}" ; do
            case ${i:0:1} in
                '#') # current tag
                    echo -n "\u4\b4\fr"
                    ;;
                '+') # active on other monitor
                    echo -n "\u7\b7\f8"
                    ;;
                ':')
                    # Tag with windows
                    echo -n "\ur\br\f4"
                    ;;
                '!') # urgent tag
                    echo -n "\u1\br\f1"
                    ;;
                *)
                    echo -n "\ur\br\fr"
                    ;;
            esac
            echo -n " ${i:1} " # | tr '[:lower:]' '[:upper:]'
        done

        # align right
        echo -n "\r\ur\fr\br"
        # display song and separator only if something's playing
        if [[ $song ]]; then
            if [ ${#song} -gt 30 ]
            then
                song="$(echo -n $song | cut -c1-30)..."
            fi
            echo -n "\ur\fr$song$separator"
        fi
        echo -n "$network_status"
        echo -n "$separator"
        echo -n "$Battery"
        echo -n "$separator"
        echo -n "$date_day" | tr '[:lower:]' '[:upper:]'
        echo -n " \f2"
        echo -n "$date_min  " | tr '[:lower:]' '[:upper:]'
        echo
        # wait for next event
        read line || break
        cmd=( $line )
        # find out event origin
        case "${cmd[0]}" in
            tag*)
                TAGS=( $(herbstclient tag_status $monitor) )
                #unset TAGS[${#TAGS[@]}-1]
                ;;
            date_day)
                date_day="${cmd[@]:1}"
                ;;
            date_min)
                date_min="${cmd[@]:1}"
                ;;
            player)
                song=$(get_mpd_song)
                ;;
            focus_changed|window_title_changed)
                windowtitle="${cmd[@]:2}"
                ;;
            Battery)
                Battery="${cmd[1]} ${cmd[2]}"
                ;;
            network)
                network_status="${cmd[1]} ${cmd[2]}"
                ;;
            quit_panel)
                exit
                ;;
            reload)
                exit
                ;;
        esac
    done
} 2> /dev/null | bar $1