mirror of
https://github.com/CCLeonOS/LeonOS.git
synced 2026-03-03 06:47:00 +00:00
feat(配置程序): 添加查找设置功能并修复通配符匹配
为config程序添加find命令,支持按名称或描述搜索设置,并支持大小写不敏感选项 修复find.lua中的通配符匹配问题 优化history.lua中的线程环境访问逻辑 添加测试配置文件test_config_find.lua 更新安装程序版本号
This commit is contained in:
@@ -31,10 +31,12 @@ local function show_help()
|
||||
print(" set <setting> <value> Set the value of a specific setting")
|
||||
print(" default <setting> Reset a setting to its default value")
|
||||
print(" save Save current settings to file")
|
||||
print(" find <pattern> 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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {}
|
||||
|
||||
70
data/computercraft/lua/rom/programs/test_config_find.lua
Normal file
70
data/computercraft/lua/rom/programs/test_config_find.lua
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user