1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-09-16 09:16:22 +02:00

Added a command-search command

This commit is contained in:
Lynn
2018-12-09 00:34:25 +01:00
parent 0a076b5e1f
commit a9d40f74a9
2 changed files with 68 additions and 1 deletions

View File

@@ -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)

View File

@@ -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