Nitpick style, mainly to make luacheck happy

Only cover the widget types that were recently refactored.
A few conventions are set as followed:
* Textwidth is preferably 80, since the whole point of tiling windows
  is to be able to view more pane of codes
* Standard, 3rd-party and local require should be grouped together
* Closing parentheses should not be on lines by themselves

There should be a style guide to clarify these.
This commit is contained in:
Nguyễn Gia Phong 2019-08-23 22:50:13 +07:00
parent 025d2e1ad5
commit 83efd26802
17 changed files with 182 additions and 142 deletions

View File

@ -3,7 +3,15 @@ std = "min"
-- Global objects defined by the C code
read_globals = {
"timer", -- deprecated, but used in older versions.
"timer", -- deprecated, but used in older versions.
}
-- Warnings to be ignored
ignore = {
"212", -- Unused argument.
}
-- Not enforced, but preferable
max_code_line_length = 80
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -1,3 +1,5 @@
# Changes in 2.4.0 (WIP)
IMPORTANT:
- `volume` now uses 🔉 and 🔈 instead of ♫ and ♩ to show mute state.
@ -27,6 +29,7 @@ Fixed:
- [pkg,weather,contrib/btc] Allow function call without Awesome
- [pkg] Use more updated front-ends for Debian/Ubuntu (apt) and Fedora (dnf)
- [os] Splitted os_all into os_linux and os_bsd (and refactored to async)
- Tweak `.luacheckrc` to suit functional style and soft-limit text width to 80
Removed:

View File

