Released version 3-2015061300
[notion.git] / contrib / statusbar / statusbar_wsname.lua
blob8220cbda8fb0708424e753ff52486e3441ebaaab
1 -- Authors: Etan Reisner <deryni@gmail.com>
2 -- License: MIT, see http://opensource.org/licenses/mit-license.php
3 -- Last Changed: 2007-01-23
4 --
5 --[[
6 Author: Etan Reisner
7 Email: deryni@gmail.com
8 Summary: Adds a handful of statusbar meters displaying the current workspace name, all workspace names, etc. in a variety of manners.
9 Version: 0.6
10 Last Updated: 2007-01-23
12 Copyright (c) Etan Reisner 2007
14 This software is released under the terms of the MIT license. For more
15 information, see http://opensource.org/licenses/mit-license.php .
16 --]]
18 -- Usage:
19 -- Add %wsname (or %wsname_full) to your statusbar template.
21 -- For multiple head setups with multiple statusbars,
22 -- use %wsname_{screennum} in your template (%wsname is the same as %wsname_0).
24 -- The %wsname_fullall meter shows a combined %wsname_full display for all screens.
26 -- The %wsname_pre meter contains the workspaces earlier in the list than the
27 -- current workspace.
29 -- The %wsname_post meter contains the workspaces later in the list than the
30 -- current workspace.
32 if not mod_statusbar then
33 return
34 end
36 local defaults = {
37 marker = "*",
38 template = "xxxxxxxxxxx",
39 all_marker = " - ",
42 local wsname = table.join(wsname or {}, defaults)
44 local function get_ws_name(t)
45 local reg = t["reg"]
47 if not obj_is(reg, "WScreen") then
48 return
49 end
51 local ws_names_all, wsname_pre, wsname_post = nil, "", ""
53 local function inform(screen)
54 if screen:mx_count() == 0 then
55 return
56 end
58 local ws_names, before, id = nil, true, screen:id()
60 local function compose_ws_names(ws)
61 local marker, current = "", false
62 local wsn = ws:name() or "?"
64 if ws == screen:mx_current() then
65 marker = wsname.marker
66 before = false
67 current = true
68 end
70 if not ws_names then
71 ws_names = marker..wsn..marker
72 else
73 ws_names = ws_names.." "..marker..wsn..marker
74 end
76 if before and not current then
77 if wsname_pre == "" then
78 wsname_pre = wsn
79 else
80 wsname_pre = wsname_pre.." "..wsn
81 end
82 elseif not current then
83 if wsname_post == "" then
84 wsname_post = wsn
85 else
86 wsname_post = wsname_post.." "..wsn
87 end
88 end
90 return true
91 end
93 screen:mx_i(compose_ws_names)
95 mod_statusbar.inform("wsname_"..id, screen:mx_current():name())
96 mod_statusbar.inform("wsname_full_"..id, ws_names)
97 mod_statusbar.inform("wsname_full_"..id.."_template", ws_names:len())
98 mod_statusbar.inform("wsname_pre_"..id, wsname_pre)
99 mod_statusbar.inform("wsname_pre_"..id.."_template", wsname_pre:len())
100 mod_statusbar.inform("wsname_post_"..id, wsname_post)
101 mod_statusbar.inform("wsname_post_"..id.."_template", wsname_post:len())
103 if id == 0 then
104 mod_statusbar.inform("wsname", screen:mx_current():name())
105 mod_statusbar.inform("wsname_full", ws_names)
106 mod_statusbar.inform("wsname_full_template", ws_names:len())
107 mod_statusbar.inform("wsname_pre", wsname_pre)
108 mod_statusbar.inform("wsname_pre_template", wsname_pre:len())
109 mod_statusbar.inform("wsname_post", wsname_post)
110 mod_statusbar.inform("wsname_post_template", wsname_post:len())
113 if not ws_names_all then
114 ws_names_all = ws_names
115 else
116 ws_names_all = ws_names_all..wsname.all_marker..ws_names
119 return true
122 ioncore.region_i(inform, "WScreen")
124 mod_statusbar.inform("wsname_fullall", ws_names_all)
125 mod_statusbar.inform("wsname_fullall_template", string.len(ws_names_all))
127 ioncore.defer(function() mod_statusbar.update() end)
130 local function setup_hooks()
131 local hook
133 hook = ioncore.get_hook("screen_managed_changed_hook")
134 if hook then
135 hook:add(get_ws_name)
139 -- Init
140 setup_hooks()
142 local function inform(screen)
143 local template
144 local id = screen:id()
146 if wsname[id] and wsname[id].template then
147 template = wsname[id].template
148 else
149 template = wsname.template
152 mod_statusbar.inform("wsname_"..id.."_template", template)
154 return true
157 mod_statusbar.inform("wsname_template", wsname.template)
158 ioncore.region_i(inform, "WScreen")
160 mod_statusbar.update(true)