summaryrefslogtreecommitdiffstatshomepage
path: root/plugins
diff options
context:
space:
mode:
author cracyc <cracyc@users.noreply.github.com>2016-11-05 17:45:35 -0500
committer cracyc <cracyc@users.noreply.github.com>2016-11-06 13:41:43 -0600
commit2de04414ed0fdff027eb99c83faca2074a944bc6 (patch)
tree4e3343e841788e9e16faf639127db13e94471643 /plugins
parent6fd9586a8966196857c8c36401215430b695ee93 (diff)
plugins/console: add lua console [Carl]
Diffstat (limited to 'plugins')
-rw-r--r--plugins/console/init.lua84
-rw-r--r--plugins/console/plugin.json10
2 files changed, 94 insertions, 0 deletions
diff --git a/plugins/console/init.lua b/plugins/console/init.lua
new file mode 100644
index 00000000000..fea0f930d42
--- /dev/null
+++ b/plugins/console/init.lua
@@ -0,0 +1,84 @@
+-- license:BSD-3-Clause
+-- copyright-holders:Carl
+local exports = {}
+exports.name = "console"
+exports.version = "0.0.1"
+exports.description = "Console plugin"
+exports.license = "The BSD 3-Clause License"
+exports.author = { name = "Carl" }
+
+local console = exports
+
+function console.startplugin()
+ local conth = emu.thread()
+ local started = false
+ local ln = require("linenoise")
+ print(" _/ _/ _/_/ _/ _/ _/_/_/_/");
+ print(" _/_/ _/_/ _/ _/ _/_/ _/_/ _/ ");
+ print(" _/ _/ _/ _/_/_/_/ _/ _/ _/ _/_/_/ ");
+ print(" _/ _/ _/ _/ _/ _/ _/ ");
+ print("_/ _/ _/ _/ _/ _/ _/_/_/_/ \n");
+ print(emu.app_name() .. " " .. emu.app_version(), "\nCopyright (C) Nicola Salmoria and the MAME team\n");
+ print(_VERSION, "\nCopyright (C) Lua.org, PUC-Rio\n");
+ -- linenoise isn't thread safe but that means history can handled here
+ -- that also means that bad things will happen if anything outside lua tries to use it
+ -- especially the completion callback
+ ln.historysetmaxlen(10)
+ local scr = "local ln = require('linenoise')\n"
+ scr = scr .. "ln.setcompletion(function(c, str) status = str\n"
+ scr = scr .. " yield()\n" -- coroutines can't yield in the middle of a callback so this is a real thread
+ scr = scr .. " status:gsub('[^,]*', function(s) if s ~= '' then ln.addcompletion(c, s) end end)\n"
+ scr = scr .. "end)\n"
+ scr = scr .. "return ln.linenoise('\x1b[1;36m[MAME]\x1b[0m> ')"
+
+ function get_completions(str)
+ local function is_pair_iterable(t)
+ local mt = getmetatable(t)
+ return type(t) == 'table' or (mt and mt.__pairs)
+ end
+ local comps = ","
+ local table, last = str:match("([(]?[%w.:()]-)[:.]?([%w]*)$")
+ if last == "" then
+ return comps
+ end
+ local err
+ err, tablef = pcall(load("return " .. table))
+ if (not err) or (not tablef) then
+ return comps
+ end
+ if is_pair_iterable(tablef) then
+ for k, v in pairs(tablef) do
+ if k:match("^" .. last) then
+ comps = comps .. "," .. table
+ end
+ end
+ end
+ return comps
+ end
+
+ emu.register_periodic(function()
+ if conth.yield then
+ conth:continue(get_completions(conth.result))
+ return
+ elseif conth.busy then
+ return
+ elseif started then
+ local cmd = conth.result
+ ln.historyadd(cmd)
+ local func, err = load(cmd)
+ if not func then
+ print("error: ", err)
+ else
+ local status
+ status, err = pcall(func)
+ if not status then
+ print("error: ", err)
+ end
+ end
+ end
+ conth:start(scr)
+ started = true
+ end)
+end
+
+return exports
diff --git a/plugins/console/plugin.json b/plugins/console/plugin.json
new file mode 100644
index 00000000000..182bb887785
--- /dev/null
+++ b/plugins/console/plugin.json
@@ -0,0 +1,10 @@
+{
+ "plugin": {
+ "name": "console",
+ "description": "Console plugin",
+ "version": "0.0.1",
+ "author": "Carl",
+ "type": "plugin",
+ "start": "false"
+ }
+}