2 -- ion/mod_statusbar/mod_statusbar.lua
4 -- Copyright (c) Tuomo Valkonen 2004-2005.
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
21 --local mod_menu=_G["mod_menu"]
23 local mod_statusbar
={}
24 _G
["mod_statusbar"]=mod_statusbar
30 string.format("[ %%date || %s: %%load || %s: %%mail_new/%%mail_total ]",
31 TR("load"), TR("mail"))
35 date_format
='%a %Y-%m-%d %H:%M',
36 template
=default_tmpl
,
37 load_wtempl
="x.xx, x.xx, x.xx",
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
55 -- Set format and update variables.
56 function mod_statusbar
.get()
57 return table.copy(settings
)
69 function mod_statusbar
.inform(name
, value
)
80 local function get_date()
81 return os
.date(settings
.date_format
)
84 function mod_statusbar
.set_timer()
86 local d
=(60-t
.sec
)*1000
88 timer
:set(d
, mod_statusbar
.timer_handler
)
91 function mod_statusbar
.timer_handler(tmr
)
92 mod_statusbar
.inform("date", get_date())
93 mod_statusbar
.update()
94 mod_statusbar
.set_timer()
97 function mod_statusbar
.init_timer()
99 timer
=ioncore
.create_timer()
101 error(TR("Failed to create a timer for statusbar."))
105 if not timer
:is_set() then
106 mod_statusbar
.timer_handler(timer
)
116 local function process_template(fn
, gn
)
117 local l
=string.gsub(settings
.template
, '(.-)%%(%%?[a-zA-Z0-9_]*)',
119 if string.len(t
)>0 then
122 if string.sub(s
, 1, 1)=='%' then
133 function mod_statusbar
.get_status()
135 process_template(function(s
)
136 table.insert(res
, {text
=meters
[s
] or "??"})
139 table.insert(res
, {text
=t
})
144 function mod_statusbar
.get_w_template()
146 process_template(function(s
)
148 local w
=settings
[s
.."_wtempl"]
149 res
=res
..(w
or m
or "??")
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()
165 for sb
, _
in statusbars
do
166 if not obj_exists(sb
) then
180 -- ion-statusd support {{{
182 local statusd_running
=false
184 function mod_statusbar
.rcv_statusd(str
)
187 local function doline(i
)
189 mod_statusbar
.update()
191 local _
, _
, m
, v
=string.find(i
, "^([^:]+):%s*(.*)")
193 mod_statusbar
.inform(m
, v
)
199 data
=string.gsub(data
..str
, "([^\n]*)\n", doline
)
200 str
=coroutine
.yield()
203 ioncore
.warn(TR("ion-statusd quit."))
204 statusd_running
=false
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
216 process_template(function(s
)
217 local _
, _
, m
= string.find(s
, "^([^_]+)")
225 table.foreach(mods
, function(k
) params
=params
.." -M "..k
end)
230 if statusd_running
then
234 local statusd
=ioncore
.lookup_script("ion-statusd")
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
)
248 -- Initialisation {{{
252 -- Create a statusbar.
253 function mod_statusbar
.create(param
)
254 local scr
=ioncore
.find_screen_id(param
.screen
or 0)
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 "..
267 local sb
=scr
:set_stdisp({
269 pos
=(param
.pos
or "bl"),
274 error(TR("Failed to create statusbar."))
277 mod_statusbar
.init_timer()
279 mod_statusbar
.launch_statusd()
282 sb
:set_natural_w(mod_statusbar
.get_w_template())
283 sb
:set_contents(mod_statusbar
.get_status())
291 -- Mark ourselves loaded.
292 _LOADED
["mod_statusbar"]=true
295 -- Load user configuration file
296 dopath('cfg_statusbar', true)