Last active
August 29, 2015 14:13
-
-
Save blueyed/3d6995d4455ebd2d930a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- textbox_cmd: a textbox that executes a command regularly. {{{ | |
-- Execute command and return its output. | |
function execute_command(command) | |
local fh = io.popen(command) | |
local r = {} | |
for i in fh:lines() do | |
r[#r+1] = i | |
end | |
io.close(fh) | |
return r | |
end | |
local textbox_cmd = { mt = {} } | |
--- Create a textbox_cmd widget. | |
-- If the cmd output changes, a note gets displayed. | |
-- @param cmd The cmd to run. | |
-- @param markup The markup string to be used with string.format (%s). | |
-- @param timeout Timeout to run the cmd again. Default is 60 seconds. | |
-- @return A textbox widget. | |
function textbox_cmd.new(cmd, markup, timeout, report_changes) | |
local markup = markup or '%s' | |
local timeout = timeout or 60 | |
local report_changes = report_changes or false | |
local w = wibox.widget.textbox() | |
local tooltip = awful.tooltip({ | |
objects = { w }, | |
}) | |
local last_result_str | |
local timer = capi.timer { timeout = timeout } | |
timer:connect_signal("timeout", function() | |
local result = execute_command(cmd) | |
local result_header = result[1] | |
local result_str = table.concat(result, "\n") | |
if report_changes then | |
if last_result_str and last_result_str ~= result_str then | |
-- awful.util.spawn("notify-send -u normal -t0 'cmd result changed' "..cmd..': from \n' .. last_result .. '\n to \n' .. result, {timeout=0}) | |
bnote("cmd result changed: " .. cmd .. ": from \n" | |
.. last_result_str .. "\n to \n" .. result_str, {timeout=0}) | |
end | |
last_result_str = result_str | |
end | |
local markup_header = string.format(markup, result_header and awful.util.escape(result_header) or "") | |
w:set_markup(markup_header) | |
-- Attach a tooltip, if there's additional information (after line 1). | |
if #result > 1 then | |
local markup_tooltip = string.format(markup, awful.util.escape(result_str)) | |
tooltip:set_markup(markup_tooltip) | |
end | |
end) | |
timer:start() | |
timer:emit_signal("timeout") | |
return w | |
end | |
function textbox_cmd.mt:__call(...) | |
return textbox_cmd.new(...) | |
end | |
textbox_cmd = setmetatable(textbox_cmd, textbox_cmd.mt) | |
-- }}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment