abstract out reading full output of pipes

This commit is contained in:
Dario Gjorgjevski 2015-08-13 01:56:37 +02:00
parent 945f220e02
commit 6603c641c6
10 changed files with 54 additions and 40 deletions

View file

@ -10,7 +10,8 @@ local debug = require("debug")
local capi = { timer = timer } local capi = { timer = timer }
local io = { open = io.open, local io = { open = io.open,
lines = io.lines } lines = io.lines,
popen = io.popen }
local rawget = rawget local rawget = rawget
-- Lain helper functions for internal use -- 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 -- {{{ A map utility
helpers.map_table = {} helpers.map_table = {}

View file

@ -8,10 +8,10 @@
--]] --]]
local newtimer = require("lain.helpers").newtimer local newtimer = require("lain.helpers").newtimer
local read_pipe = require("lain.helpers").read_pipe
local wibox = require("wibox") local wibox = require("wibox")
local io = { popen = io.popen }
local string = { match = string.match, local string = { match = string.match,
format = string.format } format = string.format }
@ -32,9 +32,7 @@ local function worker(args)
alsa.widget = wibox.widget.textbox('') alsa.widget = wibox.widget.textbox('')
function alsa.update() function alsa.update()
local f = assert(io.popen(string.format("%s get %s", alsa.cmd, alsa.channel))) local mixer = read_pipe(string.format("%s get %s", alsa.cmd, alsa.channel))
local mixer = f:read("*all")
f:close()
volume_now = {} volume_now = {}

View file

@ -8,12 +8,12 @@
--]] --]]
local newtimer = require("lain.helpers").newtimer local newtimer = require("lain.helpers").newtimer
local read_pipe = require("lain.helpers").read_pipe
local awful = require("awful") local awful = require("awful")
local beautiful = require("beautiful") local beautiful = require("beautiful")
local naughty = require("naughty") local naughty = require("naughty")
local io = { popen = io.popen }
local math = { modf = math.modf } local math = { modf = math.modf }
local mouse = mouse local mouse = mouse
local string = { format = string.format, local string = { format = string.format,
@ -122,9 +122,7 @@ local function worker(args)
function alsabar.update() function alsabar.update()
-- Get mixer control contents -- Get mixer control contents
local f = assert(io.popen(string.format("%s get %s", alsabar.cmd, alsabar.channel))) local mixer = read_pipe(string.format("%s get %s", alsabar.cmd, alsabar.channel))
local mixer = f:read("*all")
f:close()
-- Capture mixer control state: [5%] ... ... [on] -- Capture mixer control state: [5%] ... ... [on]
local volu, mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)") local volu, mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")

View file

@ -7,9 +7,10 @@
--]] --]]
local newtimer = require("lain.helpers").newtimer local newtimer = require("lain.helpers").newtimer
local read_pipe = require("lain.helpers").read_pipe
local wibox = require("wibox") local wibox = require("wibox")
local io = { popen = io.popen }
local setmetatable = setmetatable local setmetatable = setmetatable
-- Basic template for custom widgets -- Basic template for custom widgets
@ -25,9 +26,7 @@ local function worker(args)
base.widget = wibox.widget.textbox('') base.widget = wibox.widget.textbox('')
function base.update() function base.update()
local f = assert(io.popen(cmd)) output = read_pipe(cmd)
output = f:read("*all")
f:close()
widget = base.widget widget = base.widget
settings() settings()
end end

View file

