From a9d40f74a9111697619f8a73c64b465dde9d445b Mon Sep 17 00:00:00 2001 From: Lynn Date: Sun, 9 Dec 2018 00:34:25 +0100 Subject: [PATCH] Added a command-search command --- features/custom_commands.lua | 41 ++++++++++++++++++++++++++++++++++++ utils/command.lua | 28 +++++++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/features/custom_commands.lua b/features/custom_commands.lua index c09ab5bd..149b9f1f 100644 --- a/features/custom_commands.lua +++ b/features/custom_commands.lua @@ -8,6 +8,9 @@ local Report = require 'features.report' local Server = require 'features.server' local Timestamp = require 'utils.timestamp' local Command = require 'utils.command' +local string_length = string.len +local string_repeat = string.rep +local format = string.format --local Antigrief = require 'features.antigrief' @@ -595,3 +598,41 @@ commands.add_command( 'moves you to the antigrief surface or back (Admins only)', antigrief_surface_tp ) ]] + +Command.add('search-command', { + description = 'Search for commands matching the keyword in name or description', + arguments = {'keyword'}, + capture_excess_arguments = true, +}, function (arguments, player) + local keyword = arguments.keyword + local p = player.print + if string_length(keyword) < 2 then + p('Keyword should be 2 characters or more') + return + end + + local matches = Command.search(arguments.keyword) + local count = #matches + + if count == 0 then + p('----- Search Results -----') + p(format('No commands found matching "%s"', keyword)) + return + end + + local max = count > 7 and 7 or count + + p('----- Search Results -----') + for i = 1, max do + p(format('/%s', matches[i])) + end + + local diff = count - max + local singular = diff == 1 + p('-------------------------') + if max ~= count then + p(format('%d %s hidden, please narrow your search', diff, singular and 'result was' or 'results were')) + else + p(format('%d %s matching "%s"', count, singular and 'result' or 'results', keyword)) + end +end) diff --git a/utils/command.lua b/utils/command.lua index 745a654d..0641f6d7 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -4,6 +4,8 @@ local insert = table.insert local format = string.format local next = next local serialize = serpent.line +local match = string.match +local lower = string.lower local Command = {} @@ -164,7 +166,7 @@ function Command.add(command_name, options, callback) end if parameter == nil then - insert(errors, format('Argument %s from command %s is missing.', argument, command_name)) + insert(errors, format('Argument "%s" from command %s is missing.', argument, command_name)) else named_arguments[argument] = parameter end @@ -203,4 +205,28 @@ function Command.add(command_name, options, callback) end) end +function Command.search(keyword) + local matches = {} + local count = 0 + keyword = keyword:lower() + for name, description in pairs(commands.commands) do + local command = format('%s %s', name, description) + if match(command:lower(), keyword) then + count = count + 1 + matches[count] = command + end + end + + -- built-in commands use LocalisedString, which cannot be translated until player.print is called + for name in pairs(commands.game_commands) do + name = name + if match(name:lower(), keyword) then + count = count + 1 + matches[count] = name + end + end + + return matches +end + return Command