pull #307: worker -> factory

This commit is contained in:
copycat-killer 2017-02-08 20:45:11 +01:00
parent 3f854a45f5
commit 586c54a257
21 changed files with 127 additions and 141 deletions

View File

@ -7,17 +7,16 @@
--]] --]]
local helpers = require("lain.helpers") local helpers = require("lain.helpers")
local shell = require("awful.util").shell local shell = require("awful.util").shell
local wibox = require("wibox") local wibox = require("wibox")
local string = { match = string.match, local string = { match = string.match,
format = string.format } format = string.format }
local setmetatable = setmetatable
-- ALSA volume -- ALSA volume
-- lain.widget.alsa -- lain.widget.alsa
local function worker(args) local function factory(args)
local alsa = { widget = wibox.widget.textbox() } local alsa = { widget = wibox.widget.textbox() }
local args = args or {} local args = args or {}
local timeout = args.timeout or 5 local timeout = args.timeout or 5
@ -53,4 +52,4 @@ local function worker(args)
return alsa return alsa
end end
return setmetatable({}, { __call = function(_, ...) return worker(...) end }) return factory

View File

@ -32,7 +32,7 @@ local alsabar = {
_muted = false _muted = false
} }
local function worker(args) local function factory(args)
local args = args or {} local args = args or {}
local timeout = args.timeout or 5 local timeout = args.timeout or 5
local settings = args.settings or function() end local settings = args.settings or function() end
@ -135,4 +135,4 @@ local function worker(args)
return alsabar return alsabar
end end
return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end }) return setmetatable(alsabar, { __call = function(_, ...) return factory(...) end })

View File

@ -7,24 +7,23 @@
--]] --]]
local first_line = require("lain.helpers").first_line local first_line = require("lain.helpers").first_line
local newtimer = require("lain.helpers").newtimer local newtimer = require("lain.helpers").newtimer
local naughty = require("naughty") local naughty = require("naughty")
local wibox = require("wibox") local wibox = require("wibox")
local math = { abs = math.abs, local math = { abs = math.abs,
floor = math.floor, floor = math.floor,
log10 = math.log10, log10 = math.log10,
min = math.min } min = math.min }
local string = { format = string.format } local string = { format = string.format }
local ipairs = ipairs local ipairs = ipairs
local type = type local type = type
local tonumber = tonumber local tonumber = tonumber
local setmetatable = setmetatable
-- Battery infos -- Battery infos
-- lain.widget.bat -- lain.widget.bat
local function worker(args) local function factory(args)
local bat = { widget = wibox.widget.textbox() } local bat = { widget = wibox.widget.textbox() }
local args = args or {} local args = args or {}
local timeout = args.timeout or 30 local timeout = args.timeout or 30
@ -179,4 +178,4 @@ local function worker(args)
return bat return bat
end end
return setmetatable({}, { __call = function(_, ...) return worker(...) end }) return factory

View File

@ -98,7 +98,7 @@ function calendar.attach(widget)
awful.button({ }, 5, function () calendar.show(0, 1) end))) awful.button({ }, 5, function () calendar.show(0, 1) end)))
end end
local function worker(args) local function factory(args)
local args = args or {} local args = args or {}
calendar.cal = args.cal or "/usr/bin/cal" calendar.cal = args.cal or "/usr/bin/cal"
calendar.attach_to = args.attach_to or {} calendar.attach_to = args.attach_to or {}
@ -117,4 +117,4 @@ local function worker(args)
for i, widget in ipairs(calendar.attach_to) do calendar.attach(widget) end for i, widget in ipairs(calendar.attach_to) do calendar.attach(widget) end
end end
return setmetatable(calendar, { __call = function(_, ...) return worker(...) end }) return setmetatable(calendar, { __call = function(_, ...) return factory(...) end })

View File

