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

View File

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