]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Add an option `-t` to force text output in /help
authorPedro Gimeno <pgimeno@users.noreply.notabug.org>
Sat, 3 Apr 2021 20:51:09 +0000 (22:51 +0200)
committersfan5 <sfan5@live.de>
Fri, 1 Oct 2021 14:22:05 +0000 (16:22 +0200)
This also improves detection of whether formspecs are available.

builtin/common/chatcommands.lua
builtin/common/information_formspecs.lua

index c945e7bdb2926733dfecb72dbb8f8a979e3118e9..21417e42bbabceaf47ba5144dadae42cb2e9bf93 100644 (file)
@@ -6,6 +6,40 @@ local S = core.get_translator("__builtin")
 
 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)
        def = def or {}
        def.params = def.params or ""
@@ -33,22 +67,30 @@ function core.override_chatcommand(name, redefinition)
        core.registered_chatcommands[name] = chatcommand
 end
 
+local function format_help_line(cmd, def)
+       local cmd_marker = INIT == "client" and "." or "/"
+       local msg = core.colorize("#00ffff", cmd_marker .. cmd)
+       if def.params and def.params ~= "" then
+               msg = msg .. " " .. def.params
+       end
+       if def.description and def.description ~= "" then
+               msg = msg .. ": " .. def.description
+       end
+       return msg
+end
+
 local function do_help_cmd(name, param)
-       local function format_help_line(cmd, def)
-               local cmd_marker = "/"
-               if INIT == "client" then
-                       cmd_marker = "."
-               end
-               local msg = core.colorize("#00ffff", cmd_marker .. cmd)
-               if def.params and def.params ~= "" then
-                       msg = msg .. " " .. def.params
-               end
-               if def.description and def.description ~= "" then
-                       msg = msg .. ": " .. def.description
-               end
-               return msg
+       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
-       if param == "" then
+       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 = {}
                for cmd, def in pairs(core.registered_chatcommands) do
                        if INIT == "client" or core.check_player_privs(name, def.privs) then
@@ -71,7 +113,10 @@ local function do_help_cmd(name, param)
                                .. "everything.")
                end
                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 = {}
                for cmd, def in pairs(core.registered_chatcommands) do
                        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:")
                end
                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 = {}
                for priv, def in pairs(core.registered_privileges) do
                        privs[#privs + 1] = priv .. ": " .. def.description
@@ -94,7 +143,7 @@ local function do_help_cmd(name, param)
                table.sort(privs)
                return true, S("Available privileges:").."\n"..table.concat(privs, "\n")
        else
-               local cmd = param
+               local cmd = args[1]
                local def = core.registered_chatcommands[cmd]
                if not def then
                        local msg
@@ -120,8 +169,8 @@ if INIT == "client" then
        })
 else
        core.register_chatcommand("help", {
-               params = S("[all | privs | <cmd>]"),
-               description = S("Get help for commands or list privileges"),
+               params = S("[all | privs | <cmd>] [-t]"),
+               description = S("Get help for commands or list privileges (-t: output in chat)"),
                func = do_help_cmd,
        })
 end
index e814b4c43c8e50bba5b1bd435941b1f3461f0fe0..3405263bf42506489b04a4df1818776f418ac6cf 100644 (file)
@@ -125,30 +125,12 @@ core.register_on_player_receive_fields(function(player, formname, fields)
        end
 end)
 
-
-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",
-                       build_chatcommands_formspec(name))
-               if name ~= admin then
-                       return true
-               end
-       end
-
-       return old_help_func(name, param)
+function core.show_general_help_formspec(name)
+       core.show_formspec(name, "__builtin:help_cmds",
+               build_chatcommands_formspec(name))
 end
 
+function core.show_privs_help_formspec(name)
+       core.show_formspec(name, "__builtin:help_privs",
+               build_privs_formspec(name))
+end