@ -21,7 +21,7 @@ local setmetatable = setmetatable
-- lain.widget.contrib.gpmdp -- lain.widget.contrib.gpmdp
local gpmdp = {} local gpmdp = {}
local function worker(args) local function factory(args)
local args = args or {} local args = args or {}
local timeout = args.timeout or 2 local timeout = args.timeout or 2
local notify = args.notify or "off" local notify = args.notify or "off"
@ -89,4 +89,4 @@ local function worker(args)
return gpmdp return gpmdp
end end
return setmetatable(gpmdp, { __call = function(_, ...) return worker(...) end }) return setmetatable(gpmdp, { __call = function(_, ...) return factory(...) end })

View File

@ -14,6 +14,6 @@
local wrequire = require("lain.helpers").wrequire local wrequire = require("lain.helpers").wrequire
local setmetatable = setmetatable local setmetatable = setmetatable
local widgets = { _NAME = "lain.widget.contrib" } local widget = { _NAME = "lain.widget.contrib" }
return setmetatable(widgets, { __index = wrequire }) return setmetatable(widget, { __index = wrequire })

View File

@ -16,52 +16,50 @@ local setmetatable = setmetatable
-- Keyboard layout switcher -- Keyboard layout switcher
-- lain.widget.contrib.kblayout -- lain.widget.contrib.kblayout
local kbdlayout = {}
local function worker(args) local function factory(args)
local args = args or {} local kbdlayout = { widget = wibox.widget.textbox() }
local layouts = args.layouts or {} local args = args or {}
local settings = args.settings or function () end local layouts = args.layouts or {}
local add_us_secondary = true local settings = args.settings or function () end
local timeout = args.timeout or 5 local add_us_secondary = true
local idx = 1 local timeout = args.timeout or 5
local idx = 1
if args.add_us_secondary == false then add_us_secondary = false end if args.add_us_secondary == false then add_us_secondary = false end
kbdlayout.widget = wibox.widget.textbox() local function kbd_run_settings(layout, variant)
kbdlayout_now = {
layout = string.match(layout, "[^,]+"), -- Make sure to match the primary layout only.
variant = variant
}
widget = kbdlayout.widget
settings()
end
local function kbd_run_settings(layout, variant) function kbdlayout.update()
kbdlayout_now = { helpers.async("setxkbmap -query", function(status)
layout = string.match(layout, "[^,]+"), -- Make sure to match the primary layout only. kbd_run_settings(string.match(status, "layout:%s*([^\n]*)"),
variant = variant string.match(status, "variant:%s*([^\n]*)"))
} end)
widget = kbdlayout.widget end
settings()
end
function kbdlayout.update() function kbdlayout.set(i)
helpers.async("setxkbmap -query", function(status) if #layouts == 0 then return end
kbd_run_settings(string.match(status, "layout:%s*([^\n]*)"), idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed.
string.match(status, "variant:%s*([^\n]*)")) local to_execute = "setxkbmap " .. layouts[idx].layout
end)
end
function kbdlayout.set(i) if add_us_secondary and not string.match(layouts[idx].layout, ",?us,?") then
if #layouts == 0 then return end to_execute = to_execute .. ",us"
idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed. end
local to_execute = "setxkbmap " .. layouts[idx].layout
if add_us_secondary and not string.match(layouts[idx].layout, ",?us,?") then if layouts[idx].variant then
to_execute = to_execute .. ",us" to_execute = to_execute .. " " .. layouts[idx].variant
end end
if layouts[idx].variant then if execute(to_execute) then
to_execute = to_execute .. " " .. layouts[idx].variant kbd_run_settings(layouts[idx].layout, layouts[idx].variant)
end end
if execute(to_execute) then
kbd_run_settings(layouts[idx].layout, layouts[idx].variant)
end
end end
function kbdlayout.next() kbdlayout.set(idx + 1) end function kbdlayout.next() kbdlayout.set(idx + 1) end
@ -77,4 +75,4 @@ local function worker(args)
return kbdlayout return kbdlayout
end end
return setmetatable({}, { __call = function (_, ...) return worker(...) end }) return factory

