File: statusbar_wsname.lua

package info (click to toggle)
notion 4.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,656 kB
  • sloc: ansic: 47,365; sh: 2,093; makefile: 594; perl: 270
file content (160 lines) | stat: -rw-r--r-- 4,614 bytes parent folder | download | duplicates (6)
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
158
159
160
-- Authors: Etan Reisner <deryni@gmail.com>
-- License: MIT, see http://opensource.org/licenses/mit-license.php
-- Last Changed: 2007-01-23
--
--[[
Author: Etan Reisner
Email: deryni@gmail.com
Summary: Adds a handful of statusbar meters displaying the current workspace name, all workspace names, etc. in a variety of manners.
Version: 0.6
Last Updated: 2007-01-23

Copyright (c) Etan Reisner 2007

This software is released under the terms of the MIT license. For more
information, see http://opensource.org/licenses/mit-license.php .
--]]

-- Usage:
-- Add %wsname (or %wsname_full) to your statusbar template.
--
-- For multiple head setups with multiple statusbars,
-- use %wsname_{screennum} in your template (%wsname is the same as %wsname_0).
--
-- The %wsname_fullall meter shows a combined %wsname_full display for all screens.
--
-- The %wsname_pre meter contains the workspaces earlier in the list than the
-- current workspace.
--
-- The %wsname_post meter contains the workspaces later in the list than the
-- current workspace.

if not mod_statusbar then
    return
end

local defaults = {
    marker = "*",
    template = "xxxxxxxxxxx",
    all_marker = " - ",
}

local wsname = table.join(wsname or {}, defaults)

local function get_ws_name(t)
    local reg = t["reg"]

    if not obj_is(reg, "WScreen") then
        return
    end

    local ws_names_all, wsname_pre, wsname_post = nil, "", ""

    local function inform(screen)
        if screen:mx_count() == 0 then
            return
        end

        local ws_names, before, id = nil, true, screen:id()

        local function compose_ws_names(ws)
            local marker, current = "", false
            local wsn = ws:name() or "?"

            if ws == screen:mx_current() then
                marker = wsname.marker
                before = false
                current = true
            end

            if not ws_names then
                ws_names = marker..wsn..marker
            else
                ws_names = ws_names.." "..marker..wsn..marker
            end

            if before and not current then
                if wsname_pre == "" then
                    wsname_pre = wsn
                else
                    wsname_pre = wsname_pre.." "..wsn
                end
            elseif not current then
                if wsname_post == "" then
                    wsname_post = wsn
                else
                    wsname_post = wsname_post.." "..wsn
                end
            end

            return true
        end

        screen:mx_i(compose_ws_names)

        mod_statusbar.inform("wsname_"..id, screen:mx_current():name())
        mod_statusbar.inform("wsname_full_"..id, ws_names)
        mod_statusbar.inform("wsname_full_"..id.."_template", ws_names:len())
        mod_statusbar.inform("wsname_pre_"..id, wsname_pre)
        mod_statusbar.inform("wsname_pre_"..id.."_template", wsname_pre:len())
        mod_statusbar.inform("wsname_post_"..id, wsname_post)
        mod_statusbar.inform("wsname_post_"..id.."_template", wsname_post:len())

        if id == 0 then
            mod_statusbar.inform("wsname", screen:mx_current():name())
            mod_statusbar.inform("wsname_full", ws_names)
            mod_statusbar.inform("wsname_full_template", ws_names:len())
            mod_statusbar.inform("wsname_pre", wsname_pre)
            mod_statusbar.inform("wsname_pre_template", wsname_pre:len())
            mod_statusbar.inform("wsname_post", wsname_post)
            mod_statusbar.inform("wsname_post_template", wsname_post:len())
        end

        if not ws_names_all then
            ws_names_all = ws_names
        else
            ws_names_all = ws_names_all..wsname.all_marker..ws_names
        end

        return true
    end

    ioncore.region_i(inform, "WScreen")

    mod_statusbar.inform("wsname_fullall", ws_names_all)
    mod_statusbar.inform("wsname_fullall_template", string.len(ws_names_all))

    ioncore.defer(function() mod_statusbar.update() end)
end

local function setup_hooks()
    local hook

    hook = ioncore.get_hook("screen_managed_changed_hook")
    if hook then
        hook:add(get_ws_name)
    end
end

-- Init
setup_hooks()

local function inform(screen)
    local template
    local id = screen:id()

    if wsname[id] and wsname[id].template then
        template = wsname[id].template
    else
        template = wsname.template
    end

    mod_statusbar.inform("wsname_"..id.."_template", template)

    return true
end

mod_statusbar.inform("wsname_template", wsname.template)
ioncore.region_i(inform, "WScreen")

mod_statusbar.update(true)