mirror of
https://github.com/holub/mame
synced 2025-07-04 09:28:51 +03:00
plugins/console: much better completions (nw)
This commit is contained in:
parent
769810638a
commit
3b512b0fd8
@ -25,25 +25,56 @@ function console.startplugin()
|
|||||||
-- that also means that bad things will happen if anything outside lua tries to use it
|
-- that also means that bad things will happen if anything outside lua tries to use it
|
||||||
-- especially the completion callback
|
-- especially the completion callback
|
||||||
ln.historysetmaxlen(10)
|
ln.historysetmaxlen(10)
|
||||||
local scr = "local ln = require('linenoise')\n"
|
local scr = [[
|
||||||
scr = scr .. "ln.setcompletion(function(c, str) status = str\n"
|
local ln = require('linenoise')
|
||||||
scr = scr .. " yield()\n" -- coroutines can't yield in the middle of a callback so this is a real thread
|
ln.setcompletion(function(c, str)
|
||||||
scr = scr .. " status:gsub('[^,]*', function(s) if s ~= '' then ln.addcompletion(c, s) end end)\n"
|
status = str
|
||||||
scr = scr .. "end)\n"
|
yield()
|
||||||
scr = scr .. "return ln.linenoise('\x1b[1;36m[MAME]\x1b[0m> ')"
|
status:gsub('[^,]*', function(s) if s ~= '' then ln.addcompletion(c, s) end end)
|
||||||
|
end)
|
||||||
|
return ln.linenoise('\x1b[1;36m[MAME]\x1b[0m> ')
|
||||||
|
]]
|
||||||
|
|
||||||
|
local function find_unmatch(str, openpar, pair)
|
||||||
|
local done = false
|
||||||
|
if not str:match(openpar) then
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
local tmp = str:gsub(pair, "")
|
||||||
|
if not tmp:match(openpar) then
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
repeat
|
||||||
|
str = str:gsub(".-" .. openpar .. "(.*)", function (s)
|
||||||
|
tmp = s:gsub(pair, "")
|
||||||
|
if not tmp:match(openpar) then
|
||||||
|
done = true
|
||||||
|
end
|
||||||
|
return s
|
||||||
|
end)
|
||||||
|
until done or str == ""
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
|
||||||
local function get_completions(str)
|
local function get_completions(str)
|
||||||
local comps = ","
|
local comps = ","
|
||||||
local table = str:match("([(]?[%w.:()]-)[:.][%w_]*$")
|
local rest, dot, last = str:match("(.-)([.:]?)([^.:]*)$")
|
||||||
local rest, last = str:match("(.-[:.]?)([%w_]*)$")
|
str = find_unmatch(str, "%(", "%b()")
|
||||||
|
str = find_unmatch(str, "%[", "%b[]")
|
||||||
|
local table = str:match("([%w_%.:%(%)%[%]]-)[:.][%w_]*$")
|
||||||
local err
|
local err
|
||||||
if table == "" or not table then
|
if rest == "" or not table then
|
||||||
table = "_G"
|
if dot == "" then
|
||||||
|
table = "_G"
|
||||||
|
else
|
||||||
|
return comps
|
||||||
|
end
|
||||||
end
|
end
|
||||||
err, tablef = pcall(load("return " .. table))
|
err, tablef = pcall(load("return " .. table))
|
||||||
if (not err) or (not tablef) then
|
if (not err) or (not tablef) then
|
||||||
return comps
|
return comps
|
||||||
end
|
end
|
||||||
|
rest = rest .. dot
|
||||||
if type(tablef) == 'table' then
|
if type(tablef) == 'table' then
|
||||||
for k, v in pairs(tablef) do
|
for k, v in pairs(tablef) do
|
||||||
if k:match("^" .. last) then
|
if k:match("^" .. last) then
|
||||||
|
Loading…
Reference in New Issue
Block a user