View File

@ -21,7 +21,7 @@ local setmetatable = setmetatable
-- lain.widget.contrib.moc -- lain.widget.contrib.moc
local moc = {} local moc = {}
local function worker(args) local function factory(args)
local args = args or {} local args = args or {}
local timeout = args.timeout or 2 local timeout = args.timeout or 2
local music_dir = args.music_dir or os.getenv("HOME") .. "/Music" local music_dir = args.music_dir or os.getenv("HOME") .. "/Music"
@ -98,4 +98,4 @@ local function worker(args)
return moc return moc
end end
return setmetatable(moc, { __call = function(_, ...) return worker(...) end }) return setmetatable(moc, { __call = function(_, ...) return factory(...) end })

View File

@ -19,7 +19,7 @@ local setmetatable = setmetatable
-- lain.widget.cpu -- lain.widget.cpu
local cpu = { core = {} } local cpu = { core = {} }
local function worker(args) local function factory(args)
local args = args or {} local args = args or {}
local timeout = args.timeout or 2 local timeout = args.timeout or 2
local settings = args.settings or function() end local settings = args.settings or function() end
@ -79,4 +79,4 @@ local function worker(args)
return cpu return cpu
end end
return setmetatable(cpu, { __call = function(_, ...) return worker(...) end }) return setmetatable(cpu, { __call = function(_, ...) return factory(...) end })

View File

@ -7,15 +7,12 @@
--]] --]]
local helpers = require("lain.helpers") local helpers = require("lain.helpers")
local shell = require("awful.util").shell local shell = require("awful.util").shell
local focused = require("awful.screen").focused local focused = require("awful.screen").focused
local wibox = require("wibox") local wibox = require("wibox")
local naughty = require("naughty") local naughty = require("naughty")
local string = string local string = string
local tonumber = tonumber local tonumber = tonumber
local setmetatable = setmetatable local setmetatable = setmetatable
-- File system disk space usage -- File system disk space usage
@ -45,7 +42,7 @@ function fs.show(seconds, scr)
}) })
end end
local function worker(args) local function factory(args)
local args = args or {} local args = args or {}
local timeout = args.timeout or 600 local timeout = args.timeout or 600
local partition = args.partition or "/" local partition = args.partition or "/"
@ -127,4 +124,4 @@ local function worker(args)
return fs return fs
end end
return setmetatable(fs, { __call = function(_, ...) return worker(...) end }) return setmetatable(fs, { __call = function(_, ...) return factory(...) end })

View File

@ -6,19 +6,18 @@
--]] --]]
local helpers = require("lain.helpers") local helpers = require("lain.helpers")
local naughty = require("naughty") local naughty = require("naughty")
local wibox = require("wibox") local wibox = require("wibox")
local string = { format = string.format, local string = { format = string.format,
gsub = string.gsub } gsub = string.gsub }
local type = type local type = type
local tonumber = tonumber local tonumber = tonumber
local setmetatable = setmetatable
-- Mail IMAP check -- Mail IMAP check
-- lain.widget.imap -- lain.widget.imap
local function worker(args) local function factory(args)
local imap = { widget = wibox.widget.textbox() } local imap = { widget = wibox.widget.textbox() }
local args = args or {} local args = args or {}
local server = args.server local server = args.server
@ -84,4 +83,4 @@ local function worker(args)
return imap return imap
end end
return setmetatable({}, { __call = function(_, ...) return worker(...) end }) return factory

View File

@ -15,6 +15,6 @@
local wrequire = require("lain.helpers").wrequire local wrequire = require("lain.helpers").wrequire
local setmetatable = setmetatable local setmetatable = setmetatable
local widgets = { _NAME = "lain.widget" } local widget = { _NAME = "lain.widget" }
return setmetatable(widgets, { __index = wrequire }) return setmetatable(widget, { __index = wrequire })

View File

