From 83efd26802ee926ca39868555c3f160f6adae778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Gia=20Phong?= Date: Fri, 23 Aug 2019 22:50:13 +0700 Subject: [PATCH] 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. --- .luacheckrc | 10 ++++- Changes.md | 3 ++ helpers.lua | 37 +++++++++------- widgets/bat_freebsd.lua | 14 +++---- widgets/bat_openbsd.lua | 81 +++++++++++++++++------------------- widgets/cmus_all.lua | 6 ++- widgets/cpu_freebsd.lua | 9 ++-- widgets/cpufreq_freebsd.lua | 19 ++++++--- widgets/fanspeed_freebsd.lua | 3 +- widgets/mdir_all.lua | 2 +- widgets/mem_freebsd.lua | 64 ++++++++++++++-------------- widgets/mpd_all.lua | 19 +++++---- widgets/net_freebsd.lua | 14 ++++--- widgets/os_linux.lua | 8 ++-- widgets/thermal_freebsd.lua | 3 +- widgets/uptime_freebsd.lua | 28 +++++++------ widgets/wifiiw_linux.lua | 4 +- 17 files changed, 182 insertions(+), 142 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 7aacbc1..6c05d07 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -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 diff --git a/Changes.md b/Changes.md index 4873286..bc453fa 100644 --- a/Changes.md +++ b/Changes.md @@ -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: diff --git a/helpers.lua b/helpers.lua index 5fab4cf..c3d4a22 100644 --- a/helpers.lua +++ b/helpers.lua @@ -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 diff --git a/widgets/bat_freebsd.lua b/widgets/bat_freebsd.lua index f2cf8eb..c6e743c 100644 --- a/widgets/bat_freebsd.lua +++ b/widgets/bat_freebsd.lua @@ -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 diff --git a/widgets/bat_openbsd.lua b/widgets/bat_openbsd.lua index ced83d8..6718b9b 100644 --- a/widgets/bat_openbsd.lua +++ b/widgets/bat_openbsd.lua @@ -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 } diff --git a/widgets/cmus_all.lua b/widgets/cmus_all.lua index 04581bf..25c1a9a 100644 --- a/widgets/cmus_all.lua +++ b/widgets/cmus_all.lua @@ -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", diff --git a/widgets/cpu_freebsd.lua b/widgets/cpu_freebsd.lua index c6e5272..972eca0 100644 --- a/widgets/cpu_freebsd.lua +++ b/widgets/cpu_freebsd.lua @@ -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 diff --git a/widgets/cpufreq_freebsd.lua b/widgets/cpufreq_freebsd.lua index c53e2d7..6acf2f0 100644 --- a/widgets/cpufreq_freebsd.lua +++ b/widgets/cpufreq_freebsd.lua @@ -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 -- }}} diff --git a/widgets/fanspeed_freebsd.lua b/widgets/fanspeed_freebsd.lua index e55a51f..a60b272 100644 --- a/widgets/fanspeed_freebsd.lua +++ b/widgets/fanspeed_freebsd.lua @@ -1,7 +1,8 @@ -- {{{ Grab environment -local helpers = require("vicious.helpers") local tonumber = tonumber local type = type + +local helpers = require("vicious.helpers") -- }}} diff --git a/widgets/mdir_all.lua b/widgets/mdir_all.lua index 184f41e..7853d54 100644 --- a/widgets/mdir_all.lua +++ b/widgets/mdir_all.lua @@ -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 diff --git a/widgets/mem_freebsd.lua b/widgets/mem_freebsd.lua index ebc4484..369039e 100644 --- a/widgets/mem_freebsd.lua +++ b/widgets/mem_freebsd.lua @@ -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) diff --git a/widgets/mpd_all.lua b/widgets/mpd_all.lua index e8a5afd..b54a1e2 100644 --- a/widgets/mpd_all.lua +++ b/widgets/mpd_all.lua @@ -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) diff --git a/widgets/net_freebsd.lua b/widgets/net_freebsd.lua index 2aa1180..41a2eb7 100644 --- a/widgets/net_freebsd.lua +++ b/widgets/net_freebsd.lua @@ -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("