From 945f220e022e69c1aeda0ee86558be26f19bdd40 Mon Sep 17 00:00:00 2001 From: Dario Gjorgjevski Date: Thu, 13 Aug 2015 01:07:32 +0200 Subject: [PATCH 1/8] add new widget (kbdlayout.lua) --- widgets/contrib/kbdlayout.lua | 58 +++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 widgets/contrib/kbdlayout.lua diff --git a/widgets/contrib/kbdlayout.lua b/widgets/contrib/kbdlayout.lua new file mode 100644 index 0000000..c5242c1 --- /dev/null +++ b/widgets/contrib/kbdlayout.lua @@ -0,0 +1,58 @@ + +local newtimer = require("lain.helpers").newtimer +local wibox = require("wibox") + +local string = { match = string.match } +local io = { popen = io.popen } + +local setmetatable = setmetatable + +local function worker (args) + local kbdlayout = {} + kbdlayout.widget = wibox.widget.textbox('') + + local settings = args.settings or function () end + local layouts = args.layouts + local idx = 1 + + local function run_settings (layout, variant) + widget = kbdlayout.widget + kbdlayout_now = { layout=layout, variant=variant } + settings() + end + + function kbdlayout.update () + local file = assert(io.popen('setxkbmap -query')) + local status = file:read('*all') + file:close() + + run_settings(string.match(status, "layout:%s*([^\n]*)%s*"), + string.match(status, "variant:%s*([^\n]*)%s*")) + end + + function kbdlayout.set (i) + idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed. + local to_execute = 'setxkbmap ' .. layouts[idx].layout + + if layouts[idx].variant then + to_execute = to_execute .. ' ' .. layouts[idx].variant + end + + if os.execute(to_execute) then + run_settings(layouts[idx].layout, layouts[idx].variant) + end + end + + function kbdlayout.next () + kbdlayout.set(idx + 1) + end + + function kbdlayout.prev () + kbdlayout.set(idx - 1) + end + + newtimer("kbdlayout", args.timeout or 10, kbdlayout.update) + return setmetatable(kbdlayout, { __index = kbdlayout.widget }) +end + +return setmetatable({}, { __call = function (_, ...) return worker(...) end }) From 6603c641c6ce675b010ca1977423747ca55d8795 Mon Sep 17 00:00:00 2001 From: Dario Gjorgjevski Date: Thu, 13 Aug 2015 01:56:37 +0200 Subject: [PATCH 2/8] abstract out reading full output of pipes --- helpers.lua | 15 ++++++++++++++- widgets/alsa.lua | 6 ++---- widgets/alsabar.lua | 6 ++---- widgets/base.lua | 7 +++---- widgets/contrib/kbdlayout.lua | 32 ++++++++++++++++++++++---------- widgets/fs.lua | 4 +--- widgets/imap.lua | 4 +--- widgets/maildir.lua | 8 ++++---- widgets/net.lua | 5 +---- widgets/weather.lua | 7 ++++--- 10 files changed, 54 insertions(+), 40 deletions(-) diff --git a/helpers.lua b/helpers.lua index dbee617..4e90e16 100644 --- a/helpers.lua +++ b/helpers.lua @@ -10,7 +10,8 @@ local debug = require("debug") local capi = { timer = timer } local io = { open = io.open, - lines = io.lines } + lines = io.lines, + popen = io.popen } local rawget = rawget -- Lain helper functions for internal use @@ -86,6 +87,18 @@ end -- }}} +-- {{{ Pipe operations + +-- read the full output of a pipe (command) +function helpers.read_pipe(cmd) + local f = assert(io.popen(cmd)) + local output = f:read("*all") + f:close() + return output +end + +-- }}} + -- {{{ A map utility helpers.map_table = {} diff --git a/widgets/alsa.lua b/widgets/alsa.lua index 91bf488..85d5311 100644 --- a/widgets/alsa.lua +++ b/widgets/alsa.lua @@ -8,10 +8,10 @@ --]] local newtimer = require("lain.helpers").newtimer +local read_pipe = require("lain.helpers").read_pipe local wibox = require("wibox") -local io = { popen = io.popen } local string = { match = string.match, format = string.format } @@ -32,9 +32,7 @@ local function worker(args) alsa.widget = wibox.widget.textbox('') function alsa.update() - local f = assert(io.popen(string.format("%s get %s", alsa.cmd, alsa.channel))) - local mixer = f:read("*all") - f:close() + local mixer = read_pipe(string.format("%s get %s", alsa.cmd, alsa.channel)) volume_now = {} diff --git a/widgets/alsabar.lua b/widgets/alsabar.lua index f504cc0..c7498d2 100644 --- a/widgets/alsabar.lua +++ b/widgets/alsabar.lua @@ -8,12 +8,12 @@ --]] local newtimer = require("lain.helpers").newtimer +local read_pipe = require("lain.helpers").read_pipe local awful = require("awful") local beautiful = require("beautiful") local naughty = require("naughty") -local io = { popen = io.popen } local math = { modf = math.modf } local mouse = mouse local string = { format = string.format, @@ -122,9 +122,7 @@ local function worker(args) function alsabar.update() -- Get mixer control contents - local f = assert(io.popen(string.format("%s get %s", alsabar.cmd, alsabar.channel))) - local mixer = f:read("*all") - f:close() + local mixer = read_pipe(string.format("%s get %s", alsabar.cmd, alsabar.channel)) -- Capture mixer control state: [5%] ... ... [on] local volu, mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)") diff --git a/widgets/base.lua b/widgets/base.lua index 2a7bf10..2f377f7 100644 --- a/widgets/base.lua +++ b/widgets/base.lua @@ -7,9 +7,10 @@ --]] local newtimer = require("lain.helpers").newtimer +local read_pipe = require("lain.helpers").read_pipe + local wibox = require("wibox") -local io = { popen = io.popen } local setmetatable = setmetatable -- Basic template for custom widgets @@ -25,9 +26,7 @@ local function worker(args) base.widget = wibox.widget.textbox('') function base.update() - local f = assert(io.popen(cmd)) - output = f:read("*all") - f:close() + output = read_pipe(cmd) widget = base.widget settings() end diff --git a/widgets/contrib/kbdlayout.lua b/widgets/contrib/kbdlayout.lua index c5242c1..8d13a74 100644 --- a/widgets/contrib/kbdlayout.lua +++ b/widgets/contrib/kbdlayout.lua @@ -1,9 +1,17 @@ +--[[ + + Licensed under GNU General Public License v2 + * (c) 2015, Dario Gjorgjevski + +--]] + local newtimer = require("lain.helpers").newtimer +local read_pipe = require("lain.helpers").read_pipe + local wibox = require("wibox") local string = { match = string.match } -local io = { popen = io.popen } local setmetatable = setmetatable @@ -11,29 +19,33 @@ local function worker (args) local kbdlayout = {} kbdlayout.widget = wibox.widget.textbox('') - local settings = args.settings or function () end - local layouts = args.layouts - local idx = 1 + local layouts = args.layouts + local settings = args.settings or function () end + local add_us_secondary = args.add_us_secondary or true + local idx = 1 local function run_settings (layout, variant) widget = kbdlayout.widget - kbdlayout_now = { layout=layout, variant=variant } + kbdlayout_now = { layout=string.match(layout, "[^,]+"), -- Make sure to match the primary layout only. + variant=variant } settings() end function kbdlayout.update () - local file = assert(io.popen('setxkbmap -query')) - local status = file:read('*all') - file:close() + local status = read_pipe('setxkbmap -query') - run_settings(string.match(status, "layout:%s*([^\n]*)%s*"), - string.match(status, "variant:%s*([^\n]*)%s*")) + run_settings(string.match(status, "layout:%s*([^\n]*)"), + string.match(status, "variant:%s*([^\n]*)")) end function kbdlayout.set (i) idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed. local to_execute = 'setxkbmap ' .. layouts[idx].layout + if add_us_secondary then + to_execute = to_execute .. ",us" + end + if layouts[idx].variant then to_execute = to_execute .. ' ' .. layouts[idx].variant end diff --git a/widgets/fs.lua b/widgets/fs.lua index c3a9e18..a01d3cf 100644 --- a/widgets/fs.lua +++ b/widgets/fs.lua @@ -38,9 +38,7 @@ end function fs:show(t_out) fs:hide() - local f = io.popen(helpers.scripts_dir .. "dfs") - ws = f:read("*all"):gsub("\n*$", "") - f:close() + ws = helpers.read_pipe(helpers.scripts_dir .. "dfs"):gsub("\n*$", "") if fs.followmouse then fs.notification_preset.screen = mouse.screen diff --git a/widgets/imap.lua b/widgets/imap.lua index 62b33d7..ea763df 100644 --- a/widgets/imap.lua +++ b/widgets/imap.lua @@ -43,9 +43,7 @@ local function worker(args) if not is_plain then - local f = io.popen(password) - password = f:read("*all"):gsub("\n", "") - f:close() + password = helpers.read_pipe(password):gsub("\n", "") end imap.widget = wibox.widget.textbox('') diff --git a/widgets/maildir.lua b/widgets/maildir.lua index 315ae34..cb96a30 100644 --- a/widgets/maildir.lua +++ b/widgets/maildir.lua @@ -8,12 +8,12 @@ --]] local newtimer = require("lain.helpers").newtimer +local read_pipe = require("lain.helpers").read_pipe local wibox = require("wibox") local util = require("lain.util") -local io = { popen = io.popen } local os = { getenv = os.getenv } local pairs = pairs local string = { len = string.len, @@ -50,10 +50,9 @@ local function worker(args) -- match files that begin with a dot. -- Afterwards the length of this string is the number of -- new mails in that box. - local np = io.popen("find " .. line .. + local mailstring = read_pipe("find " .. line .. "/new -mindepth 1 -type f " .. "-not -name '.*' -printf a") - local mailstring = np:read("*all") -- Strip off leading mailpath. local box = string.match(line, mailpath .. "/*([^/]+)") @@ -65,10 +64,11 @@ local function worker(args) end until line == nil + p:close() table.sort(boxes) newmail = "no mail" - --Count the total number of mails irrespective of where it was found + -- Count the total number of mails irrespective of where it was found total = 0 for box, number in pairs(boxes) diff --git a/widgets/net.lua b/widgets/net.lua index d859d91..a578ae4 100644 --- a/widgets/net.lua +++ b/widgets/net.lua @@ -13,7 +13,6 @@ local notify_fg = require("beautiful").fg_focus local naughty = require("naughty") local wibox = require("wibox") -local io = { popen = io.popen } local string = { format = string.format, gsub = string.gsub, match = string.match } @@ -28,9 +27,7 @@ local net = { } function net.get_device() - f = io.popen("ip link show | cut -d' ' -f2,9") - ws = f:read("*all") - f:close() + local ws = helpers.read_pipe("ip link show | cut -d' ' -f2,9") ws = ws:match("%w+: UP") or ws:match("ppp%w+: UNKNOWN") if ws ~= nil then return ws:match("(%w+):") diff --git a/widgets/weather.lua b/widgets/weather.lua index fb37a52..8a0d751 100644 --- a/widgets/weather.lua +++ b/widgets/weather.lua @@ -7,9 +7,12 @@ --]] local newtimer = require("lain.helpers").newtimer +local read_pipe = require("lain.helpers").read_pipe + local async = require("lain.asyncshell") local json = require("lain.util").dkjson local lain_icons = require("lain.helpers").icons_dir + local naughty = require("naughty") local wibox = require("wibox") @@ -83,9 +86,7 @@ local function worker(args) if not err and weather_now ~= nil and tonumber(weather_now["cod"]) == 200 then weather.notification_text = '' for i = 1, weather_now["cnt"] do - local f = assert(io.popen(string.format(date_cmd, weather_now["list"][i]["dt"]))) - day = string.gsub(f:read("*all"), "\n", "") - f:close() + day = string.gsub(read_pipe(string.format(date_cmd, weather_now["list"][i]["dt"])), "\n", "") tmin = math.floor(weather_now["list"][i]["temp"]["min"]) tmax = math.floor(weather_now["list"][i]["temp"]["max"]) From 9223ddfb10e77e932d3e462f6c734528e665290a Mon Sep 17 00:00:00 2001 From: Dario Gjorgjevski Date: Thu, 13 Aug 2015 02:05:49 +0200 Subject: [PATCH 3/8] add mouse bindings to kbdlayout --- widgets/contrib/kbdlayout.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/widgets/contrib/kbdlayout.lua b/widgets/contrib/kbdlayout.lua index 8d13a74..1c24647 100644 --- a/widgets/contrib/kbdlayout.lua +++ b/widgets/contrib/kbdlayout.lua @@ -10,6 +10,7 @@ local newtimer = require("lain.helpers").newtimer local read_pipe = require("lain.helpers").read_pipe local wibox = require("wibox") +local awful = require("awful") local string = { match = string.match } @@ -22,7 +23,14 @@ local function worker (args) local layouts = args.layouts local settings = args.settings or function () end local add_us_secondary = args.add_us_secondary or true + local timeout = args.timeout or 5 + local idx = 1 + + -- Mouse bindings + kbdlayout.widget:buttons(awful.util.table.join( + awful.button({ }, 1, function () kbdlayout.next() end), + awful.button({ }, 3, function () kbdlayout.prev() end))) local function run_settings (layout, variant) widget = kbdlayout.widget @@ -63,7 +71,7 @@ local function worker (args) kbdlayout.set(idx - 1) end - newtimer("kbdlayout", args.timeout or 10, kbdlayout.update) + newtimer("kbdlayout", timeout, kbdlayout.update) return setmetatable(kbdlayout, { __index = kbdlayout.widget }) end From edac7419fa0f8c849b055a08cc1a58e61c8980d3 Mon Sep 17 00:00:00 2001 From: Dario Gjorgjevski Date: Thu, 13 Aug 2015 02:11:24 +0200 Subject: [PATCH 4/8] add 'local' modifier as appropriate --- widgets/fs.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/widgets/fs.lua b/widgets/fs.lua index a01d3cf..a1d5d95 100644 --- a/widgets/fs.lua +++ b/widgets/fs.lua @@ -38,7 +38,7 @@ end function fs:show(t_out) fs:hide() - ws = helpers.read_pipe(helpers.scripts_dir .. "dfs"):gsub("\n*$", "") + local ws = helpers.read_pipe(helpers.scripts_dir .. "dfs"):gsub("\n*$", "") if fs.followmouse then fs.notification_preset.screen = mouse.screen From 9358f8ac54a20f2351e4e4b7c45735f2388c3c42 Mon Sep 17 00:00:00 2001 From: Dario Gjorgjevski Date: Thu, 13 Aug 2015 02:16:34 +0200 Subject: [PATCH 5/8] bring back mistaken deletion --- widgets/maildir.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/widgets/maildir.lua b/widgets/maildir.lua index cb96a30..eed6138 100644 --- a/widgets/maildir.lua +++ b/widgets/maildir.lua @@ -14,6 +14,7 @@ local wibox = require("wibox") local util = require("lain.util") +local io = { popen = io.popen } local os = { getenv = os.getenv } local pairs = pairs local string = { len = string.len, From 390203892a05a2bd9e08cb37621637263e4ac8c1 Mon Sep 17 00:00:00 2001 From: Dario Gjorgjevski Date: Fri, 14 Aug 2015 00:31:05 +0200 Subject: [PATCH 6/8] update wiki --- wiki | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wiki b/wiki index cec2ebd..268a2cd 160000 --- a/wiki +++ b/wiki @@ -1 +1 @@ -Subproject commit cec2ebd96e91ef3c2a61e6ea9d41f5c44931ee58 +Subproject commit 268a2cd3867d890616d6326472372e7168276927 From 75c3b81654b5159dbad6123e51838d8c3a34e3bd Mon Sep 17 00:00:00 2001 From: Dario Gjorgjevski Date: Sun, 16 Aug 2015 05:36:09 +0200 Subject: [PATCH 7/8] fix trailing whitespace and mixed indent --- widgets/contrib/kbdlayout.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/widgets/contrib/kbdlayout.lua b/widgets/contrib/kbdlayout.lua index 1c24647..8b15475 100644 --- a/widgets/contrib/kbdlayout.lua +++ b/widgets/contrib/kbdlayout.lua @@ -1,7 +1,7 @@ --[[ - Licensed under GNU General Public License v2 + Licensed under GNU General Public License v2 * (c) 2015, Dario Gjorgjevski --]] @@ -24,26 +24,26 @@ local function worker (args) local settings = args.settings or function () end local add_us_secondary = args.add_us_secondary or true local timeout = args.timeout or 5 - + local idx = 1 -- Mouse bindings kbdlayout.widget:buttons(awful.util.table.join( - awful.button({ }, 1, function () kbdlayout.next() end), - awful.button({ }, 3, function () kbdlayout.prev() end))) - + awful.button({ }, 1, function () kbdlayout.next() end), + awful.button({ }, 3, function () kbdlayout.prev() end))) + local function run_settings (layout, variant) widget = kbdlayout.widget kbdlayout_now = { layout=string.match(layout, "[^,]+"), -- Make sure to match the primary layout only. variant=variant } settings() end - + function kbdlayout.update () local status = read_pipe('setxkbmap -query') run_settings(string.match(status, "layout:%s*([^\n]*)"), - string.match(status, "variant:%s*([^\n]*)")) + string.match(status, "variant:%s*([^\n]*)")) end function kbdlayout.set (i) @@ -51,15 +51,15 @@ local function worker (args) local to_execute = 'setxkbmap ' .. layouts[idx].layout if add_us_secondary then - to_execute = to_execute .. ",us" + to_execute = to_execute .. ",us" end if layouts[idx].variant then - to_execute = to_execute .. ' ' .. layouts[idx].variant + to_execute = to_execute .. ' ' .. layouts[idx].variant end if os.execute(to_execute) then - run_settings(layouts[idx].layout, layouts[idx].variant) + run_settings(layouts[idx].layout, layouts[idx].variant) end end From 5f643320f36d201df94ee56303b0504afd695697 Mon Sep 17 00:00:00 2001 From: Dario Gjorgjevski Date: Sun, 16 Aug 2015 05:42:18 +0200 Subject: [PATCH 8/8] do not add `us' as secondary if already present --- widgets/contrib/kbdlayout.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/widgets/contrib/kbdlayout.lua b/widgets/contrib/kbdlayout.lua index 8b15475..f9342a0 100644 --- a/widgets/contrib/kbdlayout.lua +++ b/widgets/contrib/kbdlayout.lua @@ -50,7 +50,7 @@ local function worker (args) idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed. local to_execute = 'setxkbmap ' .. layouts[idx].layout - if add_us_secondary then + if add_us_secondary and not string.match(layouts[idx].layout, ",?us,?") then to_execute = to_execute .. ",us" end