@ -18,7 +18,7 @@ local setmetatable = setmetatable
-- lain.widget.mem -- lain.widget.mem
local mem = {} local mem = {}
local function worker(args) local function factory(args)
local args = args or {} local args = args or {}
local timeout = args.timeout or 2 local timeout = args.timeout or 2
local settings = args.settings or function() end local settings = args.settings or function() end
@ -53,4 +53,4 @@ local function worker(args)
return mem return mem
end end
return setmetatable(mem, { __call = function(_, ...) return worker(...) end }) return setmetatable(mem, { __call = function(_, ...) return factory(...) end })

View File

@ -23,7 +23,7 @@ local setmetatable = setmetatable
-- lain.widget.mpd -- lain.widget.mpd
local mpd = {} local mpd = {}
local function worker(args) local function factory(args)
local args = args or {} local args = args or {}
local timeout = args.timeout or 2 local timeout = args.timeout or 2
local password = (args.password and #args.password > 0 and string.format("password %s\\n", args.password)) or "" local password = (args.password and #args.password > 0 and string.format("password %s\\n", args.password)) or ""
@ -134,4 +134,4 @@ local function worker(args)
return mpd return mpd
end end
return setmetatable(mpd, { __call = function(_, ...) return worker(...) end }) return setmetatable(mpd, { __call = function(_, ...) return factory(...) end })

View File

@ -7,17 +7,16 @@
--]] --]]
local helpers = require("lain.helpers") local helpers = require("lain.helpers")
local naughty = require("naughty") local naughty = require("naughty")
local wibox = require("wibox") local wibox = require("wibox")
local string = { format = string.format, local string = { format = string.format,
match = string.match } match = string.match }
local setmetatable = setmetatable
-- Network infos -- Network infos
-- lain.widget.net -- lain.widget.net
local function worker(args) local function factory(args)
local net = { widget = wibox.widget.textbox() } local net = { widget = wibox.widget.textbox() }
net.last_t = 0 net.last_t = 0
net.last_r = 0 net.last_r = 0
@ -120,4 +119,4 @@ local function worker(args)
return net return net
end end
return setmetatable({}, { __call = function(_, ...) return worker(...) end }) return factory

View File

@ -6,18 +6,17 @@
--]] --]]
local helpers = require("lain.helpers") local helpers = require("lain.helpers")
local shell = require("awful.util").shell local shell = require("awful.util").shell
local wibox = require("wibox") local wibox = require("wibox")
local string = { gmatch = string.gmatch, local string = { gmatch = string.gmatch,
match = string.match, match = string.match,
format = string.format } format = string.format }
local setmetatable = setmetatable
-- PulseAudio volume -- PulseAudio volume
-- lain.widget.pulseaudio -- lain.widget.pulseaudio
local function worker(args) local function factory(args)
local pulseaudio = { widget = wibox.widget.textbox() } local pulseaudio = { widget = wibox.widget.textbox() }
local args = args or {} local args = args or {}
local devicetype = args.devicetype or "sink" local devicetype = args.devicetype or "sink"
@ -59,4 +58,4 @@ local function worker(args)
return pulseaudio return pulseaudio
end end
return setmetatable({}, { __call = function(_, ...) return worker(...) end }) return factory

View File

@ -33,7 +33,7 @@ local pulsebar = {
_muted = false _muted = false
} }
local function worker(args) local function factory(args)
local args = args or {} local args = args or {}
local timeout = args.timeout or 5 local timeout = args.timeout or 5
local settings = args.settings or function() end local settings = args.settings or function() end
@ -145,4 +145,4 @@ local function worker(args)
return pulsebar return pulsebar
end end
return setmetatable(pulsebar, { __call = function(_, ...) return worker(...) end }) return setmetatable(pulsebar, { __call = function(_, ...) return factory(...) end })

View File

