From 8e9644761a66ce277f1bf33bb5f6ab90375027de Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 24 Dec 2019 16:08:04 +0100 Subject: console: add a basic help command It's not really great. But I think it's good enough to save someone who's lost. Most importantly, it should become obvious _what_ the console expects (input commands), and how to exit it. Would be nice if we could show some documentation, or give a link to documentation, but that's out of scope and/or not easy. I considered implementing the "help" command as builtin command in command.c. On the other hand, this could not show console.lua specific help, so I implemented it in console.lua. The ad-hoc command parsing (that sort-of mirrors mpv input command syntax) for the "help" command is probably the worst part of this. --- player/lua/console.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/player/lua/console.lua b/player/lua/console.lua index 01377e86ef..f14d7f9362 100644 --- a/player/lua/console.lua +++ b/player/lua/console.lua @@ -330,6 +330,47 @@ function maybe_exit() end end +function help_command(param) + local cmdlist = mp.get_property_native('command-list') + local error_style = '{\\1c&H7a77f2&}' + local output = '' + if param == '' then + output = 'Available commands:\n' + for _, cmd in ipairs(cmdlist) do + output = output .. ' ' .. cmd.name + end + output = output .. '\n' + output = output .. 'Use "help command" to show information about a command.\n' + output = output .. "ESC or Ctrl+d exits the console.\n" + else + local cmd = nil + for _, curcmd in ipairs(cmdlist) do + if curcmd.name:find(param, 1, true) then + cmd = curcmd + if curcmd.name == param then + break -- exact match + end + end + end + if not cmd then + log_add(error_style, 'No command matches "' .. param .. '"!') + return + end + output = output .. 'Command "' .. cmd.name .. '"\n' + for _, arg in ipairs(cmd.args) do + output = output .. ' ' .. arg.name .. ' (' .. arg.type .. ')' + if arg.optional then + output = output .. ' (optional)' + end + output = output .. '\n' + end + if cmd.vararg then + output = output .. 'This command supports variable arguments.\n' + end + end + log_add('', output) +end + -- Run the current command and clear the line (Enter) function handle_enter() if line == '' then @@ -339,7 +380,15 @@ function handle_enter() history[#history + 1] = line end - mp.command(line) + -- match "help []", return or "", strip all whitespace + local help = line:match('^%s*help%s+(.-)%s*$') or + (line:match('^%s*help$') and '') + if help then + help_command(help) + else + mp.command(line) + end + clear() end -- cgit v1.2.3