aboutsummaryrefslogtreecommitdiffstats
path: root/herbstluftwm/.local/bin/statusbar/polybar_hlwm_workspaces.sh
blob: 52ca9e4ab2ce2a4d53ee2720137bf76a25a7df05 (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
#!/usr/bin/env bash

hc() {
    "${herbstclient_command[@]:-herbstclient}" "$@"
}

monitor_bound() {
   echo "$1" | cut -d' ' -f2
}

hlwm_id() {
    while read -r bound; do
        hc list_monitors | grep "$bound" | sed -e 's/:.*$//g'
    done <<< "$(monitor_bound "$1")"
}

echo "${MONITOR:?"Polybar should set the MONITOR variable"}"

this_monitor=$(polybar -m | grep "$MONITOR")


monitor=$(hlwm_id "$this_monitor")

get_layout() {
    current_layout=$(hc get_attr tags.focus.tiling.focused_frame.algorithm)
    case "$current_layout" in
        vertical)
            echo "V" ;;
        horizontal)
            echo "H" ;;
        max)
            echo "M" ;;
        grid)
            echo "G" ;;
    esac
}

layout=$(get_layout)

herbstclient --idle "(tag_*|(focus|window_title|attribute)_changed)" 2>/dev/null | {
    
    while true; do
        # Read tags into $tags array
        IFS=$'\t' read -ra tags <<< "$(hc tag_status "$monitor")"
        {
            # Read the prefix from each tag
            for i in "${tags[@]}" ; do
                case ${i:1} in
                    'fl')
                        ;&
                    'hidden')
                        ;&
                    'scratchpad')
                        continue
                        ;;
                esac
                case ${i:0:1} in
                    '#')
                        echo -n "%{B#9fbc00}%{F#101010}"
                        # Tag in focus on this monitor
                        ;;
                    '%')
                        echo -n "%{B#9ca668}%{F#141414}"
                        # Tag in focus on other monitor
                        ;;
                    '+')
                        echo -n "%{B#9ca668}%{F#141414}"
                        # tag viewed on this monitor
                        ;;
                    '-')
                        echo -n "%{B#9ca668}%{F#141414}"
                        # Tag viewed on other monitor
                        ;;
                    ':')
                        echo -n "%{B#222}%{F#ffffff}"
                        # Tag is not empty
                        ;;
                    '!')
                        echo -n "%{B#ff0675}%{F#141414}"
                        # Tag contains urgent window
                        ;;
                    *)
                        echo -n "%{B#222}%{F#7a7a7a}"
                        ;;
                esac

                echo "%{A1:herbstclient and . focus_monitor $monitor . use ${i:1}:} ${i:1} %{A -u -o F- B-}"
            done

            echo "%{F-}%{B-}"

            echo "| [$layout]"

        } | tr -d "\n"
        echo
        IFS=$'\t' read -ra cmd || break
        case "${cmd[0]}" in
            #focus_changed|window_title_changed)
            #    layout=$(get_layout) ;;
            attribute_changed)
                if [[ "${cmd[1]}" == "tags.focus.tiling.focused_frame.algorithm" ]]; then
                    case "${cmd[3]}" in
                        vertical)
                            layout="V" ;;
                        horizontal)
                            layout="H" ;;
                        max)
                            layout="M" ;;
                        grid)
                            layout="G" ;;
                    esac
                    #layout="${cmd[3]}"
                fi
        esac
        # wait for next event from herbstclient --idle
        # read -r || break
    done
} 2>/dev/null