Debian Codegraph @

obvious/lib/widget/textbox.lua

raw symbols SCIP
1 -----------------------------------
2 -- Author: Uli Schlachter --
3 -- Copyright 2009 Uli Schlachter --
4 -----------------------------------
5
6 local type = type
7 local pcall = pcall
8 local wibox = require("wibox")
9 local string = {
10 format = string.format
11 }
12
13 local function create(data, layout)
14 local obj = { }
15
16 obj.data = data
17 obj.widget = wibox.widget.textbox()
18 obj.format = "%3d%%"
19 obj.layout = layout
20
21 obj.update = function(obj)
22 local max = obj.data.max or 1
23 local val = obj.data:get() or max
24 local perc = val / max * 100
25 if type(obj.format) == "function" then
26 if not pcall(function () obj.widget:set_markup(obj.format(perc)) end) then
27 obj.widget:set_text(obj.format(perc))
28 end
29 else
30 if not pcall(function () obj.widget:set_markup(string.format(obj.format, perc)) end) then
31 obj.widget:set_text(string.format(obj.format, perc))
32 end
33 end
34 end
35
36 obj.set_format = function(obj, format)
37 obj.format = format
38 obj:update()
39 return obj
40 end
41
42 return obj
43 end
44
45 return {
46 create = create,
47 }
48
49 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et