From a9d40f74a9111697619f8a73c64b465dde9d445b Mon Sep 17 00:00:00 2001 From: Lynn Date: Sun, 9 Dec 2018 00:34:25 +0100 Subject: [PATCH 1/4] 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 From 35c916c9b48000827516b94d2df6fa3a672b20e9 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sun, 9 Dec 2018 00:37:59 +0100 Subject: [PATCH 2/4] Removed unused variables --- features/custom_commands.lua | 1 - utils/command.lua | 1 - 2 files changed, 2 deletions(-) diff --git a/features/custom_commands.lua b/features/custom_commands.lua index 149b9f1f..0c80154f 100644 --- a/features/custom_commands.lua +++ b/features/custom_commands.lua @@ -9,7 +9,6 @@ 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' diff --git a/utils/command.lua b/utils/command.lua index 0641f6d7..915f6e7b 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -5,7 +5,6 @@ local format = string.format local next = next local serialize = serpent.line local match = string.match -local lower = string.lower local Command = {} From 55a3cf5f226d8bb8a61705afec07da2131369bb7 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sun, 9 Dec 2018 12:44:20 +0100 Subject: [PATCH 3/4] Using # instead of string.len --- features/custom_commands.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/features/custom_commands.lua b/features/custom_commands.lua index 0c80154f..7048f804 100644 --- a/features/custom_commands.lua +++ b/features/custom_commands.lua @@ -8,7 +8,6 @@ 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 format = string.format --local Antigrief = require 'features.antigrief' @@ -605,7 +604,7 @@ Command.add('search-command', { }, function (arguments, player) local keyword = arguments.keyword local p = player.print - if string_length(keyword) < 2 then + if #keyword < 2 then p('Keyword should be 2 characters or more') return end From b44e10b2529275a1c2dc7a06af05f505af7d7c1b Mon Sep 17 00:00:00 2001 From: Lynn Date: Sun, 9 Dec 2018 14:12:59 +0100 Subject: [PATCH 4/4] Added pagination --- features/custom_commands.lua | 45 ++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/features/custom_commands.lua b/features/custom_commands.lua index 7048f804..df2e897d 100644 --- a/features/custom_commands.lua +++ b/features/custom_commands.lua @@ -9,6 +9,7 @@ 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' @@ -599,8 +600,8 @@ commands.add_command( Command.add('search-command', { description = 'Search for commands matching the keyword in name or description', - arguments = {'keyword'}, - capture_excess_arguments = true, + arguments = {'keyword', 'page'}, + default_values = {page = 1}, }, function (arguments, player) local keyword = arguments.keyword local p = player.print @@ -609,28 +610,42 @@ Command.add('search-command', { return end - local matches = Command.search(arguments.keyword) + local per_page = 7 + local matches = Command.search(keyword) local count = #matches if count == 0 then - p('----- Search Results -----') + p('---- 0 Search Results ----') p(format('No commands found matching "%s"', keyword)) + p('-------------------------') return end - local max = count > 7 and 7 or count + local page = tonumber(arguments.page) + local pages = ceil(count / per_page) - p('----- Search Results -----') - for i = 1, max do - p(format('/%s', matches[i])) + if nil == page then + p('Page should be a valid number') + return 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)) + -- 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)