prompt: Add a Vi like prompt parser

Otherwise hooks that modify the command are not documented well
enough.
This commit is contained in:
Emmanuel Lepage Vallee 2016-12-30 01:30:13 -05:00
parent 4934697817
commit f6cce940a5
2 changed files with 62 additions and 0 deletions

View File

@ -65,6 +65,13 @@
--
-- @DOC_wibox_awidget_prompt_hooks_EXAMPLE@
--
-- *[Example two] Modifying the command (+ vi like input)*:
--
-- The hook system also allows to modify the command before interpreting it in
-- the `exe_callback`.
--
-- @DOC_wibox_awidget_prompt_vilike_EXAMPLE@
--
--
-- @author Julien Danjou <julien@danjou.info>
-- @copyright 2008 Julien Danjou

View File

@ -0,0 +1,55 @@
local parent = ... --DOC_NO_USAGE --DOC_HIDE
local wibox = require( "wibox" ) --DOC_HIDE
local awful = { prompt = require("awful.prompt"),--DOC_HIDE
util = require("awful.util")}--DOC_HIDE
local beautiful = require( "beautiful" ) --DOC_HIDE
local terminal = "xterm" --DOC_HIDE
local atextbox = wibox.widget.textbox()
-- Store a list of verbs characters in a hash
local verbs = {
-- Spawn in a terminal
t = function(adjs, count, cmd) return {terminal, "-e", cmd} end, --luacheck: no unused args
-- Spawn with a shell
s = function(adjs, count, cmd) return {awful.util.shell, '-c', cmd} end, --luacheck: no unused args
}
local function vi_parse(action, command)
local req, ret = {count={}, adjectives={}}
-- Quite dumb, don't do something like <num>+<adj>+<num>+<verb>
for char in action:gmatch('(.)') do
if tonumber(char) then table.insert(req.count, char)
elseif verbs[char] then req.verb = char
else table.insert(ret.adjectives, char) end
if req.verb then
req.count = tonumber(table.concat(req.count)) or 1
ret = ret or verbs[req.verb](req.adjectives, req.count, command)
req = {count={}, adjectives={}}
end
end
return ret
end
awful.prompt.run {
prompt = '<b>Run: </b>',
text = ':t htop', --DOC_HIDE
hooks = {
{{},'Return', function(cmd)
if (not cmd) or cmd:sub(1,1) ~= ':' then return cmd end
local act, cmd2 = cmd:gmatch(':([a-zA-Z1-9]+)[ ]+(.*)')()
if not act then return cmd end
return vi_parse(act, cmd2)
end},
},
textbox = atextbox,
history_path = awful.util.getdir('cache') .. '/history',
exe_callback = function(cmd) awful.spawn(cmd) end
}
parent:add( wibox.widget { --DOC_HIDE
atextbox, --DOC_HIDE
bg = beautiful.bg_normal, --DOC_HIDE
widget = wibox.container.background --DOC_HIDE
}) --DOC_HIDE