Add awful.widget.prompt.spawn_and_handle_error helper

This is meant to avoid boilerplate code when creating prompt hooks.
This commit is contained in:
Daniel Hahler 2015-09-27 16:55:46 +02:00
parent ce2d31cbde
commit b3c7efd5e4

View file

@ -24,15 +24,19 @@ local function run(promptbox)
return prompt.run({ prompt = promptbox.prompt }, return prompt.run({ prompt = promptbox.prompt },
promptbox.widget, promptbox.widget,
function (...) function (...)
local result = spawn(...) promptbox:spawn_and_handle_error(...)
if type(result) == "string" then
promptbox.widget:set_text(result)
end
end, end,
completion.shell, completion.shell,
util.getdir("cache") .. "/history") util.getdir("cache") .. "/history")
end end
local function spawn_and_handle_error(self, ...)
local result = spawn(...)
if type(result) == "string" then
self.widget:set_text(result)
end
end
--- Create a prompt widget which will launch a command. --- Create a prompt widget which will launch a command.
-- --
-- @param args Arguments table. "prompt" is the prompt to use. -- @param args Arguments table. "prompt" is the prompt to use.
@ -45,6 +49,7 @@ function widgetprompt.new(args)
promptbox.widget = widget promptbox.widget = widget
promptbox.widget:set_ellipsize("start") promptbox.widget:set_ellipsize("start")
promptbox.run = run promptbox.run = run
promptbox.spawn_and_handle_error = spawn_and_handle_error
promptbox.prompt = args.prompt or "Run: " promptbox.prompt = args.prompt or "Run: "
return promptbox return promptbox
end end