Ported vicious.widgets module to lua 5.2

Signed-off-by: Arvydas Sidorenko <asido4@gmail.com>
Signed-off-by: Adrian C. (anrxc) <anrxc@sysphere.org>
This commit is contained in:
Arvydas Sidorenko 2012-06-15 18:07:05 +02:00 committed by Adrian C. (anrxc)
parent b6b5290093
commit 41cc2c0e27
25 changed files with 132 additions and 107 deletions

View File

@ -16,7 +16,8 @@ local math = {
-- Bat: provides state, charge, and remaining time for a requested battery
module("vicious.widgets.bat")
-- vicious.widgets.bat
local bat = {}
-- {{{ Battery widget type
@ -86,4 +87,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(mt, { __call = function(_, ...) return worker(...) end })

View File

@ -19,7 +19,8 @@ local string = {
-- Cpu: provides CPU usage for all available CPUs/cores
module("vicious.widgets.cpu")
-- vicious.widgets.cpu
local cpu = {}
-- Initialize function tables
@ -74,4 +75,4 @@ local function worker(format)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(cpu, { __call = function(_, ...) return worker(...) end })

View File

@ -12,14 +12,15 @@ local helpers = require("vicious.helpers")
-- Cpufreq: provides freq, voltage and governor info for a requested CPU
module("vicious.widgets.cpufreq")
-- vicious.widgets.cpufreq
local cpufreq = {}
-- {{{ CPU frequency widget type
local function worker(format, warg)
if not warg then return end
local cpufreq = helpers.pathtotable("/sys/devices/system/cpu/"..warg.."/cpufreq")
local _cpufreq = helpers.pathtotable("/sys/devices/system/cpu/"..warg.."/cpufreq")
local governor_state = {
["ondemand\n"] = "",
["powersave\n"] = "",
@ -34,22 +35,22 @@ local function worker(format, warg)
}
-- Get the current frequency
local freq = tonumber(cpufreq.scaling_cur_freq)
local freq = tonumber(_cpufreq.scaling_cur_freq)
-- Calculate MHz and GHz
if freq then
freqv.mhz = freq / 1000
freqv.ghz = freqv.mhz / 1000
-- Get the current voltage
if cpufreq.scaling_voltages then
freqv.mv = tonumber(string.match(cpufreq.scaling_voltages, freq.."[%s]([%d]+)"))
if _cpufreq.scaling_voltages then
freqv.mv = tonumber(string.match(_cpufreq.scaling_voltages, freq.."[%s]([%d]+)"))
-- Calculate voltage from mV
freqv.v = freqv.mv / 1000
end
end
-- Get the current governor
local governor = cpufreq.scaling_governor
local governor = _cpufreq.scaling_governor
-- Represent the governor as a symbol
governor = governor_state[governor] or governor or "N/A"
@ -57,4 +58,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(cpufreq, { __call = function(_, ...) return worker(...) end })

View File

@ -12,7 +12,8 @@ local string = { gmatch = string.gmatch }
-- Cpuinf: provides speed and cache information for all available CPUs/cores
module("vicious.widgets.cpuinf")
-- vicious.widgets.cpuinf
local cpuinf = {}
-- {{{ CPU Information widget type
@ -40,4 +41,4 @@ local function worker(format)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(cpuinf, { __call = function(_, ...) return worker(...) end })

View File

@ -14,7 +14,8 @@ local os = {
-- Date: provides access to os.date with optional time formatting
module("vicious.widgets.date")
-- vicious.widgets.date
local date = {}
-- {{{ Date widget type
@ -23,4 +24,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(date, { __call = function(_, ...) return worker(...) end })

View File

@ -17,7 +17,8 @@ local os = {
-- Disk I/O: provides I/O statistics for requested storage devices
module("vicious.widgets.dio")
-- vicious.widgets.dio
local dio = {}
-- Initialize function tables
@ -69,4 +70,4 @@ local function worker(format)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(dio, { __call = function(_, ...) return worker(...) end })

View File

@ -14,7 +14,8 @@ local helpers = require("vicious.helpers")
-- FS: provides file system disk space usage
module("vicious.widgets.fs")
-- vicious.widgets.fs
local fs = {}
-- Variable definitions
@ -48,4 +49,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(fs, { __call = function(_, ...) return worker(...) end })

View File

@ -17,7 +17,8 @@ local string = {
-- Gmail: provides count of new and subject of last e-mail on Gmail
module("vicious.widgets.gmail")
-- vicious.widgets.gmail
local gmail = {}
-- {{{ Variable definitions
@ -79,4 +80,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(gmail, { __call = function(_, ...) return worker(...) end })

View File

@ -12,7 +12,8 @@ local string = { gmatch = string.gmatch }
-- Hddtemp: provides hard drive temperatures using the hddtemp daemon
module("vicious.widgets.hddtemp")
-- vicious.widgets.hddtemp
local hddtemp = {}
-- {{{ HDD Temperature widget type
@ -34,4 +35,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(hddtemp, { __call = function(_, ...) return worker(...) end })

View File

@ -10,8 +10,9 @@ local setmetatable = setmetatable
local wrequire = require("vicious.helpers").wrequire
-- Vicious: widgets for the awesome window manager
module("vicious.widgets")
-- vicious.widgets
local widgets = { _NAME = "vicious.widgets" }
-- }}}
-- Load modules at runtime as needed
setmetatable(_M, { __index = wrequire })
return setmetatable(widgets, { __index = wrequire })

View File

@ -13,7 +13,8 @@ local helpers = require("vicious.helpers")
-- Mbox: provides the subject of last e-mail in a mbox file
module("vicious.widgets.mbox")
-- vicious.widgets.mbox
local mbox = {}
-- Initialize variables
@ -24,9 +25,9 @@ local function worker(format, warg)
if not warg then return end
-- mbox could be huge, get a 30kb chunk from EOF
if type(warg) ~= "table" then mbox = warg end
if type(warg) ~= "table" then _mbox = warg end
-- * attachment could be much bigger than 30kb
local f = io.open(mbox or warg[1])
local f = io.open(_mbox or warg[1])
f:seek("end", -30720)
local txt = f:read("*all")
f:close()
@ -49,4 +50,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(mbox, { __call = function(_, ...) return worker(...) end })

View File

@ -11,7 +11,8 @@ local string = { find = string.find }
-- Mboxc: provides the count of total, old and new messages in mbox files
module("vicious.widgets.mboxc")
-- vicious.widgets.mboxc
local mboxc = {}
-- {{{ Mbox count widget type
@ -54,4 +55,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(mbox, { __call = function(_, ...) return worker(...) end })

View File

@ -11,7 +11,8 @@ local setmetatable = setmetatable
-- Mdir: provides the number of new and unread messages in Maildir structures/dirs
module("vicious.widgets.mdir")
-- vicious.widgets.mdir
local mdir = {}
-- {{{ Maildir widget type
@ -37,4 +38,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(mdir, { __call = function(_, ...) return worker(...) end })

View File

@ -13,39 +13,40 @@ local string = { gmatch = string.gmatch }
-- Mem: provides RAM and Swap usage statistics
module("vicious.widgets.mem")
-- vicious.widgets.mem
local mem = {}
-- {{{ Memory widget type
local function worker(format)
local mem = { buf = {}, swp = {} }
local _mem = { buf = {}, swp = {} }
-- Get MEM info
for line in io.lines("/proc/meminfo") do
for k, v in string.gmatch(line, "([%a]+):[%s]+([%d]+).+") do
if k == "MemTotal" then mem.total = math.floor(v/1024)
elseif k == "MemFree" then mem.buf.f = math.floor(v/1024)
elseif k == "Buffers" then mem.buf.b = math.floor(v/1024)
elseif k == "Cached" then mem.buf.c = math.floor(v/1024)
elseif k == "SwapTotal" then mem.swp.t = math.floor(v/1024)
elseif k == "SwapFree" then mem.swp.f = math.floor(v/1024)
if k == "MemTotal" then _mem.total = math.floor(v/1024)
elseif k == "MemFree" then _mem.buf.f = math.floor(v/1024)
elseif k == "Buffers" then _mem.buf.b = math.floor(v/1024)
elseif k == "Cached" then _mem.buf.c = math.floor(v/1024)
elseif k == "SwapTotal" then _mem.swp.t = math.floor(v/1024)
elseif k == "SwapFree" then _mem.swp.f = math.floor(v/1024)
end
end
end
-- Calculate memory percentage
mem.free = mem.buf.f + mem.buf.b + mem.buf.c
mem.inuse = mem.total - mem.free
mem.bcuse = mem.total - mem.buf.f
mem.usep = math.floor(mem.inuse / mem.total * 100)
_mem.free = _mem.buf.f + _mem.buf.b + _mem.buf.c
_mem.inuse = _mem.total - _mem.free
_mem.bcuse = _mem.total - _mem.buf.f
_mem.usep = math.floor(_mem.inuse / _mem.total * 100)
-- Calculate swap percentage
mem.swp.inuse = mem.swp.t - mem.swp.f
mem.swp.usep = math.floor(mem.swp.inuse / mem.swp.t * 100)
_mem.swp.inuse = _mem.swp.t - _mem.swp.f
_mem.swp.usep = math.floor(_mem.swp.inuse / _mem.swp.t * 100)
return {mem.usep, mem.inuse, mem.total, mem.free,
mem.swp.usep, mem.swp.inuse, mem.swp.t, mem.swp.f,
mem.bcuse }
return {_mem.usep, _mem.inuse, _mem.total, _mem.free,
_mem.swp.usep, _mem.swp.inuse, _mem.swp.t, _mem.swp.f,
_mem.bcuse }
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(mem, { __call = function(_, ...) return worker(...) end })

View File

@ -13,7 +13,8 @@ local helpers = require("vicious.helpers")
-- Mpd: provides Music Player Daemon information
module("vicious.widgets.mpd")
-- vicious.widgets.mpd
local mpd = {}
-- {{{ MPD widget type
@ -60,4 +61,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(mpd, { __call = function(_, ...) return worker(...) end })

View File

@ -15,7 +15,8 @@ local helpers = require("vicious.helpers")
-- Net: provides state and usage statistics of all network interfaces
module("vicious.widgets.net")
-- vicious.widgets.net
local net = {}
-- Initialize function tables
@ -76,4 +77,4 @@ local function worker(format)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(net, { __call = function(_, ...) return worker(...) end })

View File

@ -16,7 +16,8 @@ local os = {
-- Org: provides agenda statistics for Emacs org-mode
module("vicious.widgets.org")
-- vicious.widgets.org
local org = {}
-- {{{ OrgMode widget type
@ -58,4 +59,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(org, { __call = function(_, ...) return worker(...) end })

View File

@ -19,7 +19,8 @@ local string = {
-- OS: provides operating system information
module("vicious.widgets.os")
-- vicious.widgets.os
local os = {}
-- {{{ Operating system widget type
@ -69,4 +70,4 @@ local function worker(format)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(os, { __call = function(_, ...) return worker(...) end })

View File

@ -11,7 +11,8 @@ local setmetatable = setmetatable
-- Pkg: provides number of pending updates on UNIX systems
module("vicious.widgets.pkg")
-- vicious.widgets.pkg
local pkg = {}
-- {{{ Packages widget type
@ -31,16 +32,16 @@ local function worker(format, warg)
}
-- Check if updates are available
local pkg = manager[warg]
local f = io.popen(pkg.cmd)
local _pkg = manager[warg]
local f = io.popen(_pkg.cmd)
for line in f:lines() do
updates = updates + 1
end
f:close()
return {pkg.sub and math.max(updates-pkg.sub, 0) or updates}
return {_pkg.sub and math.max(updates-_pkg.sub, 0) or updates}
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(pkg, { __call = function(_, ...) return worker(...) end })

View File

@ -16,7 +16,8 @@ local string = {
-- Raid: provides state information for a requested RAID array
module("vicious.widgets.raid")
-- vicious.widgets.raid
local raid = {}
-- Initialize function tables
@ -54,4 +55,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(raid, { __call = function(_, ...) return worker(...) end })

View File

@ -13,7 +13,8 @@ local helpers = require("vicious.helpers")
-- Thermal: provides temperature levels of ACPI and coretemp thermal zones
module("vicious.widgets.thermal")
-- vicious.widgets.thermal
local thermal = {}
-- {{{ Thermal widget type
@ -28,9 +29,9 @@ local function worker(format, warg)
warg = type(warg) == "table" and warg or { warg, "sys" }
-- Get temperature from thermal zone
local thermal = helpers.pathtotable(zone[warg[2]][1] .. warg[1])
local _thermal = helpers.pathtotable(zone[warg[2]][1] .. warg[1])
local data = warg[3] and thermal[warg[3]] or thermal[zone[warg[2]].file]
local data = warg[3] and _thermal[warg[3]] or _thermal[zone[warg[2]].file]
if data then
if zone[warg[2]].div then
return {data / zone[warg[2]].div}
@ -43,4 +44,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(thermal, { __call = function(_, ...) return worker(...) end })

View File

@ -13,7 +13,8 @@ local helpers = require("vicious.helpers")
-- Uptime: provides system uptime and load information
module("vicious.widgets.uptime")
-- vicious.widgets.uptime
local uptime = {}
-- {{{ Uptime widget type
@ -32,4 +33,4 @@ local function worker(format)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
local setmetatable(uptime, { __call = function(_, ...) return worker(...) end })

View File

@ -12,7 +12,8 @@ local string = { match = string.match }
-- Volume: provides volume levels and state of requested ALSA mixers
module("vicious.widgets.volume")
-- vicious.widgets.volume
local volume = {}
-- {{{ Volume widget type
@ -49,4 +50,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(volume, { __call = function(_, ...) return worker(...) end })

View File

@ -14,11 +14,12 @@ local helpers = require("vicious.helpers")
-- Weather: provides weather information for a requested station
module("vicious.widgets.weather")
-- vicious.widgets.weather
local weather = {}
-- Initialize function tables
local weather = {
local _weather = {
["{city}"] = "N/A",
["{wind}"] = "N/A",
["{windmph}"] = "N/A",
@ -43,43 +44,43 @@ local function worker(format, warg)
f:close()
-- Check if there was a timeout or a problem with the station
if ws == nil then return weather end
if ws == nil then return _weather end
weather["{city}"] = -- City and/or area
string.match(ws, "^(.+)%,.*%([%u]+%)") or weather["{city}"]
weather["{wind}"] = -- Wind direction and degrees if available
string.match(ws, "Wind:[%s][%a]+[%s][%a]+[%s](.+)[%s]at.+$") or weather["{wind}"]
weather["{windmph}"] = -- Wind speed in MPH if available
string.match(ws, "Wind:[%s].+[%s]at[%s]([%d]+)[%s]MPH") or weather["{windmph}"]
weather["{sky}"] = -- Sky conditions if available
string.match(ws, "Sky[%s]conditions:[%s](.-)[%c]") or weather["{sky}"]
weather["{weather}"] = -- Weather conditions if available
string.match(ws, "Weather:[%s](.-)[%c]") or weather["{weather}"]
weather["{tempf}"] = -- Temperature in fahrenheit
string.match(ws, "Temperature:[%s]([%-]?[%d%.]+).*[%c]") or weather["{tempf}"]
weather["{humid}"] = -- Relative humidity in percent
string.match(ws, "Relative[%s]Humidity:[%s]([%d]+)%%") or weather["{humid}"]
weather["{press}"] = -- Pressure in hPa
string.match(ws, "Pressure[%s].+%((.+)[%s]hPa%)") or weather["{press}"]
_weather["{city}"] = -- City and/or area
string.match(ws, "^(.+)%,.*%([%u]+%)") or _weather["{city}"]
_weather["{wind}"] = -- Wind direction and degrees if available
string.match(ws, "Wind:[%s][%a]+[%s][%a]+[%s](.+)[%s]at.+$") or _weather["{wind}"]
_weather["{windmph}"] = -- Wind speed in MPH if available
string.match(ws, "Wind:[%s].+[%s]at[%s]([%d]+)[%s]MPH") or _weather["{windmph}"]
_weather["{sky}"] = -- Sky conditions if available
string.match(ws, "Sky[%s]conditions:[%s](.-)[%c]") or _weather["{sky}"]
_weather["{weather}"] = -- Weather conditions if available
string.match(ws, "Weather:[%s](.-)[%c]") or _weather["{weather}"]
_weather["{tempf}"] = -- Temperature in fahrenheit
string.match(ws, "Temperature:[%s]([%-]?[%d%.]+).*[%c]") or _weather["{tempf}"]
_weather["{humid}"] = -- Relative humidity in percent
string.match(ws, "Relative[%s]Humidity:[%s]([%d]+)%%") or _weather["{humid}"]
_weather["{press}"] = -- Pressure in hPa
string.match(ws, "Pressure[%s].+%((.+)[%s]hPa%)") or _weather["{press}"]
-- Wind speed in km/h if MPH was available
if weather["{windmph}"] ~= "N/A" then
weather["{windmph}"] = tonumber(weather["{windmph}"])
weather["{windkmh}"] = math.ceil(weather["{windmph}"] * 1.6)
if _weather["{windmph}"] ~= "N/A" then
_weather["{windmph}"] = tonumber(_weather["{windmph}"])
_weather["{windkmh}"] = math.ceil(_weather["{windmph}"] * 1.6)
end -- Temperature in °C if °F was available
if weather["{tempf}"] ~= "N/A" then
weather["{tempf}"] = tonumber(weather["{tempf}"])
weather["{tempc}"] = math.ceil((weather["{tempf}"] - 32) * 5/9)
if _weather["{tempf}"] ~= "N/A" then
_weather["{tempf}"] = tonumber(_weather["{tempf}"])
_weather["{tempc}"] = math.ceil((_weather["{tempf}"] - 32) * 5/9)
end -- Capitalize some stats so they don't look so out of place
if weather["{sky}"] ~= "N/A" then
weather["{sky}"] = helpers.capitalize(weather["{sky}"])
if _weather["{sky}"] ~= "N/A" then
_weather["{sky}"] = helpers.capitalize(_weather["{sky}"])
end
if weather["{weather}"] ~= "N/A" then
weather["{weather}"] = helpers.capitalize(weather["{weather}"])
if _weather["{weather}"] ~= "N/A" then
_weather["{weather}"] = helpers.capitalize(_weather["{weather}"])
end
return weather
return _weather
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(weather, { __call = function(_, ...) return worker(...) end })

View File

@ -20,7 +20,8 @@ local string = {
-- Wifi: provides wireless information for a requested interface
module("vicious.widgets.wifi")
-- vicious.widgets.wifi
local wifi = {}
-- {{{ Wireless widget type
@ -77,4 +78,4 @@ local function worker(format, warg)
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
return setmetatable(wifi, { __call = function(_, ...) return worker(...) end })