Merge pull request #483 from blueyed/Elv13-prompt_hook_v2

Prompt hook v2
This commit is contained in:
Daniel Hahler 2015-11-08 15:13:49 +01:00
commit aec751f0ea
2 changed files with 79 additions and 11 deletions

View File

@ -224,7 +224,27 @@ end
-- user editing.
--
-- @tparam table args A table with optional arguments: `fg_cursor`, `bg_cursor`,
-- `ul_cursor`, `prompt`, `text`, `selectall`, `font`, `autoexec`.
-- `ul_cursor`, `prompt`, `text`, `selectall`, `font`, `autoexec`, `hooks`.
-- @tparam[opt] table args.hooks The "hooks" argument uses a syntax similar to
-- `awful.key`. It will call a function for the matching modifiers + key.
-- It receives the command (widget text/input) as an argument.
-- If the callback returns a command, this will be passed to the
-- `exe_callback`, otherwise nothing gets executed by default, and the hook
-- needs to handle it.
-- hooks = {
-- -- Apply startup notification properties with Shift-Return.
-- {{"Shift" }, "Return", function(command)
-- mypromptbox[awful.screen.focused()]:spawn_and_handle_error(
-- command, {floating=true})
-- end},
-- -- Override default behavior of "Return": launch commands prefixed
-- -- with ":" in a terminal.
-- {{}, "Return", function(command)
-- if command:sub(1,1) == ":" then
-- return terminal .. ' -e ' .. command:sub(2)
-- end
-- end}
-- }
-- @param textbox The textbox to use for the prompt.
-- @param exe_callback The callback function to call with command as argument
-- when finished.
@ -238,7 +258,8 @@ end
-- @param[opt] changed_callback The callback function to call
-- with command as argument when a command was changed.
-- @param[opt] keypressed_callback The callback function to call
-- with mod table, key and command as arguments when a key was pressed.
-- with mod table, key and command as arguments when a key was pressed.
function prompt.run(args, textbox, exe_callback, completion_callback, history_path, history_max, done_callback, changed_callback, keypressed_callback)
local grabber
local theme = beautiful.get()
@ -253,6 +274,7 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa
local text = args.text or ""
local font = args.font or theme.font
local selectall = args.selectall
local hooks = {}
search_term=nil
@ -262,20 +284,36 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa
local cur_pos = (selectall and 1) or text:wlen() + 1
-- The completion element to use on completion request.
local ncomp = 1
if not textbox or not exe_callback then
if not textbox or not (exe_callback or args.hooks) then
return
end
-- Build the hook map
for k,v in ipairs(args.hooks or {}) do
if #v == 3 then
local mods,key,callback = unpack(v)
if type(callback) == "function" then
hooks[key] = hooks[key] or {}
hooks[key][#hooks[key]+1] = v
else
assert("The hook's 3rd parameter has to be a function.")
end
else
assert("The hook has to have 3 parameters.")
end
end
textbox:set_font(font)
textbox:set_markup(prompt_text_with_cursor{
text = text, text_color = inv_col, cursor_color = cur_col,
cursor_pos = cur_pos, cursor_ul = cur_ul, selectall = selectall,
prompt = prettyprompt })
local exec = function()
local function exec(cb)
textbox:set_markup("")
history_add(history_path, command)
keygrabber.stop(grabber)
exe_callback(command)
cb(command)
if done_callback then done_callback() end
end
@ -319,6 +357,31 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa
end
end
-- User defined cases
if hooks[key] then
for k,v in ipairs(hooks[key]) do
if #modifiers == #v[1] then
local match = true
for k2,v2 in ipairs(v[1]) do
match = match and mod[v2]
end
if match or #modifiers == 0 then
local cb
local ret = v[3](command)
if ret then
command = ret
cb = exe_callback
else
-- No callback.
cb = function() end
end
exec(cb)
return
end
end
end
end
-- Get out cases
if (mod.Control and (key == "c" or key == "g"))
or (not mod.Control and key == "Escape") then
@ -330,7 +393,7 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa
elseif (mod.Control and (key == "j" or key == "m"))
or (not mod.Control and key == "Return")
or (not mod.Control and key == "KP_Enter") then
exec()
exec(exe_callback)
-- We already unregistered ourselves so we don't want to return
-- true, otherwise we may unregister someone else.
return
@ -497,7 +560,7 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa
key = ""
-- execute if only one match found and autoexec flag set
if matches and #matches == 1 and args.autoexec then
exec()
exec(exe_callback)
return
end
else

View File

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