1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-03-03 14:53:01 +02:00

Merge pull request #530 from iltar/search-command

Added a command-search command
This commit is contained in:
Matthew 2018-12-09 08:36:33 -05:00 committed by GitHub
commit fe73cab917
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 1 deletions

View File

@ -8,6 +8,8 @@ local Report = require 'features.report'
local Server = require 'features.server'
local Timestamp = require 'utils.timestamp'
local Command = require 'utils.command'
local format = string.format
local ceil = math.ceil
--local Antigrief = require 'features.antigrief'
@ -595,3 +597,55 @@ 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', 'page'},
default_values = {page = 1},
}, function (arguments, player)
local keyword = arguments.keyword
local p = player.print
if #keyword < 2 then
p('Keyword should be 2 characters or more')
return
end
local per_page = 7
local matches = Command.search(keyword)
local count = #matches
if count == 0 then
p('---- 0 Search Results ----')
p(format('No commands found matching "%s"', keyword))
p('-------------------------')
return
end
local page = tonumber(arguments.page)
local pages = ceil(count / per_page)
if nil == page then
p('Page should be a valid number')
return
end
-- just show the last page
if page > pages then
page = pages
end
if page < 1 then
page = 1
end
local page_start = per_page * (page - 1) + 1
local page_end = per_page * page
page_end = page_end <= count and page_end or count
p(format('---- %d Search %s -----', count, count == 1 and 'Result' or 'Results'))
p(format('Searching for: "%s"', keyword))
for i = page_start, page_end do
p(format('[%d] /%s', i, matches[i]))
end
p(format('-------- Page %d / %d --------', page, pages))
end)

View File

@ -4,6 +4,7 @@ local insert = table.insert
local format = string.format
local next = next
local serialize = serpent.line
local match = string.match
local Command = {}
@ -164,7 +165,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 +204,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