@ -1,9 +1,17 @@
--[[
Licensed under GNU General Public License v2
* (c) 2015, Dario Gjorgjevski
--]]
local newtimer = require("lain.helpers").newtimer local newtimer = require("lain.helpers").newtimer
local read_pipe = require("lain.helpers").read_pipe
local wibox = require("wibox") local wibox = require("wibox")
local string = { match = string.match } local string = { match = string.match }
local io = { popen = io.popen }
local setmetatable = setmetatable local setmetatable = setmetatable
@ -11,29 +19,33 @@ local function worker (args)
local kbdlayout = {} local kbdlayout = {}
kbdlayout.widget = wibox.widget.textbox('') kbdlayout.widget = wibox.widget.textbox('')
local settings = args.settings or function () end
local layouts = args.layouts 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 idx = 1
local function run_settings (layout, variant) local function run_settings (layout, variant)
widget = kbdlayout.widget 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() settings()
end end
function kbdlayout.update () function kbdlayout.update ()
local file = assert(io.popen('setxkbmap -query')) local status = read_pipe('setxkbmap -query')
local status = file:read('*all')
file:close()
run_settings(string.match(status, "layout:%s*([^\n]*)%s*"), run_settings(string.match(status, "layout:%s*([^\n]*)"),
string.match(status, "variant:%s*([^\n]*)%s*")) string.match(status, "variant:%s*([^\n]*)"))
end end
function kbdlayout.set (i) function kbdlayout.set (i)
idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed. idx = ((i - 1) % #layouts) + 1 -- Make sure to wrap around as needed.
local to_execute = 'setxkbmap ' .. layouts[idx].layout local to_execute = 'setxkbmap ' .. layouts[idx].layout
if add_us_secondary then
to_execute = to_execute .. ",us"
end
if layouts[idx].variant then if layouts[idx].variant then
to_execute = to_execute .. ' ' .. layouts[idx].variant to_execute = to_execute .. ' ' .. layouts[idx].variant
end end

View file

@ -38,9 +38,7 @@ end
function fs:show(t_out) function fs:show(t_out)
fs:hide() fs:hide()
local f = io.popen(helpers.scripts_dir .. "dfs") ws = helpers.read_pipe(helpers.scripts_dir .. "dfs"):gsub("\n*$", "")
ws = f:read("*all"):gsub("\n*$", "")
f:close()
if fs.followmouse then if fs.followmouse then
fs.notification_preset.screen = mouse.screen fs.notification_preset.screen = mouse.screen

View file

@ -43,9 +43,7 @@ local function worker(args)
if not is_plain if not is_plain
then then
local f = io.popen(password) password = helpers.read_pipe(password):gsub("\n", "")
password = f:read("*all"):gsub("\n", "")
f:close()
end end
imap.widget = wibox.widget.textbox('') imap.widget = wibox.widget.textbox('')

View file

@ -8,12 +8,12 @@
--]] --]]
local newtimer = require("lain.helpers").newtimer local newtimer = require("lain.helpers").newtimer
local read_pipe = require("lain.helpers").read_pipe
local wibox = require("wibox") local wibox = require("wibox")
local util = require("lain.util") local util = require("lain.util")
local io = { popen = io.popen }
local os = { getenv = os.getenv } local os = { getenv = os.getenv }
local pairs = pairs local pairs = pairs
local string = { len = string.len, local string = { len = string.len,
@ -50,10 +50,9 @@ local function worker(args)
-- match files that begin with a dot. -- match files that begin with a dot.
-- Afterwards the length of this string is the number of -- Afterwards the length of this string is the number of
-- new mails in that box. -- new mails in that box.
local np = io.popen("find " .. line .. local mailstring = read_pipe("find " .. line ..
"/new -mindepth 1 -type f " .. "/new -mindepth 1 -type f " ..
"-not -name '.*' -printf a") "-not -name '.*' -printf a")
local mailstring = np:read("*all")
-- Strip off leading mailpath. -- Strip off leading mailpath.
local box = string.match(line, mailpath .. "/*([^/]+)") local box = string.match(line, mailpath .. "/*([^/]+)")
@ -65,10 +64,11 @@ local function worker(args)
end end
until line == nil until line == nil
p:close()
table.sort(boxes) table.sort(boxes)
newmail = "no mail" 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 total = 0
for box, number in pairs(boxes) for box, number in pairs(boxes)

View file

@ -13,7 +13,6 @@ local notify_fg = require("beautiful").fg_focus
local naughty = require("naughty") local naughty = require("naughty")
local wibox = require("wibox") local wibox = require("wibox")
local io = { popen = io.popen }
local string = { format = string.format, local string = { format = string.format,
gsub = string.gsub, gsub = string.gsub,
match = string.match } match = string.match }
@ -28,9 +27,7 @@ local net = {
} }
function net.get_device() function net.get_device()
f = io.popen("ip link show | cut -d' ' -f2,9") local ws = helpers.read_pipe("ip link show | cut -d' ' -f2,9")
ws = f:read("*all")
f:close()
ws = ws:match("%w+: UP") or ws:match("ppp%w+: UNKNOWN") ws = ws:match("%w+: UP") or ws:match("ppp%w+: UNKNOWN")
if ws ~= nil then if ws ~= nil then
return ws:match("(%w+):") return ws:match("(%w+):")

View file

@ -7,9 +7,12 @@
--]] --]]
local newtimer = require("lain.helpers").newtimer local newtimer = require("lain.helpers").newtimer
local read_pipe = require("lain.helpers").read_pipe
local async = require("lain.asyncshell") local async = require("lain.asyncshell")
local json = require("lain.util").dkjson local json = require("lain.util").dkjson
local lain_icons = require("lain.helpers").icons_dir local lain_icons = require("lain.helpers").icons_dir
local naughty = require("naughty") local naughty = require("naughty")
local wibox = require("wibox") 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 if not err and weather_now ~= nil and tonumber(weather_now["cod"]) == 200 then
weather.notification_text = '' weather.notification_text = ''
for i = 1, weather_now["cnt"] do for i = 1, weather_now["cnt"] do
local f = assert(io.popen(string.format(date_cmd, weather_now["list"][i]["dt"]))) day = string.gsub(read_pipe(string.format(date_cmd, weather_now["list"][i]["dt"])), "\n", "")
day = string.gsub(f:read("*all"), "\n", "")
f:close()
tmin = math.floor(weather_now["list"][i]["temp"]["min"]) tmin = math.floor(weather_now["list"][i]["temp"]["min"])
tmax = math.floor(weather_now["list"][i]["temp"]["max"]) tmax = math.floor(weather_now["list"][i]["temp"]["max"])