Debian Codegraph @

obvious/battery/init.lua

raw symbols SCIP
1 --------------------------------------------
2 -- Author: Gregor Best --
3 -- Copyright 2009, 2010, 2011 Gregor Best --
4 --------------------------------------------
5
6 local tonumber = tonumber
7 local tostring = tostring
8 local setmetatable = setmetatable
9 local type = type
10 local io = {
11 popen = io.popen
12 }
13 local capi = {
14 mouse = mouse
15 }
16 local table = {
17 remove = table.remove
18 }
19 local math = {
20 floor = math.floor
21 }
22
23 local naughty = require("naughty")
24 local awful = require("awful")
25 local wibox = require("wibox")
26 local lib = {
27 hooks = require("obvious.lib.hooks"),
28 markup = require("obvious.lib.markup")
29 }
30 local sformat = string.format
31 local backends = require 'obvious.battery.backends'
32
33 local battery = {}
34
35 local widget = wibox.widget.textbox()
36 local status_text = {
37 ["charged"] = "↯",
38 ["full"] = "↯",
39 ["high"] = "↯",
40 ["discharging"] = "▼",
41 ["not connected"] = "▼",
42 ["charging"] = "▲",
43 ["unknown"] = "⌁"
44 }
45
46 local backend
47
48 local inverted = false
49 local cycle_count = 0
50 local previous_state
51
52 local function update(force)
53 if not backend then
54 return
55 end
56
57 if force then
58 cycle_count = 0
59 end
60 if cycle_count == 0 then
61 previous_state = { backend:state() }
62 end
63 cycle_count = (cycle_count + 1) % 60
64 local bats = previous_state
65
66 if #bats == 0 then
67 widget:set_markup("no data")
68 return
69 end
70
71 local markup = ''
72
73 for i = 1, #bats do
74 local bat = bats[i]
75 local color
76
77 local blinking = false
78
79 local charge = bat.charge
80
81 if charge == nil then
82 color = '#900000'
83 charge = 'Unknown charge'
84 elseif charge >= 60 then
85 color = '#009000'
86 elseif charge > 35 then
87 color = '#909000'
88 else
89 color = '#900000'
90 blinking = bat.charge <= 20 and (bat.status == 'discharging' or bat.status == 'not connected')
91 end
92
93 local status = status_text[bat.status] or 'unknown'
94
95 local battery_status
96
97 if blinking and inverted then
98 battery_status = status .. ' ' .. awful.util.escape(tostring(bat.charge)) .. '%'
99 else
100 battery_status = lib.markup.fg.color(color, status) .. ' ' .. awful.util.escape(tostring(bat.charge)) .. '%'
101 end
102
103 if bat.time then
104 local hours = math.floor(bat.time / 60)
105 local minutes = bat.time % 60
106
107 battery_status = battery_status .. ' ' .. awful.util.escape(sformat('%02d:%02d', hours, minutes))
108 end
109
110 if blinking and inverted then
111 battery_status = lib.markup.bg.color(color, battery_status)
112 end
113 inverted = not inverted
114
115 if i == 1 then
116 markup = markup .. battery_status
117 else
118 markup = markup .. ' ' .. battery_status
119 end
120 end
121
122 widget:set_markup(markup)
123 end
124
125 local function detail()
126 local details = backend:details()
127
128 if not details then
129 details = 'no details available'
130 end
131 naughty.notify({
132 text = details,
133 screen = capi.mouse.screen,
134 })
135 update(true)
136 end
137
138 function battery.get_data()
139 if not backend then
140 backend = backends.get(battery.preferred_backend)
141 end
142
143 local bats = { backend:state() }
144 if bats then
145 return bats[1]
146 end
147 end
148
149 widget:buttons(awful.util.table.join(
150 awful.button({ }, 1, detail)
151 ))
152 -- XXX FIXME
153 lib.hooks.timer.register(1, 300, update)
154
155 return setmetatable(battery, { __call = function ()
156 if not backend then
157 backend = backends.get(battery.preferred_backend)
158 end
159 update()
160 lib.hooks.timer.start(update)
161 return widget
162 end })
163
164 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et