trunk: changeset 1940
[notion/jeffpc.git] / mod_statusbar / mod_statusbar.lua
blobb81792cbabfc5c2917ec2f95474336943ce67503
1 --
2 -- ion/mod_statusbar/mod_statusbar.lua
3 --
4 -- Copyright (c) Tuomo Valkonen 2004-2005.
5 --
6 -- Ion is free software; you can redistribute it and/or modify it under
7 -- the terms of the GNU Lesser General Public License as published by
8 -- the Free Software Foundation; either version 2.1 of the License, or
9 -- (at your option) any later version.
12 -- This is a slight abuse of the _LOADED variable perhaps, but library-like
13 -- packages should handle checking if they're loaded instead of confusing
14 -- the user with require/include differences.
15 if _LOADED["mod_statusbar"] then return end
17 if not ioncore.load_module("mod_statusbar") then
18 return
19 end
21 --local mod_menu=_G["mod_menu"]
22 --assert(mod_menu)
23 local mod_statusbar={}
24 _G["mod_statusbar"]=mod_statusbar
27 -- Settings etc. {{{
29 local default_tmpl=
30 string.format("[ %%date || %s: %%load || %s: %%mail_new/%%mail_total ]",
31 TR("load"), TR("mail"))
33 -- Default settings
34 local settings={
35 date_format='%a %Y-%m-%d %H:%M',
36 template=default_tmpl,
37 load_wtempl="x.xx, x.xx, x.xx",
40 local statusbars={}
43 --DOC
44 -- Set format and update variables.
45 function mod_statusbar.set(s)
46 settings=table.join(s, settings)
47 local wt=mod_statusbar.get_w_template()
48 for sb, _ in statusbars do
49 sb:set_natural_w(wt)
50 end
51 end
54 --DOC
55 -- Set format and update variables.
56 function mod_statusbar.get()
57 return table.copy(settings)
58 end
60 -- }}}
63 -- Meter list {{{
65 local meters={}
67 --DOC
68 -- Inform of a value.
69 function mod_statusbar.inform(name, value)
70 meters[name]=value
71 end
73 -- }}}
76 -- Date meter {{{
78 local timer
80 local function get_date()
81 return os.date(settings.date_format)
82 end
84 function mod_statusbar.set_timer()
85 local t=os.date('*t')
86 local d=(60-t.sec)*1000
88 timer:set(d, mod_statusbar.timer_handler)
89 end
91 function mod_statusbar.timer_handler(tmr)
92 mod_statusbar.inform("date", get_date())
93 mod_statusbar.update()
94 mod_statusbar.set_timer()
95 end
97 function mod_statusbar.init_timer()
98 if not timer then
99 timer=ioncore.create_timer()
100 if not timer then
101 error(TR("Failed to create a timer for statusbar."))
105 if not timer:is_set() then
106 mod_statusbar.timer_handler(timer)
111 -- }}}
114 -- Status update {{{
116 local function process_template(fn, gn)
117 local l=string.gsub(settings.template, '(.-)%%(%%?[a-zA-Z0-9_]*)',
118 function(t, s)
119 if string.len(t)>0 then
120 gn(t)
122 if string.sub(s, 1, 1)=='%' then
123 gn('%')
124 elseif s~="" then
125 fn(s)
127 end)
128 if l then
129 gn(l)
133 function mod_statusbar.get_status()
134 local res={}
135 process_template(function(s)
136 table.insert(res, {text=meters[s] or "??"})
137 end,
138 function(t)
139 table.insert(res, {text=t})
140 end)
141 return res
144 function mod_statusbar.get_w_template()
145 local res=""
146 process_template(function(s)
147 local m=meters[s]
148 local w=settings[s.."_wtempl"]
149 res=res..(w or m or "??")
150 end,
151 function(t)
152 res=res..t
153 end)
154 return res
158 --DOC
159 -- Update statusbar contents. To be called after series
160 -- of \fnref{mod_statusbar.inform} calls.
161 function mod_statusbar.update()
162 local st=mod_statusbar.get_status()
163 local found=false
165 for sb, _ in statusbars do
166 if not obj_exists(sb) then
167 statusbars[sb]=nil
168 else
169 sb:set_contents(st)
170 found=true
174 return found
177 -- }}}
180 -- ion-statusd support {{{
182 local statusd_running=false
184 function mod_statusbar.rcv_statusd(str)
185 local data=""
187 local function doline(i)
188 if i=="." then
189 mod_statusbar.update()
190 else
191 local _, _, m, v=string.find(i, "^([^:]+):%s*(.*)")
192 if m and v then
193 mod_statusbar.inform(m, v)
198 while str do
199 data=string.gsub(data..str, "([^\n]*)\n", doline)
200 str=coroutine.yield()
203 ioncore.warn(TR("ion-statusd quit."))
204 statusd_running=false
205 meters={}
206 mod_statusbar.update()
210 function mod_statusbar.launch_statusd()
211 local function get_statusd_params()
212 if settings.statusd_params then
213 return settings.statusd_params
214 else
215 local mods={}
216 process_template(function(s)
217 local _, _, m = string.find(s, "^([^_]+)")
218 if m then
219 mods[m]=true
221 end,
222 function()
223 end)
224 local params=""
225 table.foreach(mods, function(k) params=params.." -M "..k end)
226 return params
230 if statusd_running then
231 return
234 local statusd=ioncore.lookup_script("ion-statusd")
235 if not statusd then
236 ioncore.warn(TR("Could not find %s", script))
239 local cmd=statusd.." "..get_statusd_params()
240 local cr=coroutine.wrap(mod_statusbar.rcv_statusd)
242 statusd_running=ioncore.popen_bgread(cmd, cr)
245 --}}}
248 -- Initialisation {{{
251 --DOC
252 -- Create a statusbar.
253 function mod_statusbar.create(param)
254 local scr=ioncore.find_screen_id(param.screen or 0)
255 if not scr then
256 error(TR("Screen not found."))
259 if not param.force then
260 local stdisp=scr:get_stdisp()
261 if stdisp and stdisp.reg then
262 error(TR("Screen already has an stdisp. Refusing to create a "..
263 "statusbar."))
267 local sb=scr:set_stdisp({
268 type="WStatusBar",
269 pos=(param.pos or "bl"),
270 name="*statusbar*",
273 if not sb then
274 error(TR("Failed to create statusbar."))
277 mod_statusbar.init_timer()
279 mod_statusbar.launch_statusd()
281 statusbars[sb]=true
282 sb:set_natural_w(mod_statusbar.get_w_template())
283 sb:set_contents(mod_statusbar.get_status())
285 return sb
288 -- }}}
291 -- Mark ourselves loaded.
292 _LOADED["mod_statusbar"]=true
295 -- Load user configuration file
296 dopath('cfg_statusbar', true)