All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
4 Licensed under GNU General Public License v2
5 * (c) 2013, Luke Bonham
6 * (c) 2010-2012, Peter Hofmann
10 local markup = require("lain.util.markup")
11 local first_line = require("lain.helpers").first_line
13 local beautiful = require("beautiful")
14 local wibox = require("wibox")
16 local math = { ceil = math.ceil }
17 local string = { format = string.format,
18 gmatch = string.gmatch }
20 local setmetatable = setmetatable
30 local args = args or {}
31 local refresh_timeout = args.refresh_timeout or 5
32 local header = args.header or " Cpu "
33 local header_color = args.header or beautiful.fg_normal or "#FFFFFF"
34 local color = args.color or beautiful.fg_focus or "#FFFFFF"
35 local footer = args.footer or "%"
37 local w = wibox.widget.textbox()
39 local cpuusageupdate = function()
40 -- Read the amount of time the CPUs have spent performing
41 -- different kinds of work. Read the first line of /proc/stat
42 -- which is the sum of all CPUs.
43 local times = first_line("/proc/stat")
47 for field in string.gmatch(times, "[%s]+([^%s]+)")
49 -- 3 = idle, 4 = ioWait. Essentially, the CPUs have done
50 -- nothing during these times.
58 local active = total - idle
60 -- Read current data and calculate relative values.
61 local dactive = active - cpu.last_active
62 local dtotal = total - cpu.last_total
63 local dta = math.ceil((dactive / dtotal) * 100)
65 w:set_markup(markup(header_color, header) .. markup(color, dta .. footer) .. " ")
67 -- Save current data for the next run.
68 cpu.last_active = active
69 cpu.last_total = total
72 local cpuusagetimer = timer({ timeout = refresh_timeout })
73 cpuusagetimer:connect_signal("timeout", cpuusageupdate)
75 cpuusagetimer:emit_signal("timeout")
80 return setmetatable(cpu, { __call = function(_, ...) return worker(...) end })