Debian Codegraph @

obvious/temp_info/init.lua

raw symbols SCIP
1 -----------------------------------
2 -- Author: Eligio Becerra --
3 -- Copyright 2009 Eligio Becerra --
4 -----------------------------------
5
6 local setmetatable = setmetatable
7 local tonumber = tonumber
8 local sformat = string.format
9 local smatch = string.match
10 local sgmatch = string.gmatch
11 local tconcat = table.concat
12 local spawn = require 'awful.spawn'
13 local wibox = require 'wibox'
14 local markup = require 'obvious.lib.markup'
15 local hooks = require 'obvious.lib.hooks'
16
17 local temperature = {}
18
19 local widget = wibox.widget.textbox()
20
21 local colors = {
22 normal = '#009000',
23 warm = '#909000',
24 hot = '#900000',
25 }
26
27 local function acpi_backend(callback)
28 local callbacks = {}
29
30 local lines = {}
31
32 function callbacks.stdout(line)
33 lines[#lines + 1] = line
34 end
35
36 function callbacks.output_done()
37 local d = tconcat(lines)
38 local temp = {}
39 for t in sgmatch(d, 'Thermal %d+: %w+, (%d+.?%d*) degrees') do
40 temp[#temp + 1] = tonumber(t)
41 end
42
43 if #temp == 0 then
44 return callback()
45 end
46
47 return callback(temp)
48 end
49
50 local err = spawn.with_line_callback({'acpi', '-t'}, callbacks)
51
52 if type(err) == 'string' then
53 return callback()
54 end
55 end
56
57 local function sensors_backend(callback)
58 local callbacks = {}
59
60 local in_temp_block = false
61 local ignore_further_lines = false
62 local stats = {}
63
64 function callbacks.stdout(line)
65 if ignore_further_lines then
66 return
67 end
68
69 -- we assume that the first temp1 block is the CPU, and that
70 -- the CPU stats are under temp1
71 if line == 'temp1:' then
72 in_temp_block = true
73 elseif in_temp_block then
74 if line == '' then
75 ignore_further_lines = true
76 end
77
78 local name, value = smatch(line, '^%s*temp1_(%w+):%s+(.+)')
79 if name and value then
80 stats[name] = tonumber(value)
81 end
82 end
83 end
84
85 function callbacks.output_done()
86 if stats.input then
87 return callback { stats.input }
88 else
89 return callback()
90 end
91 end
92
93 local err = spawn.with_line_callback({'sensors', '-u'}, callbacks)
94
95 if type(err) == 'string' then
96 return callback()
97 end
98 end
99
100 local function noop_backend(callback)
101 callback {}
102 end
103
104 local backends = {
105 acpi_backend,
106 sensors_backend,
107 noop_backend,
108 }
109 local current_backend
110
111 local function find_backend(next_backend_idx, callback)
112 if current_backend then
113 return callback(current_backend)
114 end
115
116 local backend = backends[next_backend_idx]
117
118 backend(function(stats)
119 if stats then
120 current_backend = backend
121 return callback(backend)
122 end
123
124 if next_backend_idx < #backends then
125 find_backend(next_backend_idx + 1, callback)
126 end
127 end)
128 end
129
130 local function update()
131 current_backend(function(temp)
132 local color = colors.hot
133
134 if not temp[1] then
135 widget:set_text 'no data'
136 return
137 end
138
139 if temp[1] < 50 then
140 color = colors.normal
141 elseif temp[1] >= 50 and temp[1] < 60 then
142 color = colors.warm
143 end
144
145 widget:set_markup(sformat('%.2f', temp[1]) .. ' ' .. markup.fg.color(color, 'C'))
146 end)
147 end
148
149 hooks.timer.register(5, 30, update)
150 hooks.timer.stop(update)
151
152 return setmetatable(temperature, { __call = function ()
153 find_backend(1, function()
154 hooks.timer.start(update)
155 update()
156 end)
157
158 return widget
159 end })
160
161 -- vim: filetype=lua:expandtab:shiftwidth=3:tabstop=3:softtabstop=3:textwidth=80