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

@ -6,4 +6,12 @@ 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 -- 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: IMPORTANT:
- `volume` now uses 🔉 and 🔈 instead of ♫ and ♩ to show mute state. - `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,weather,contrib/btc] Allow function call without Awesome
- [pkg] Use more updated front-ends for Debian/Ubuntu (apt) and Fedora (dnf) - [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) - [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: Removed:

View File

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

View File

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

View File

@ -34,18 +34,17 @@ local STATES = { [0] = "↯", -- not charging
return helpers.setasyncall{ return helpers.setasyncall{
async = function (format, warg, callback) async = function (format, warg, callback)
local filter = "hw.sensors.acpi" .. (warg or "bat0") if warg == nil then warg = 'bat0' end
local pattern = filter .. ".(%S+)=(%S+)" local pattern = ("hw.sensors.acpi%s.(%S+)=(%S+)"):format(warg)
local bat_info = {}
spawn.with_line_callback_with_shell( spawn.async(
("sysctl -a | grep '^%s'"):format(filter), "sysctl -a",
{ stdout = function (line) function (stdout, stderr, exitreason, exitcode)
for key, value in line:gmatch(pattern) do local bat_info = {}
for key, value in stdout:gmatch(pattern) do
bat_info[key] = value bat_info[key] = value
end end
end,
output_done = function ()
-- current state -- current state
local state = STATES[tonumber(bat_info.raw0)] local state = STATES[tonumber(bat_info.raw0)]
@ -53,10 +52,8 @@ return helpers.setasyncall{
local percent = tonumber( local percent = tonumber(
bat_info.watthour3 / bat_info.watthour0 * 100) bat_info.watthour3 / bat_info.watthour0 * 100)
local time local time = ""
if tonumber(bat_info.power0) < 1 then if tonumber(bat_info.power0) >= 1 then
time = ""
else
local raw_time = bat_info.watthour3 / bat_info.power0 local raw_time = bat_info.watthour3 / bat_info.power0
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)
@ -81,5 +78,5 @@ return helpers.setasyncall{
-- * wear level (in percent) -- * wear level (in percent)
-- * present_rate (in Watts) -- * present_rate (in Watts)
callback{state, percent, time, wear, rate} callback{state, percent, time, wear, rate}
end }) end)
end } end }

View File

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

View File

@ -1,7 +1,8 @@
-- {{{ Grab environment -- {{{ Grab environment
local helpers = require("vicious.helpers")
local math = { floor = math.floor } local math = { floor = math.floor }
local string = { gmatch = string.gmatch } local string = { gmatch = string.gmatch }
local helpers = require("vicious.helpers")
-- }}} -- }}}
@ -54,7 +55,9 @@ function cpu_freebsd.async(format, warg, callback)
for i = 1, #tmp_usage do for i = 1, #tmp_usage do
tmp_usage[i] = tmp_total[i] - cpu_total[i] 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 end
cpu_total = tmp_total cpu_total = tmp_total

View File

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

View File

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

View File

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

View File

@ -1,13 +1,14 @@
-- {{{ Grab environment -- {{{ Grab environment
local tonumber = tonumber local tonumber = tonumber
local math = { floor = math.floor } local math = { floor = math.floor }
local helpers = require("vicious.helpers")
local spawn = require("vicious.spawn")
local string = { local string = {
match = string.match, match = string.match,
gmatch = string.gmatch, gmatch = string.gmatch,
find = string.find find = string.find
} }
local helpers = require("vicious.helpers")
local spawn = require("vicious.spawn")
-- }}} -- }}}
-- Mem: provides RAM and Swap usage statistics -- Mem: provides RAM and Swap usage statistics
@ -23,15 +24,15 @@ function mem_freebsd.async(format, warg, callback)
"vm.swap_enabled" }, "vm.swap_enabled" },
function(ret) function(ret)
local pagesize = tonumber(ret["hw.pagesize"]) local pgsz = tonumber(ret["hw.pagesize"])
local _mem = { buf = {}, total = nil } local _mem = { buf = {}, total = nil }
-- Get memory space in bytes -- Get memory space in bytes
_mem.total = tonumber(ret["vm.stats.vm.v_page_count"]) * pagesize _mem.total = tonumber(ret["vm.stats.vm.v_page_count"]) * pgsz
_mem.buf.free = tonumber(ret["vm.stats.vm.v_free_count"]) * pagesize _mem.buf.free = tonumber(ret["vm.stats.vm.v_free_count"]) * pgsz
_mem.buf.laundry = tonumber(ret["vm.stats.vm.v_laundry_count"]) * pagesize _mem.buf.laundry = tonumber(ret["vm.stats.vm.v_laundry_count"]) * pgsz
_mem.buf.cache = tonumber(ret["vm.stats.vm.v_cache_count"]) * pagesize _mem.buf.cache = tonumber(ret["vm.stats.vm.v_cache_count"]) * pgsz
_mem.buf.wired = tonumber(ret["vm.stats.vm.v_wire_count"]) * pagesize _mem.buf.wired = tonumber(ret["vm.stats.vm.v_wire_count"]) * pgsz
-- Rework into megabytes -- Rework into megabytes
_mem.total = math.floor(_mem.total/1048576) _mem.total = math.floor(_mem.total/1048576)
@ -68,7 +69,8 @@ function mem_freebsd.async(format, warg, callback)
spawn.with_line_callback("swapinfo -m", { spawn.with_line_callback("swapinfo -m", {
stdout = function(line) stdout = function(line)
if not string.find(line, "Device") then 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 -- Add swap space in Mbytes
_swp.total = _swp.total + tonumber(ltotal) _swp.total = _swp.total + tonumber(ltotal)
_swp.inuse = _swp.inuse + tonumber(lused) _swp.inuse = _swp.inuse + tonumber(lused)

View File

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

View File

@ -1,12 +1,13 @@
-- {{{ Grab environment -- {{{ Grab environment
local tonumber = tonumber local tonumber = tonumber
local os = { time = os.time } local os = { time = os.time }
local helpers = require("vicious.helpers")
local spawn = require("vicious.spawn")
local string = { local string = {
match = string.match, match = string.match,
gmatch = string.gmatch gmatch = string.gmatch
} }
local helpers = require("vicious.helpers")
local spawn = require("vicious.spawn")
-- }}} -- }}}
@ -36,7 +37,8 @@ local function parse(stdout, stderr, exitreason, exitcode)
if buffer == nil then if buffer == nil then
buffer = { tonumber(split[8]), tonumber(split[11]) } -- recv (field 8) and send (field 11) buffer = { tonumber(split[8]), tonumber(split[11]) } -- recv (field 8) and send (field 11)
else 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 end
end end

View File

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

View File

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

View File

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