@ -34,6 +34,12 @@ local spawn = require("vicious.spawn")
-- vicious.helpers
local helpers = {}
-- {{{ Constants definitions
local OS_UNSUPPORTED_ERR = "Vicious: platform not supported: %s"
local NOT_FOUND_MSG = "module '%s' not found"
local NOT_FOUND_ERR = [[
Vicious: %s is not available for the current platform or does not exist]]
-- }}}
-- {{{ Variable definitions
local scroller = {}
@ -56,8 +62,8 @@ end
-- }}}
-- {{{ Loader of vicious modules
function helpers.wrequire(table, key)
local ret = rawget(table, key)
function helpers.wrequire(collection, key)
local ret = rawget(collection, key)
if ret then
return ret
@ -69,27 +75,26 @@ function helpers.wrequire(table, key)
openbsd = { "openbsd", "bsd", "all" }
}
local os = ostable[helpers.getos()]
assert(os, "Vicious: platform not supported: " .. helpers.getos())
local platform = ostable[helpers.getos()]
assert(platform, OS_UNSUPPORTED_ERR:format(helpers.getos()))
for i = 1, #os do
local name = table._NAME .. "." .. key .. "_" .. os[i]
local basename = collection._NAME .. '.' .. key
for i = 1, #platform do
local name = basename .. '_' .. platform[i]
local status, value = pcall(require, name)
if status then
ret = value
break
end
local not_found_msg = "module '"..name.."' not found"
-- ugly but there is afaik no other way to check if a module exists
if value:sub(1, #not_found_msg) ~= not_found_msg then
-- module found, but different issue -> let's raise the real error
require(name)
-- This is ugly but AFAWK there is no other way to check for
-- the type of error. If other error get caught, raise it.
if value:find(NOT_FOUND_MSG:format(name), 1, true) == nil then
require(name)
end
end
assert(ret, "Vicious: widget " .. table._NAME .. "." .. key .. " not available for current platform or does not exist")
assert(ret, NOT_FOUND_ERR:format(basename))
return ret
end
-- }}}
@ -110,8 +115,8 @@ end
-- {{{ Expose path as a Lua table
function helpers.pathtotable(dir)
return setmetatable({ _path = dir },
{ __index = function(table, index)
local path = table._path .. '/' .. index
{ __index = function(self, index)
local path = self._path .. '/' .. index
local f = io.open(path)
if f then
local s = f:read("*all")
@ -120,7 +125,7 @@ function helpers.pathtotable(dir)
return s
else
local o = { _path = path }
setmetatable(o, getmetatable(table))
setmetatable(o, getmetatable(self))
return o
end
end

View File

@ -1,13 +1,13 @@
-- {{{ Grab environment
local tonumber = tonumber
local math = { floor = math.floor }
local helpers = require("vicious.helpers")
local spawn = require("vicious.spawn")
local string = {
gmatch = string.gmatch,
match = string.match,
format = string.format
}
local helpers = require("vicious.helpers")
local spawn = require("vicious.spawn")
-- }}}
-- Battery: provides battery level of requested battery
@ -36,7 +36,7 @@ local function parse(stdout, stderr, exitreason, exitcode)
local state = battery_state[bat_info["State"]] or "N/A"
-- battery capacity in percent
local percent = tonumber(string.match(bat_info["Remaining capacity"], "[%d]+"))
local percent = tonumber(bat_info["Remaining capacity"]:match"[%d]+")
-- use remaining (charging or discharging) time calculated by acpiconf
local time = bat_info["Remaining time"]
@ -47,13 +47,13 @@ local function parse(stdout, stderr, exitreason, exitcode)
-- calculate wear level from (last full / design) capacity
local wear = "N/A"
if bat_info["Last full capacity"] and bat_info["Design capacity"] then
local l_full = tonumber(string.match(bat_info["Last full capacity"], "[%d]+"))
local design = tonumber(string.match(bat_info["Design capacity"], "[%d]+"))
local l_full = tonumber(bat_info["Last full capacity"]:match"[%d]+")
local design = tonumber(bat_info["Design capacity"]:match"[%d]+")
wear = math.floor(l_full / design * 100)
end
-- dis-/charging rate as presented by battery
local rate = string.match(bat_info["Present rate"], "([%d]+)%smW")
local rate = bat_info["Present rate"]:match"([%d]+)%smW"
rate = string.format("%2.1f", tonumber(rate / 1000))
-- returns

View File

@ -34,52 +34,49 @@ local STATES = { [0] = "↯", -- not charging
return helpers.setasyncall{
async = function (format, warg, callback)
local filter = "hw.sensors.acpi" .. (warg or "bat0")
local pattern = filter .. ".(%S+)=(%S+)"
local bat_info = {}
if warg == nil then warg = 'bat0' end
local pattern = ("hw.sensors.acpi%s.(%S+)=(%S+)"):format(warg)
spawn.with_line_callback_with_shell(
("sysctl -a | grep '^%s'"):format(filter),
{ stdout = function (line)
for key, value in line:gmatch(pattern) do
bat_info[key] = value
end
end,
output_done = function ()
-- current state
local state = STATES[tonumber(bat_info.raw0)]
spawn.async(
"sysctl -a",
function (stdout, stderr, exitreason, exitcode)
local bat_info = {}
for key, value in stdout:gmatch(pattern) do
bat_info[key] = value
end
-- battery capacity in percent
local percent = tonumber(
bat_info.watthour3 / bat_info.watthour0 * 100)
-- current state
local state = STATES[tonumber(bat_info.raw0)]
local time
if tonumber(bat_info.power0) < 1 then
time = ""
else
local raw_time = bat_info.watthour3 / bat_info.power0
local hours, hour_fraction = math.modf(raw_time)
local minutes = math.floor(60 * hour_fraction)
time = ("%d:%0.2d"):format(hours, minutes)
end
-- battery capacity in percent
local percent = tonumber(
bat_info.watthour3 / bat_info.watthour0 * 100)
-- calculate wear level from (last full / design) capacity
local wear = "N/A"
if bat_info.watthour0 and bat_info.watthour4 then
local l_full = tonumber(bat_info.watthour0)
local design = tonumber(bat_info.watthour4)
wear = math.floor(l_full / design * 100)
end
local time = ""
if tonumber(bat_info.power0) >= 1 then
local raw_time = bat_info.watthour3 / bat_info.power0
local hours, hour_fraction = math.modf(raw_time)
local minutes = math.floor(60 * hour_fraction)
time = ("%d:%0.2d"):format(hours, minutes)
end
-- dis-/charging rate as presented by battery
local rate = bat_info.power0
-- calculate wear level from (last full / design) capacity
local wear = "N/A"
if bat_info.watthour0 and bat_info.watthour4 then
local l_full = tonumber(bat_info.watthour0)
local design = tonumber(bat_info.watthour4)
wear = math.floor(l_full / design * 100)
end
-- Pass the following arguments to callback function:
-- * battery state symbol (↯, -, !, + or N/A)
-- * remaining_capacity (in percent)
-- * remaining_time, by battery
-- * wear level (in percent)
-- * present_rate (in Watts)
callback{state, percent, time, wear, rate}
end })
-- dis-/charging rate as presented by battery
local rate = bat_info.power0
-- Pass the following arguments to callback function:
-- * battery state symbol (↯, -, !, + or N/A)
-- * remaining_capacity (in percent)
-- * remaining_time, by battery
-- * wear level (in percent)
-- * present_rate (in Watts)
callback{state, percent, time, wear, rate}
end)
end }

View File

@ -12,6 +12,8 @@ local helpers = require"vicious.helpers"
local spawn = require"vicious.spawn"
-- }}}
local CMUS_SOCKET = helpers.shellquote(os.getenv"CMUS_SOCKET")
-- Cmus: provides CMUS information
-- vicious.widgets.cmus
return helpers.setasyncall{
@ -19,8 +21,8 @@ return helpers.setasyncall{
local server = ""
if type(warg) == "table" then
server = " --server " .. helpers.shellquote(warg.host or warg[1])
elseif CMUS_SOCKET then
server = " --server " .. helpers.shellquote(os.getenv"CMUS_SOCKET")
elseif CMUS_SOCKET ~= nil then
server = " --server " .. CMUS_SOCKET
end
local cmus_state = { ["{duration}"] = 0, ["{file}"] = "N/A",

View File

@ -1,7 +1,8 @@
-- {{{ Grab environment
local helpers = require("vicious.helpers")
local math = { floor = math.floor }
local string = { gmatch = string.gmatch }
local helpers = require("vicious.helpers")
-- }}}
@ -39,7 +40,7 @@ function cpu_freebsd.async(format, warg, callback)
tmp_usage[i] = 0
end
-- CPU usage
-- CPU usage
for i, v in ipairs(matches) do
local index = math.floor((i-1) / 5) + 2 -- current cpu
@ -54,7 +55,9 @@ function cpu_freebsd.async(format, warg, callback)
for i = 1, #tmp_usage do
tmp_usage[i] = tmp_total[i] - cpu_total[i]
tmp_usage[i] = math.floor((tmp_usage[i] - (tmp_idle[i] - cpu_idle[i])) / tmp_usage[i] * 100)
tmp_usage[i] = math.floor(
(tmp_usage[i] - (tmp_idle[i] - cpu_idle[i]))
/ tmp_usage[i] * 100)
end
cpu_total = tmp_total

View File

@ -20,13 +20,20 @@ function cpufreq_freebsd.async(format, warg, callback)
["governor"] = "N/A",
}
helpers.sysctl_async({ "dev.cpu." .. warg .. ".freq" }, function(ret)
freqv.mhz = tonumber(ret["dev.cpu." .. warg .. ".freq"])
freqv.ghz = freqv.mhz / 1000
return callback({freqv.mhz, freqv.ghz, freqv.mv, freqv.v, freqv.governor})
end)
helpers.sysctl_async(
{ "dev.cpu." .. warg .. ".freq" },
function (ret)
freqv.mhz = tonumber(ret["dev.cpu." .. warg .. ".freq"])
freqv.ghz = freqv.mhz / 1000
return callback({
freqv.mhz,
freqv.ghz,
freqv.mv,
freqv.v,
freqv.governor
})
end)
end
-- }}}

View File

@ -1,7 +1,8 @@
-- {{{ Grab environment
local helpers = require("vicious.helpers")
local tonumber = tonumber
local type = type
local helpers = require("vicious.helpers")
-- }}}

View File

@ -21,7 +21,7 @@ local mdir_all = {}
function mdir_all.async(format, warg, callback)
if type(warg) ~= "table" then return callback{} end
local starting_points = ""
for i,dir in ipairs(warg) do
for _,dir in ipairs(warg) do
starting_points = starting_points .. " " .. helpers.shellquote(dir)
end
if starting_points == "" then return callback{ 0, 0 } end

View File

@ -1,13 +1,14 @@
-- {{{ Grab environment
local tonumber = tonumber
local math = { floor = math.floor }
local helpers = require("vicious.helpers")
local spawn = require("vicious.spawn")
local string = {
local string = {
match = string.match,
gmatch = string.gmatch,
find = string.find
}
local helpers = require("vicious.helpers")
local spawn = require("vicious.spawn")
-- }}}
-- Mem: provides RAM and Swap usage statistics
@ -17,21 +18,21 @@ local mem_freebsd = {}
-- {{{ Memory widget type
function mem_freebsd.async(format, warg, callback)
helpers.sysctl_async({ "hw.pagesize",
helpers.sysctl_async({ "hw.pagesize",
"vm.stats.vm",
"vm.swap_total",
"vm.swap_enabled" },
function(ret)
local pagesize = tonumber(ret["hw.pagesize"])
local pgsz = tonumber(ret["hw.pagesize"])
local _mem = { buf = {}, total = nil }
-- Get memory space in bytes
_mem.total = tonumber(ret["vm.stats.vm.v_page_count"]) * pagesize
_mem.buf.free = tonumber(ret["vm.stats.vm.v_free_count"]) * pagesize
_mem.buf.laundry = tonumber(ret["vm.stats.vm.v_laundry_count"]) * pagesize
_mem.buf.cache = tonumber(ret["vm.stats.vm.v_cache_count"]) * pagesize
_mem.buf.wired = tonumber(ret["vm.stats.vm.v_wire_count"]) * pagesize
_mem.total = tonumber(ret["vm.stats.vm.v_page_count"]) * pgsz
_mem.buf.free = tonumber(ret["vm.stats.vm.v_free_count"]) * pgsz
_mem.buf.laundry = tonumber(ret["vm.stats.vm.v_laundry_count"]) * pgsz
_mem.buf.cache = tonumber(ret["vm.stats.vm.v_cache_count"]) * pgsz
_mem.buf.wired = tonumber(ret["vm.stats.vm.v_wire_count"]) * pgsz
-- Rework into megabytes
_mem.total = math.floor(_mem.total/1048576)
@ -68,46 +69,47 @@ function mem_freebsd.async(format, warg, callback)
spawn.with_line_callback("swapinfo -m", {
stdout = function(line)
if not string.find(line, "Device") then
local ltotal, lused, lfree = string.match(line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)")
local ltotal, lused, lfree = string.match(
line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)")
-- Add swap space in Mbytes
_swp.total = _swp.total + tonumber(ltotal)
_swp.inuse = _swp.inuse + tonumber(lused)
_swp.buf.free = _swp.buf.free + tonumber(lfree)
end
end,
output_done = function()
output_done = function()
print(_swp.inuse, _swp.total)
_swp.usep = math.floor(_swp.inuse / _swp.total * 100)
callback({ _mem.usep,
_mem.inuse,
_mem.total,
callback({ _mem.usep,
_mem.inuse,
_mem.total,
_mem.free,
_swp.usep,
_swp.inuse,
_swp.total,
_swp.usep,
_swp.inuse,
_swp.total,
_swp.buf.free,
_mem.wirep,
_mem.wire,
_mem.notfreeablep,
_mem.wirep,
_mem.wire,
_mem.notfreeablep,
_mem.notfreeable })
end
end
})
else
_swp.usep = -1
_swp.inuse = -1
_swp.total = -1
_swp.buf.free = -1
callback({ _mem.usep,
_mem.inuse,
_mem.total,
callback({ _mem.usep,
_mem.inuse,
_mem.total,
_mem.free,
_swp.usep,
_swp.inuse,
_swp.total,
_swp.usep,
_swp.inuse,
_swp.total,
_swp.buf.free,
_mem.wirep,
_mem.wire,
_mem.notfreeablep,
_mem.wirep,
_mem.wire,
_mem.notfreeablep,
_mem.notfreeable })
end
end)

View File

@ -7,6 +7,7 @@
local tonumber = tonumber
local math = { floor = math.floor }
local type = type
local helpers = require"vicious.helpers"
local spawn = require"vicious.spawn"
-- }}}
@ -24,7 +25,7 @@ end
-- }}}
-- {{{ Format playing progress
function format_progress(elapsed, duration)
local function format_progress(elapsed, duration)
local em, es = math.floor(elapsed / 60), math.floor(elapsed % 60)
local dm, ds = math.floor(duration / 60), math.floor(duration % 60)
@ -32,18 +33,22 @@ function format_progress(elapsed, duration)
return ("%d:%02d"):format(em, es), ("%d:%02d"):format(dm, ds)
elseif dm < 60 then
return ("%02d:%02d"):format(em, es), ("%02d:%02d"):format(dm, ds)
elseif dm < 600 then
return ("%d:%02d:%02d"):format(math.floor(em / 60), math.floor(em % 60), es),
("%d:%02d:%02d"):format(math.floor(dm / 60), math.floor(dm % 60), ds)
end
local eh, dh = math.floor(em / 60), math.floor(dm / 60)
em, dm = math.floor(em % 60), math.floor(dm % 60)
if dm < 600 then
return ("%d:%02d:%02d"):format(eh, em, es),
("%d:%02d:%02d"):format(dh, dm, ds)
else
return ("%02d:%02d:%02d"):format(math.floor(em / 60), math.floor(em % 60), es),
("%02d:%02d:%02d"):format(math.floor(dm / 60), math.floor(dm % 60), ds)
return ("%02d:%02d:%02d"):format(eh, em, es),
("%02d:%02d:%02d"):format(dh, dm, ds)
end
end
-- }}}
-- {{{ Format playing progress (percentage)
function format_progress_percentage(elapsed, duration)
local function format_progress_percentage(elapsed, duration)
if duration > 0 then
local percentage = math.floor((elapsed / duration) * 100 + 0.5)
return ("%d%%"):format(percentage)

View File

@ -1,12 +1,13 @@
-- {{{ Grab environment
local tonumber = tonumber
local os = { time = os.time }
local helpers = require("vicious.helpers")
local spawn = require("vicious.spawn")
local string = {
local string = {
match = string.match,
gmatch = string.gmatch
}
local helpers = require("vicious.helpers")
local spawn = require("vicious.spawn")
-- }}}
@ -28,7 +29,7 @@ local function parse(stdout, stderr, exitreason, exitcode)
local args = {}
local buffer = nil
local now = os.time()
for line in string.gmatch(stdout, "[^\n]+") do
if not (line:find("<Link") or line:find("Name")) then -- skipping missleading lines
local split = { line:match(("([^%s]*)%s*"):rep(12)) }
@ -36,7 +37,8 @@ local function parse(stdout, stderr, exitreason, exitcode)
if buffer == nil then
buffer = { tonumber(split[8]), tonumber(split[11]) } -- recv (field 8) and send (field 11)
else
buffer = { buffer[1] + tonumber(split[8]), buffer[2] + tonumber(split[11]) }
buffer = { buffer[1] + tonumber(split[8]),
buffer[2] + tonumber(split[11]) }
end
end
end
@ -65,7 +67,7 @@ local function parse(stdout, stderr, exitreason, exitcode)
helpers.uformat(args, "down", down, unit)
helpers.uformat(args, "up", up, unit)
end
nets["time"] = now
-- Store totals

View File

@ -9,8 +9,9 @@ local tonumber = tonumber
local math = { ceil = math.ceil }
local los = { getenv = os.getenv }
local setmetatable = setmetatable
local helpers = require("vicious.helpers")
local string = { gsub = string.gsub }
local helpers = require"vicious.helpers"
-- }}}
@ -32,7 +33,7 @@ local function worker(format)
-- Linux manual page: uname(2)
local kernel = helpers.pathtotable("/proc/sys/kernel")
for k, v in pairs(system) do
for k, _ in pairs(system) do
if kernel[k] then
system[k] = string.gsub(kernel[k], "[%s]*$", "")
end
@ -56,4 +57,5 @@ local function worker(format)
end
-- }}}
return setmetatable(os_linux, { __call = function(_, ...) return worker(...) end })
return setmetatable(os_linux,
{ __call = function(_, ...) return worker(...) end })

View File

@ -1,7 +1,8 @@
-- {{{ Grab environment
local string = { match = string.match }
local helpers = require("vicious.helpers")
local type = type
local helpers = require("vicious.helpers")
-- }}}

View File

@ -1,9 +1,9 @@
-- {{{ Grab environment
local tonumber = tonumber
local math = { floor = math.floor }
local string = { match = string.match }
local helpers = require("vicious.helpers")
local os = { time = os.time }
local helpers = require("vicious.helpers")
-- }}}
@ -14,19 +14,21 @@ local uptime_freebsd = {}
-- {{{ Uptime widget type
function uptime_freebsd.async(format, warg, callback)
helpers.sysctl_async({ "vm.loadavg",
"kern.boottime" },
function(ret)
local l1, l5, l15 = string.match(ret["vm.loadavg"], "{ ([%d]+%.[%d]+) ([%d]+%.[%d]+) ([%d]+%.[%d]+) }")
local up_t = os.time() - tonumber(string.match(ret["kern.boottime"], "sec = ([%d]+)"))
helpers.sysctl_async(
{ "vm.loadavg", "kern.boottime" },
function(ret)
local l1, l5, l15 = ret["vm.loadavg"]:match(
"{ ([%d]+%.[%d]+) ([%d]+%.[%d]+) ([%d]+%.[%d]+) }")
local up_t = os.time() - tonumber(
ret["kern.boottime"]:match"sec = ([%d]+)")
-- Get system uptime
local up_d = math.floor(up_t / (3600 * 24))
local up_h = math.floor((up_t % (3600 * 24)) / 3600)
local up_m = math.floor(((up_t % (3600 * 24)) % 3600) / 60)
-- Get system uptime
local up_d = math.floor(up_t / (3600 * 24))
local up_h = math.floor((up_t % (3600 * 24)) / 3600)
local up_m = math.floor(((up_t % (3600 * 24)) % 3600) / 60)
return callback({ up_d, up_h, up_m, l1, l5, l15 })
end)
return callback({ up_d, up_h, up_m, l1, l5, l15 })
end)
end
-- }}}

View File

@ -46,8 +46,8 @@ function wifiiw_linux.async(format, warg, callback)
spawn.easy_async_with_shell(
LINK:format(warg),
function (stdout, stderr, exitreason, exitcode)
parse_link(stdout)
function (std_out, std_err, exit_reason, exit_code)
parse_link(std_out)
spawn.easy_async_with_shell(
INFO:format(warg),
function (stdout, stderr, exitreason, exitcode)