@ -17,7 +17,7 @@ local setmetatable = setmetatable
-- lain.widget.sysload -- lain.widget.sysload
local sysload = {} local sysload = {}
local function worker(args) local function factory(args)
local args = args or {} local args = args or {}
local timeout = args.timeout or 2 local timeout = args.timeout or 2
local settings = args.settings or function() end local settings = args.settings or function() end
@ -40,4 +40,4 @@ local function worker(args)
return sysload return sysload
end end
return setmetatable(sysload, { __call = function(_, ...) return worker(...) end }) return setmetatable(sysload, { __call = function(_, ...) return factory(...) end })

View File

@ -16,7 +16,7 @@ local setmetatable = setmetatable
-- lain.widget.temp -- lain.widget.temp
local temp = {} local temp = {}
local function worker(args) local function factory(args)
local args = args or {} local args = args or {}
local timeout = args.timeout or 2 local timeout = args.timeout or 2
local tempfile = args.tempfile or "/sys/class/thermal/thermal_zone0/temp" local tempfile = args.tempfile or "/sys/class/thermal/thermal_zone0/temp"
@ -42,4 +42,4 @@ local function worker(args)
return temp return temp
end end
return setmetatable(temp, { __call = function(_, ...) return worker(...) end }) return setmetatable(temp, { __call = function(_, ...) return factory(...) end })

View File

@ -6,15 +6,14 @@
--]] --]]
local helpers = require("lain.helpers") local helpers = require("lain.helpers")
local textbox = require("wibox.widget.textbox") local textbox = require("wibox.widget.textbox")
local setmetatable = setmetatable
-- Template for asynchronous watcher widgets -- Template for asynchronous watcher widgets
-- lain.widget.watch -- lain.widget.watch
local function worker(args) local function factory(args)
local watch = {} local watch = { widget = args.widget or textbox() }
local args = args or {} local args = args or {}
local timeout = args.timeout or 5 local timeout = args.timeout or 5
local nostart = args.nostart or false local nostart = args.nostart or false
@ -22,8 +21,6 @@ local function worker(args)
local cmd = args.cmd local cmd = args.cmd
local settings = args.settings or function() widget:set_text(output) end local settings = args.settings or function() widget:set_text(output) end
watch.widget = args.widget or textbox()
function watch.update() function watch.update()
helpers.async(cmd, function(f) helpers.async(cmd, function(f)
output = f output = f
@ -40,4 +37,4 @@ local function worker(args)
return watch return watch
end end
return setmetatable({}, { __call = function(_, ...) return worker(...) end }) return factory

View File

@ -6,25 +6,24 @@
--]] --]]
local helpers = require("lain.helpers") local helpers = require("lain.helpers")
local json = require("lain.util").dkjson local json = require("lain.util").dkjson
local focused = require("awful.screen").focused local focused = require("awful.screen").focused
local naughty = require("naughty") local naughty = require("naughty")
local wibox = require("wibox") local wibox = require("wibox")
local math = { floor = math.floor } local math = { floor = math.floor }
local os = { time = os.time, local os = { time = os.time,
date = os.date, date = os.date,
difftime = os.difftime } difftime = os.difftime }
local string = { format = string.format, local string = { format = string.format,
gsub = string.gsub } gsub = string.gsub }
local tonumber = tonumber local tonumber = tonumber
local setmetatable = setmetatable
-- OpenWeatherMap -- OpenWeatherMap
-- current weather and X-days forecast -- current weather and X-days forecast
-- lain.widget.weather -- lain.widget.weather
local function worker(args) local function factory(args)
local weather = { widget = wibox.widget.textbox() } local weather = { widget = wibox.widget.textbox() }
local args = args or {} local args = args or {}
local APPID = args.APPID or "3e321f9414eaedbfab34983bda77a66e" -- lain default local APPID = args.APPID or "3e321f9414eaedbfab34983bda77a66e" -- lain default
@ -171,4 +170,4 @@ local function worker(args)
return weather return weather
end end
return setmetatable({}, { __call = function(_, ...) return worker(...) end }) return factory