From f6cce940a598fdbe8c5e9f1ed9b4b240be633b58 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Fri, 30 Dec 2016 01:30:13 -0500 Subject: [PATCH] prompt: Add a Vi like prompt parser Otherwise hooks that modify the command are not documented well enough. --- lib/awful/prompt.lua | 7 +++ .../examples/wibox/awidget/prompt/vilike.lua | 55 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 tests/examples/wibox/awidget/prompt/vilike.lua diff --git a/lib/awful/prompt.lua b/lib/awful/prompt.lua index c3d0f466..3dc5201d 100644 --- a/lib/awful/prompt.lua +++ b/lib/awful/prompt.lua @@ -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 diff --git a/tests/examples/wibox/awidget/prompt/vilike.lua b/tests/examples/wibox/awidget/prompt/vilike.lua new file mode 100644 index 00000000..01594c5a --- /dev/null +++ b/tests/examples/wibox/awidget/prompt/vilike.lua @@ -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 +++ + 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 = 'Run: ', + 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