summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2019-12-24 16:08:04 +0100
committerwm4 <wm4@nowhere>2019-12-24 16:08:04 +0100
commit8e9644761a66ce277f1bf33bb5f6ab90375027de (patch)
tree6dabb954924ef0a95c9069f7b33586eb5065ccbc
parent1d2fcb9227544e29d261a7256f0774d78fd72e60 (diff)
downloadmpv-8e9644761a66ce277f1bf33bb5f6ab90375027de.tar.bz2
mpv-8e9644761a66ce277f1bf33bb5f6ab90375027de.tar.xz
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.
-rw-r--r--player/lua/console.lua51
1 files changed, 50 insertions, 1 deletions
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 [<text>]", return <text> 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