1
1
Fork 0
mirror of https://github.com/vicious-widgets/vicious synced 2023-12-14 07:13:07 +01:00

Convert tabs to spaces and better adhere to the code style practices

This commit is contained in:
Enric Morales 2019-10-15 16:22:26 +02:00
parent ce66335081
commit 25897d5982
2 changed files with 58 additions and 54 deletions

View file

@ -245,32 +245,32 @@ function helpers.sysctl_async(path_table, parse)
spawn.with_line_callback("sysctl " .. path, { spawn.with_line_callback("sysctl " .. path, {
stdout = function(line) stdout = function(line)
local separators = { local separators = {
freebsd = ": ", freebsd = ": ",
linux = " = ", linux = " = ",
openbsd = "=" openbsd = "="
} }
local pattern = ("(.+)%s(.+)"):format(separators[helpers.getos()]) local pattern = ("(.+)%s(.+)"):format(separators[helpers.getos()])
local key, value = string.match(line, pattern) local key, value = string.match(line, pattern)
ret[key] = value ret[key] = value
end, end,
stderr = function(line) stderr = function(line)
local messages = { local messages = {
openbsd = {"level name .+ in (.+) is invalid"}, openbsd = { "level name .+ in (.+) is invalid" },
linux = {"cannot stat /proc/sys/(.+):", linux = { "cannot stat /proc/sys/(.+):",
"permission denied on key '(.+)'"}, "permission denied on key '(.+)'" },
freebsd = {"unknown oid '(.+)'"} freebsd = { "unknown oid '(.+)'" }
} }
for i=1,#messages[helpers.getos()] do for _, error_message in ipairs(messages[helpers.getos()]) do
local key = line:match(messages[helpers.getos()][i]) local key = line:match(error_message)
if key then if key then
key = key:gsub("/", ".") key = key:gsub("/", ".")
ret[key] = "N/A" ret[key] = "N/A"
end end
end end
end, end,
output_done = function() parse(ret) end output_done = function () parse(ret) end
}) })
end end
-- }}} -- }}}

View file

@ -19,6 +19,10 @@
-- {{{ Grab environment -- {{{ Grab environment
local tonumber = tonumber local tonumber = tonumber
local table = {
insert = table.insert
}
local math = { local math = {
floor = math.floor, floor = math.floor,
modf = math.modf modf = math.modf
@ -32,58 +36,58 @@ function bat_openbsd.async(format, warg, callback)
local battery_id = warg or "bat0" local battery_id = warg or "bat0"
local fields = { local fields = {
charging_rate = ("hw.sensors.acpi%s.power0"):format(battery_id), charging_rate = ("hw.sensors.acpi%s.power0"):format(battery_id),
last_full_capacity = ("hw.sensors.acpi%s.watthour0"):format(battery_id), last_full_capacity = ("hw.sensors.acpi%s.watthour0"):format(battery_id),
remaining_capacity = ("hw.sensors.acpi%s.watthour3"):format(battery_id), remaining_capacity = ("hw.sensors.acpi%s.watthour3"):format(battery_id),
design_capacity = ("hw.sensors.acpi%s.watthour4"):format(battery_id), design_capacity = ("hw.sensors.acpi%s.watthour4"):format(battery_id),
state = ("hw.sensors.acpi%s.raw0"):format(battery_id) state = ("hw.sensors.acpi%s.raw0"):format(battery_id)
} }
local sysctl_args = {} local sysctl_args = {}
for _, v in pairs(fields) do table.insert(sysctl_args, v) end for _, v in pairs(fields) do table.insert(sysctl_args, v) end
local battery = {} local battery = {}
helpers.sysctl_async(sysctl_args, function(ret) helpers.sysctl_async(sysctl_args, function (ret)
for k, v in pairs(fields) do for k, v in pairs(fields) do
-- discard the description that comes after the values -- discard the description that comes after the values
battery[k] = tonumber(ret[v]:match("(.-) ")) battery[k] = tonumber(ret[v]:match("(.-) "))
end end
local states = { local states = {
[0] = "", -- not charging [0] = "", -- not charging
[1] = "-", -- discharging [1] = "-", -- discharging
[2] = "!", -- critical [2] = "!", -- critical
[3] = "+", -- charging [3] = "+", -- charging
[4] = "N/A", -- unknown status [4] = "N/A", -- unknown status
[255] = "N/A" -- unimplemented by the driver [255] = "N/A" -- unimplemented by the driver
} }
local state = states[battery.state] local state = states[battery.state]
local charge = tonumber(battery.remaining_capacity local charge = tonumber(battery.remaining_capacity
/ battery.last_full_capacity * 100) / battery.last_full_capacity * 100)
local remaining_time local remaining_time
if battery.charging_rate < 1 then if battery.charging_rate < 1 then
remaining_time = "" remaining_time = ""
else else
local raw_time = battery.remaining_capacity / battery.rate local raw_time = battery.remaining_capacity / battery.rate
local hours, hour_fraction = math.modf(raw_time) local hours, hour_fraction = math.modf(raw_time)
local minutes = math.floor(60 * hour_fraction) local minutes = math.floor(60 * hour_fraction)
remaining_time = ("%d:%0.2d"):format(hours, minutes) remaining_time = ("%d:%0.2d"):format(hours, minutes)
end end
local wear = math.floor(battery.last_full_capacity, local wear = math.floor(battery.last_full_capacity,
battery.design_capacity) battery.design_capacity)
-- Pass the following arguments to callback function: -- Pass the following arguments to callback function:
-- * battery state symbol (↯, -, !, + or N/A) -- * battery state symbol (↯, -, !, + or N/A)
-- * remaining capacity (in percent) -- * remaining capacity (in percent)
-- * remaining time, as reported by the battery -- * remaining time, as reported by the battery
-- * wear level (in percent) -- * wear level (in percent)
-- * present_rate (in Watts/hour) -- * present_rate (in Watts/hour)
return callback({ state, charge, remaining_time, return callback({ state, charge, remaining_time,
wear, battery.charging_rate }) wear, battery.charging_rate })
end) end)
end end
return helpers.setasyncall(bat_openbsd) return helpers.setasyncall(bat_openbsd)