From 487e8f14c239a6e2ef6f848e2cdf228130f39f33 Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Wed, 3 Sep 2025 12:50:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E9=85=8D=E7=BD=AE=E7=A8=8B=E5=BA=8F):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9F=A5=E6=89=BE=E8=AE=BE=E7=BD=AE=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=B9=B6=E4=BF=AE=E5=A4=8D=E9=80=9A=E9=85=8D=E7=AC=A6?= =?UTF-8?q?=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为config程序添加find命令,支持按名称或描述搜索设置,并支持大小写不敏感选项 修复find.lua中的通配符匹配问题 优化history.lua中的线程环境访问逻辑 添加测试配置文件test_config_find.lua 更新安装程序版本号 --- .../computercraft/lua/rom/programs/config.lua | 85 ++++++++++++++++++- data/computercraft/lua/rom/programs/find.lua | 4 +- .../lua/rom/programs/history.lua | 7 +- .../lua/rom/programs/test_config_find.lua | 70 +++++++++++++++ installer.lua | 2 +- 5 files changed, 156 insertions(+), 12 deletions(-) create mode 100644 data/computercraft/lua/rom/programs/test_config_find.lua diff --git a/data/computercraft/lua/rom/programs/config.lua b/data/computercraft/lua/rom/programs/config.lua index 687ab9a..0a22199 100644 --- a/data/computercraft/lua/rom/programs/config.lua +++ b/data/computercraft/lua/rom/programs/config.lua @@ -31,10 +31,12 @@ local function show_help() print(" set Set the value of a specific setting") print(" default Reset a setting to its default value") print(" save Save current settings to file") + print(" find Find settings by name or description") print(" help Show this help message") print("") print("Options:") print(" --details, -d Show detailed information when listing settings") + print(" --case-insensitive, -i Search case-insensitively") print("") print("Examples:") print(" config list # List all settings") @@ -42,6 +44,8 @@ local function show_help() print(" config get list.show_hidden # Get the value of list.show_hidden") print(" config set list.show_hidden true # Set list.show_hidden to true") print(" config default list.show_hidden # Reset list.show_hidden to default") + print(" config find 'hidden' # Find settings containing 'hidden'") + print(" config find 'color' -i # Find settings with 'color' (case-insensitive)") print(" config save # Save current settings") end @@ -154,6 +158,71 @@ local function save_settings() end end +-- 查找设置 +local function find_settings(pattern, detailed, case_insensitive) + local setting_names = settings.getNames() + if #setting_names == 0 then + print("No settings defined.") + return + end + + local matches = {} + local search_pattern = pattern + + if case_insensitive then + search_pattern = search_pattern:lower() + end + + -- 搜索匹配的设置 + for _, name in ipairs(setting_names) do + local details = settings.getDetails(name) + if details then + local name_match = false + local desc_match = false + + if case_insensitive then + name_match = name:lower():find(search_pattern, 1, true) ~= nil + if details.description then + desc_match = details.description:lower():find(search_pattern, 1, true) ~= nil + end + else + name_match = name:find(search_pattern, 1, true) ~= nil + if details.description then + desc_match = details.description:find(search_pattern, 1, true) ~= nil + end + end + + if name_match or desc_match then + table.insert(matches, {name = name, details = details, name_match = name_match, desc_match = desc_match}) + end + end + end + + if #matches == 0 then + print("No settings found matching pattern '" .. pattern .. "'") + return + end + + print("Found " .. #matches .. " matching setting" .. (#matches > 1 and "s" or "") .. ":") + print("====================") + + for _, match in ipairs(matches) do + term.setTextColor(colors.yellow) + print(match.name) + term.setTextColor(colors.white) + + if detailed then + print(" Description: " .. (match.details.description or "No description")) + print(" Type: " .. (match.details.type or "unknown")) + print(" Default: " .. tostring(match.details.default)) + print(" Current: " .. tostring(match.details.value)) + else + print(" Current: " .. tostring(match.details.value) .. ", Default: " .. tostring(match.details.default)) + end + print("----------------") + end +end + -- 主函数 local function main(args) if #args == 0 then @@ -163,13 +232,19 @@ local function main(args) local command = args[1] local detailed = false + local case_insensitive = false - -- 检查是否有--details或-d选项 - for i, arg in ipairs(args) do - if arg == "--details" or arg == "-d" then + -- 检查是否有选项 + local i = 2 + while i <= #args do + if args[i] == "--details" or args[i] == "-d" then detailed = true table.remove(args, i) - break + elseif args[i] == "--case-insensitive" or args[i] == "-i" then + case_insensitive = true + table.remove(args, i) + else + i = i + 1 end end @@ -183,6 +258,8 @@ local function main(args) default_setting(args[2]) elseif command == "save" then save_settings() + elseif command == "find" and #args >= 2 then + find_settings(args[2], detailed, case_insensitive) elseif command == "help" or command == "--help" or command == "-h" then show_help() else diff --git a/data/computercraft/lua/rom/programs/find.lua b/data/computercraft/lua/rom/programs/find.lua index d504cbe..912696f 100644 --- a/data/computercraft/lua/rom/programs/find.lua +++ b/data/computercraft/lua/rom/programs/find.lua @@ -56,8 +56,8 @@ local function matches_pattern(str, pattern, case_insensitive) -- 转换通配符模式为Lua正则表达式 pattern = pattern:gsub("%.", "%%.") - :gsub("%*\%\*", "%.%") - :gsub("%*", "[^"]*") + :gsub("%*%%%*", ".*") + :gsub("%*", "[^/]*") :gsub("%?", ".") return str:match("^" .. pattern .. "$") ~= nil diff --git a/data/computercraft/lua/rom/programs/history.lua b/data/computercraft/lua/rom/programs/history.lua index 4a36a43..a7c0861 100644 --- a/data/computercraft/lua/rom/programs/history.lua +++ b/data/computercraft/lua/rom/programs/history.lua @@ -9,11 +9,8 @@ local function getHistory() -- Search for the shell thread to access its history local thread = require("rc.thread") local shellThread = thread.vars().parentShell - if shellThread then - local env = thread.getenv(shellThread) - if env and env.history then - return env.history - end + if shellThread and shellThread.env and shellThread.env.history then + return shellThread.env.history end -- Fallback to empty history if not found return {} diff --git a/data/computercraft/lua/rom/programs/test_config_find.lua b/data/computercraft/lua/rom/programs/test_config_find.lua new file mode 100644 index 0000000..bf6316c --- /dev/null +++ b/data/computercraft/lua/rom/programs/test_config_find.lua @@ -0,0 +1,70 @@ +local shell = require("shell") +local term = require("term") +local colors = require("colors") + +-- 保存当前颜色设置 +local old_fg = term.getTextColor() +local old_bg = term.getBackgroundColor() + +-- 测试函数 +local function run_test(name, command) + term.setTextColor(colors.cyan) + print("\n=== Running test: " .. name .. " ===") + term.setTextColor(colors.white) + print("Command: " .. command) + print("----------------------------------------") + shell.run(command) + print("----------------------------------------") +end + +-- 主测试函数 +local function main() + -- 恢复颜色设置 + term.setTextColor(old_fg) + term.setBackgroundColor(old_bg) + term.at(1, 1).clearLine() + + -- 设置标题颜色 + term.setTextColor(colors.white) + term.setBackgroundColor(colors.cyan) + term.at(1, 1).write("=== Testing Config Find Command ===") + term.setTextColor(old_fg) + term.setBackgroundColor(old_bg) + term.at(1, 2) + + -- 测试1: 基本find命令 - 搜索带"hidden"的设置 + run_test("Basic search for 'hidden'", "config find hidden") + + -- 测试2: 带--details选项的find命令 + run_test("Search with details", "config find hidden --details") + + -- 测试3: 大小写不敏感搜索 + run_test("Case-insensitive search", "config find HIDDEN -i") + + -- 测试4: 使用长选项--case-insensitive + run_test("Case-insensitive search (long option)", "config find HIDDEN --case-insensitive") + + -- 测试5: 搜索描述中的关键词 + -- 注意:这取决于实际设置的描述内容,可能需要根据系统调整 + run_test("Search in descriptions", "config find directory") + + -- 测试6: 组合选项 - 带详细信息的大小写不敏感搜索 + run_test("Combined options", "config find HIDDEN -i --details") + + -- 测试7: 搜索不存在的模式 + run_test("Search for non-existent pattern", "config find nonexistentpattern123") + + -- 测试8: 查看帮助信息,确认find命令已正确添加 + run_test("Check help message", "config --help") + + -- 测试9: 测试find命令的错误用法 + run_test("Test invalid usage", "config find") + + -- 显示测试完成信息 + term.setTextColor(colors.green) + print("\n=== All tests completed ===") + term.setTextColor(old_fg) +end + +-- 运行测试 +main() \ No newline at end of file diff --git a/installer.lua b/installer.lua index 7a88061..e715904 100644 --- a/installer.lua +++ b/installer.lua @@ -1,5 +1,5 @@ -- LeonOS installer -local INSTALLER_VERSION = "0.3.8" +local INSTALLER_VERSION = "0.3.8 Beta 1" local DEFAULT_ROM_DIR = "/leonos" print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")