awesomerc/ui/desktop_decoration/bar/widgets/prompt.lua

83 lines
2.0 KiB
Lua

local acompletion = require "awful.completion"
local aspawn = require "awful.spawn"
local gstring = require "gears.string"
local gtable = require "gears.table"
local prompt = require "awful.widget.prompt"
local string = string
local function completion_cb(
self,
command_before_comp,
cur_pos_before_comp,
ncomp
)
return acompletion.generic(
command_before_comp,
cur_pos_before_comp,
ncomp,
self.completion_keywords
)
end
local function exe_cb(self, input)
-- Exit if the input is empty
if not input or #input == 0 then
return
end
-- Trim
input = string.gsub(input, "^%s*(.-)%s*$", "%1")
-- If the input is not a VI command, spawn it
if input:sub(1, 1) ~= ":" then
aspawn(input)
return
end
-- Parse the custom command
local command, parameters = input:gmatch ":([%w-]+)%s*(.*)"()
command = command:sub(1, 1)
parameters = gstring.split(parameters, "%s")
-- Quit if the command doesn't exist
if not self.commands[command] then
print('":' .. command .. '" not reconized as a command')
return
end
self.commands[command].callback(parameters)
end
local my_prompt = { mt = {} }
function my_prompt.new(args)
local prompt_widget = nil
prompt_widget = prompt {
prompt = "<b></b> ", -- needs the 3 spaces
completion_callback = function(...)
return completion_cb(prompt_widget, ...)
end,
exe_callback = function(...)
return exe_cb(prompt_widget, ...)
end,
}
prompt_widget.commands = args.commands
prompt_widget.completion_keywords = gtable.join(
-- TODO: find applications list
gtable.find_keys(args.commands, function()
return true
end, false),
args.completion_keywords or {}
)
return prompt_widget
end
function my_prompt.mt:__call(...)
return my_prompt.new(...)
end
return setmetatable(my_prompt, my_prompt.mt)