Add an option -t to force text output in /help

This also improves detection of whether formspecs are available.
This commit is contained in:
Pedro Gimeno 2021-04-03 22:51:09 +02:00 committed by sfan5
parent 982e03f60d
commit d7e7ade0f6
2 changed files with 76 additions and 45 deletions

@ -6,6 +6,40 @@ local S = core.get_translator("__builtin")
core.registered_chatcommands = {} core.registered_chatcommands = {}
-- Interpret the parameters of a command, separating options and arguments.
-- Input: parameters of a command
-- Returns: opts, args
-- opts is a string of option letters, or false on error
-- args is an array with the non-option arguments in order, or an error message
-- Example: for this command line:
-- /command a b -cd e f -g
-- the function would receive:
-- a b -cd e f -g
-- and it would return:
-- "cdg", {"a", "b", "e", "f"}
-- Negative numbers are taken as arguments. Long options (--option) are
-- currently rejected as reserved.
local function getopts(param)
local opts = ""
local args = {}
for match in param:gmatch("%S+") do
if match:byte(1) == 45 then -- 45 = '-'
local second = match:byte(2)
if second == 45 then
return false, S("Flags beginning with -- are reserved")
elseif second and (second < 48 or second > 57) then -- 48 = '0', 57 = '9'
opts = opts .. match:sub(2)
else
-- numeric, add it to args
args[#args + 1] = match
end
else
args[#args + 1] = match
end
end
return opts, args
end
function core.register_chatcommand(cmd, def) function core.register_chatcommand(cmd, def)
def = def or {} def = def or {}
def.params = def.params or "" def.params = def.params or ""
@ -33,12 +67,8 @@ function core.override_chatcommand(name, redefinition)
core.registered_chatcommands[name] = chatcommand core.registered_chatcommands[name] = chatcommand
end end
local function do_help_cmd(name, param)
local function format_help_line(cmd, def) local function format_help_line(cmd, def)
local cmd_marker = "/" local cmd_marker = INIT == "client" and "." or "/"
if INIT == "client" then
cmd_marker = "."
end
local msg = core.colorize("#00ffff", cmd_marker .. cmd) local msg = core.colorize("#00ffff", cmd_marker .. cmd)
if def.params and def.params ~= "" then if def.params and def.params ~= "" then
msg = msg .. " " .. def.params msg = msg .. " " .. def.params
@ -48,7 +78,19 @@ local function do_help_cmd(name, param)
end end
return msg return msg
end end
if param == "" then
local function do_help_cmd(name, param)
local opts, args = getopts(param)
if not opts then
return false, args
end
if #args > 1 then
return false, S("Too many arguments, try using just /help <command>")
end
local use_gui = INIT ~= "client" and core.get_player_by_name(name)
use_gui = use_gui and not opts:find("t")
if #args == 0 and not use_gui then
local cmds = {} local cmds = {}
for cmd, def in pairs(core.registered_chatcommands) do for cmd, def in pairs(core.registered_chatcommands) do
if INIT == "client" or core.check_player_privs(name, def.privs) then if INIT == "client" or core.check_player_privs(name, def.privs) then
@ -71,7 +113,10 @@ local function do_help_cmd(name, param)
.. "everything.") .. "everything.")
end end
return true, msg return true, msg
elseif param == "all" then elseif #args == 0 or (args[1] == "all" and use_gui) then
core.show_general_help_formspec(name)
return true
elseif args[1] == "all" then
local cmds = {} local cmds = {}
for cmd, def in pairs(core.registered_chatcommands) do for cmd, def in pairs(core.registered_chatcommands) do
if INIT == "client" or core.check_player_privs(name, def.privs) then if INIT == "client" or core.check_player_privs(name, def.privs) then
@ -86,7 +131,11 @@ local function do_help_cmd(name, param)
msg = core.gettext("Available commands:") msg = core.gettext("Available commands:")
end end
return true, msg.."\n"..table.concat(cmds, "\n") return true, msg.."\n"..table.concat(cmds, "\n")
elseif INIT == "game" and param == "privs" then elseif INIT == "game" and args[1] == "privs" then
if use_gui then
core.show_privs_help_formspec(name)
return true
end
local privs = {} local privs = {}
for priv, def in pairs(core.registered_privileges) do for priv, def in pairs(core.registered_privileges) do
privs[#privs + 1] = priv .. ": " .. def.description privs[#privs + 1] = priv .. ": " .. def.description
@ -94,7 +143,7 @@ local function do_help_cmd(name, param)
table.sort(privs) table.sort(privs)
return true, S("Available privileges:").."\n"..table.concat(privs, "\n") return true, S("Available privileges:").."\n"..table.concat(privs, "\n")
else else
local cmd = param local cmd = args[1]
local def = core.registered_chatcommands[cmd] local def = core.registered_chatcommands[cmd]
if not def then if not def then
local msg local msg
@ -120,8 +169,8 @@ if INIT == "client" then
}) })
else else
core.register_chatcommand("help", { core.register_chatcommand("help", {
params = S("[all | privs | <cmd>]"), params = S("[all | privs | <cmd>] [-t]"),
description = S("Get help for commands or list privileges"), description = S("Get help for commands or list privileges (-t: output in chat)"),
func = do_help_cmd, func = do_help_cmd,
}) })
end end

@ -125,30 +125,12 @@ core.register_on_player_receive_fields(function(player, formname, fields)
end end
end) end)
function core.show_general_help_formspec(name)
local help_command = core.registered_chatcommands["help"]
local old_help_func = help_command.func
help_command.func = function(name, param)
local admin = core.settings:get("name")
-- If the admin ran help, put the output in the chat buffer as well to
-- work with the server terminal
if param == "privs" then
core.show_formspec(name, "__builtin:help_privs",
build_privs_formspec(name))
if name ~= admin then
return true
end
end
if param == "" or param == "all" then
core.show_formspec(name, "__builtin:help_cmds", core.show_formspec(name, "__builtin:help_cmds",
build_chatcommands_formspec(name)) build_chatcommands_formspec(name))
if name ~= admin then
return true
end
end end
return old_help_func(name, param) function core.show_privs_help_formspec(name)
core.show_formspec(name, "__builtin:help_privs",
build_privs_formspec(name))
end end