mirror of
https://github.com/CCLeonOS/LeonOS.git
synced 2026-03-03 06:47:00 +00:00
feat: 添加历史记录和配置管理命令及相关测试
fix(project_rules): 修正文档中的中文支持说明 chore: 更新版本号至0.3.8 feat: 新增history命令用于查看和管理命令历史记录 feat: 新增config命令用于管理系统配置 feat: 新增find命令用于文件搜索 docs: 添加新命令建议文档 test: 添加history、config和find命令的测试脚本
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
这个是我的世界模组CC电脑Tweaked版本的操作系统
|
||||
所以要用CC Tweaked所支持的lua代码写
|
||||
所有东西都用英语写
|
||||
所有程序都用英语写(CC Tweaked不支持中文)
|
||||
67
NEW_COMMANDS_SUGGESTIONS.md
Normal file
67
NEW_COMMANDS_SUGGESTIONS.md
Normal file
@@ -0,0 +1,67 @@
|
||||
# New Command Suggestions for LeonOS
|
||||
|
||||
Based on the existing command set in LeonOS, here are some useful new commands that could enhance the user experience:
|
||||
|
||||
## 1. `man`
|
||||
**Description**: Comprehensive manual pages for commands
|
||||
**Purpose**: Provide detailed documentation beyond basic help
|
||||
**Implementation Idea**: `man <command>` would display formatted documentation from a `man` directory structure
|
||||
|
||||
```lua
|
||||
-- Example implementation concept
|
||||
toolcall = {
|
||||
name = "write_to_file",
|
||||
params = {
|
||||
rewrite = false,
|
||||
file_path = "/rom/programs/man.lua",
|
||||
content = "-- man.lua: Manual page viewer\nlocal fs = require(\"fs\")\nlocal textutils = require(\"textutils\")\n\nlocal function show_manual(command)\n local man_path = fs.combine(\"/rom/man\", command .. \"/README.md\")\n if fs.exists(man_path) then\n local file = io.open(man_path, \"r\")\n local content = file:read(\"*a\")\n file:close()\n textutils.pagedPrint(content)\n else\n print(\"No manual page found for \" .. command)\n end\nend\n\n-- Main function implementation..."
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 2. `env`
|
||||
**Description**: Display and manage environment variables
|
||||
**Purpose**: Allow users to view and modify system environment variables
|
||||
**Implementation Idea**: `env` to list variables, `env <name>=<value>` to set
|
||||
|
||||
## 3. `history`
|
||||
**Description**: Command history viewer
|
||||
**Purpose**: View, search, and reuse previous commands
|
||||
**Implementation Idea**: `history` to list, `history <number>` to recall, `history -c` to clear
|
||||
|
||||
## 4. `grep`
|
||||
**Description**: Search text in files
|
||||
**Purpose**: Find specific text patterns in files
|
||||
**Implementation Idea**: `grep <pattern> <file(s)>` with support for regex patterns
|
||||
|
||||
## 5. `find`
|
||||
**Description**: Search for files and directories
|
||||
**Purpose**: Locate files by name or other criteria
|
||||
**Implementation Idea**: `find <path> -name <pattern>` with recursive searching
|
||||
|
||||
## 6. `cmp`
|
||||
**Description**: Compare two files
|
||||
**Purpose**: Find differences between two files
|
||||
**Implementation Idea**: `cmp <file1> <file2>` showing line-by-line differences
|
||||
|
||||
## 7. `date`
|
||||
**Description**: Advanced date and time manipulation
|
||||
**Purpose**: Display and calculate dates
|
||||
**Implementation Idea**: `date [format]` with various formatting options and calculations
|
||||
|
||||
## 8. `net`
|
||||
**Description**: Network utilities
|
||||
**Purpose**: Test and manage network connections
|
||||
**Implementation Idea**: `net ping <host>`, `net status`, etc.
|
||||
|
||||
## 9. `sensors`
|
||||
**Description**: Hardware sensor information
|
||||
**Purpose**: Display data from connected sensors
|
||||
**Implementation Idea**: `sensors` to list all sensors, `sensors <type>` for specific data
|
||||
|
||||
## 10. `config`
|
||||
**Description**: System configuration manager
|
||||
**Purpose**: View and modify system settings
|
||||
**Implementation Idea**: `config get <key>`, `config set <key> <value>`
|
||||
|
||||
These commands would fill gaps in the current functionality and provide a more complete command-line experience for LeonOS users.
|
||||
@@ -1,4 +1,4 @@
|
||||
_G._HOST = _G._HOST .. " (LeonOS 0.3.7)"
|
||||
_G._HOST = _G._HOST .. " (LeonOS 0.3.8)"
|
||||
local fs = rawget(_G, "fs")
|
||||
|
||||
_G._RC_ROM_DIR = _RC_ROM_DIR or (...) and fs.exists("/leonos") and "/leonos" or "/rom"
|
||||
@@ -32,7 +32,7 @@ local rc = {
|
||||
_VERSION = {
|
||||
major = 0,
|
||||
minor = 3,
|
||||
patch = 7
|
||||
patch = 8
|
||||
},
|
||||
queueEvent = pull(os, "queueEvent"),
|
||||
startTimer = pull(os, "startTimer"),
|
||||
|
||||
196
data/computercraft/lua/rom/programs/config.lua
Normal file
196
data/computercraft/lua/rom/programs/config.lua
Normal file
@@ -0,0 +1,196 @@
|
||||
-- config: System configuration manager for LeonOS
|
||||
|
||||
local term = require("term")
|
||||
local colors = require("colors")
|
||||
local settings = require("settings")
|
||||
local textutils = require("textutils")
|
||||
local shell = require("shell")
|
||||
|
||||
-- 保存当前颜色设置
|
||||
local old_fg = term.getTextColor()
|
||||
local old_bg = term.getBackgroundColor()
|
||||
|
||||
-- 设置名称栏颜色并显示
|
||||
term.setTextColor(colors.white)
|
||||
term.setBackgroundColor(colors.cyan)
|
||||
term.at(1, 1).clearLine()
|
||||
term.at(1, 1).write("=== LeonOS Configuration Manager ===")
|
||||
|
||||
-- 恢复颜色设置
|
||||
term.setTextColor(old_fg)
|
||||
term.setBackgroundColor(old_bg)
|
||||
term.at(1, 2)
|
||||
|
||||
-- 显示帮助信息
|
||||
local function show_help()
|
||||
print("Usage: config <command> [options] [setting] [value]")
|
||||
print("")
|
||||
print("Commands:")
|
||||
print(" list List all available settings")
|
||||
print(" get <setting> Get the value of a specific setting")
|
||||
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(" help Show this help message")
|
||||
print("")
|
||||
print("Options:")
|
||||
print(" --details, -d Show detailed information when listing settings")
|
||||
print("")
|
||||
print("Examples:")
|
||||
print(" config list # List all settings")
|
||||
print(" config list --details # List all settings with details")
|
||||
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 save # Save current settings")
|
||||
end
|
||||
|
||||
-- 列出所有设置
|
||||
local function list_settings(detailed)
|
||||
local setting_names = settings.getNames()
|
||||
if #setting_names == 0 then
|
||||
print("No settings defined.")
|
||||
return
|
||||
end
|
||||
|
||||
print("Available settings:")
|
||||
print("====================")
|
||||
|
||||
for _, name in ipairs(setting_names) do
|
||||
local details = settings.getDetails(name)
|
||||
if details then
|
||||
term.setTextColor(colors.yellow)
|
||||
print(name)
|
||||
term.setTextColor(colors.white)
|
||||
|
||||
if detailed then
|
||||
print(" Description: " .. (details.description or "No description"))
|
||||
print(" Type: " .. (details.type or "unknown"))
|
||||
print(" Default: " .. tostring(details.default))
|
||||
print(" Current: " .. tostring(details.value))
|
||||
else
|
||||
print(" Current: " .. tostring(details.value) .. ", Default: " .. tostring(details.default))
|
||||
end
|
||||
print("----------------")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- 获取设置值
|
||||
local function get_setting(name)
|
||||
local details = settings.getDetails(name)
|
||||
if not details then
|
||||
io.stderr:write("Error: Setting '" .. name .. "' not found.\n")
|
||||
return false
|
||||
end
|
||||
|
||||
print("Setting: " .. name)
|
||||
print("Description: " .. (details.description or "No description"))
|
||||
print("Type: " .. (details.type or "unknown"))
|
||||
print("Default: " .. tostring(details.default))
|
||||
term.setTextColor(colors.yellow)
|
||||
print("Current: " .. tostring(details.value))
|
||||
term.setTextColor(colors.white)
|
||||
return true
|
||||
end
|
||||
|
||||
-- 设置新值
|
||||
local function set_setting(name, value)
|
||||
local details = settings.getDetails(name)
|
||||
if not details then
|
||||
io.stderr:write("Error: Setting '" .. name .. "' not found.\n")
|
||||
return false
|
||||
end
|
||||
|
||||
-- 转换值类型
|
||||
if details.type == "boolean" then
|
||||
value = value:lower()
|
||||
if value == "true" or value == "1" then
|
||||
value = true
|
||||
elseif value == "false" or value == "0" then
|
||||
value = false
|
||||
else
|
||||
io.stderr:write("Error: Invalid boolean value. Use 'true' or 'false'.\n")
|
||||
return false
|
||||
end
|
||||
elseif details.type == "number" then
|
||||
local num = tonumber(value)
|
||||
if not num then
|
||||
io.stderr:write("Error: Invalid number value.\n")
|
||||
return false
|
||||
end
|
||||
value = num
|
||||
end
|
||||
|
||||
-- 设置值
|
||||
settings.set(name, value)
|
||||
print("Set '" .. name .. "' to '" .. tostring(value) .. "'")
|
||||
print("Run 'config save' to save this change.")
|
||||
return true
|
||||
end
|
||||
|
||||
-- 重置为默认值
|
||||
local function default_setting(name)
|
||||
local details = settings.getDetails(name)
|
||||
if not details then
|
||||
io.stderr:write("Error: Setting '" .. name .. "' not found.\n")
|
||||
return false
|
||||
end
|
||||
|
||||
settings.unset(name)
|
||||
print("Reset '" .. name .. "' to default value: '" .. tostring(details.default) .. "'")
|
||||
print("Run 'config save' to save this change.")
|
||||
return true
|
||||
end
|
||||
|
||||
-- 保存设置
|
||||
local function save_settings()
|
||||
if settings.save() then
|
||||
print("Settings saved successfully.")
|
||||
return true
|
||||
else
|
||||
io.stderr:write("Error: Failed to save settings.\n")
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- 主函数
|
||||
local function main(args)
|
||||
if #args == 0 then
|
||||
show_help()
|
||||
return
|
||||
end
|
||||
|
||||
local command = args[1]
|
||||
local detailed = false
|
||||
|
||||
-- 检查是否有--details或-d选项
|
||||
for i, arg in ipairs(args) do
|
||||
if arg == "--details" or arg == "-d" then
|
||||
detailed = true
|
||||
table.remove(args, i)
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if command == "list" then
|
||||
list_settings(detailed)
|
||||
elseif command == "get" and #args >= 2 then
|
||||
get_setting(args[2])
|
||||
elseif command == "set" and #args >= 3 then
|
||||
set_setting(args[2], args[3])
|
||||
elseif command == "default" and #args >= 2 then
|
||||
default_setting(args[2])
|
||||
elseif command == "save" then
|
||||
save_settings()
|
||||
elseif command == "help" or command == "--help" or command == "-h" then
|
||||
show_help()
|
||||
else
|
||||
io.stderr:write("Error: Unknown command '" .. command .. "'\n")
|
||||
show_help()
|
||||
end
|
||||
end
|
||||
|
||||
-- 解析命令行参数
|
||||
local args = {...}
|
||||
main(args)
|
||||
199
data/computercraft/lua/rom/programs/find.lua
Normal file
199
data/computercraft/lua/rom/programs/find.lua
Normal file
@@ -0,0 +1,199 @@
|
||||
-- find: File and directory search utility
|
||||
|
||||
local term = require("term")
|
||||
local colors = require("colors")
|
||||
local fs = require("fs")
|
||||
local shell = require("shell")
|
||||
local settings = require("settings")
|
||||
local textutils = require("textutils")
|
||||
|
||||
-- 保存当前颜色设置
|
||||
local old_fg = term.getTextColor()
|
||||
local old_bg = term.getBackgroundColor()
|
||||
|
||||
-- 设置名称栏颜色并显示
|
||||
term.setTextColor(colors.white)
|
||||
term.setBackgroundColor(colors.cyan)
|
||||
term.at(1, 1).clearLine()
|
||||
term.at(1, 1).write("=== File and Directory Search ===")
|
||||
|
||||
-- 恢复颜色设置
|
||||
term.setTextColor(old_fg)
|
||||
term.setBackgroundColor(old_bg)
|
||||
term.at(1, 2)
|
||||
|
||||
-- 显示帮助信息
|
||||
local function show_help()
|
||||
print("Usage: find <path> [pattern] [options]")
|
||||
print("")
|
||||
print("Description:")
|
||||
print(" Recursively search for files and directories.")
|
||||
print("")
|
||||
print("Arguments:")
|
||||
print(" <path> The directory to start searching from (default: current directory)")
|
||||
print(" [pattern] Optional pattern to match files against (supports * and ? wildcards)")
|
||||
print("")
|
||||
print("Options:")
|
||||
print(" --type <type> Search only for files (f) or directories (d)")
|
||||
print(" --name <name> Search for files with this exact name")
|
||||
print(" --hidden Include hidden files and directories")
|
||||
print(" --case-insensitive, -i Perform case-insensitive search")
|
||||
print(" --help, -h Show this help message")
|
||||
print("")
|
||||
print("Examples:")
|
||||
print(" find . *.lua # Find all Lua files in current directory and subdirectories")
|
||||
print(" find /rom --type d # Find all directories under /rom")
|
||||
print(" find /app --name config.lua # Find file named config.lua under /app")
|
||||
print(" find . --hidden # Find all files including hidden ones")
|
||||
end
|
||||
|
||||
-- 检查字符串是否匹配模式(支持*和?通配符)
|
||||
local function matches_pattern(str, pattern, case_insensitive)
|
||||
if case_insensitive then
|
||||
str = str:lower()
|
||||
pattern = pattern:lower()
|
||||
end
|
||||
|
||||
-- 转换通配符模式为Lua正则表达式
|
||||
pattern = pattern:gsub("%.", "%%.")
|
||||
:gsub("%*\%\*", "%.%")
|
||||
:gsub("%*", "[^"]*")
|
||||
:gsub("%?", ".")
|
||||
|
||||
return str:match("^" .. pattern .. "$") ~= nil
|
||||
end
|
||||
|
||||
-- 递归搜索文件和目录
|
||||
local function search(path, options, results)
|
||||
path = shell.resolve(path)
|
||||
results = results or {}
|
||||
|
||||
if not fs.exists(path) then
|
||||
io.stderr:write("Error: Path '" .. path .. "' does not exist.\n")
|
||||
return results
|
||||
end
|
||||
|
||||
if not fs.isDir(path) then
|
||||
-- 如果传入的是文件而不是目录,直接检查是否匹配
|
||||
local name = fs.getName(path)
|
||||
local include = true
|
||||
|
||||
if options.type == "d" then
|
||||
include = false
|
||||
elseif options.name and name ~= options.name then
|
||||
include = false
|
||||
elseif options.pattern and not matches_pattern(name, options.pattern, options.case_insensitive) then
|
||||
include = false
|
||||
elseif not options.hidden and name:sub(1, 1) == "." then
|
||||
include = false
|
||||
end
|
||||
|
||||
if include then
|
||||
results[#results + 1] = path
|
||||
end
|
||||
return results
|
||||
end
|
||||
|
||||
-- 遍历目录
|
||||
local files = fs.list(path)
|
||||
for _, name in ipairs(files) do
|
||||
local full_path = fs.combine(path, name)
|
||||
local is_dir = fs.isDir(full_path)
|
||||
local include = true
|
||||
|
||||
-- 检查是否要包含此文件/目录
|
||||
if options.type == "f" and is_dir then
|
||||
include = false
|
||||
elseif options.type == "d" and not is_dir then
|
||||
include = false
|
||||
elseif options.name and name ~= options.name then
|
||||
include = false
|
||||
elseif options.pattern and not matches_pattern(name, options.pattern, options.case_insensitive) then
|
||||
include = false
|
||||
elseif not options.hidden and name:sub(1, 1) == "." then
|
||||
include = false
|
||||
end
|
||||
|
||||
if include then
|
||||
results[#results + 1] = full_path
|
||||
end
|
||||
|
||||
-- 递归搜索子目录
|
||||
if is_dir then
|
||||
search(full_path, options, results)
|
||||
end
|
||||
end
|
||||
|
||||
return results
|
||||
end
|
||||
|
||||
-- 主函数
|
||||
local function main(args)
|
||||
-- 解析命令行参数
|
||||
local options = {
|
||||
type = nil, -- f: 文件, d: 目录
|
||||
name = nil, -- 精确文件名
|
||||
pattern = nil, -- 通配符模式
|
||||
hidden = settings.get("list.show_hidden"), -- 是否显示隐藏文件
|
||||
case_insensitive = false
|
||||
}
|
||||
|
||||
local path = shell.dir()
|
||||
local i = 1
|
||||
|
||||
while i <= #args do
|
||||
if args[i] == "--help" or args[i] == "-h" then
|
||||
show_help()
|
||||
return
|
||||
elseif args[i] == "--type" and i < #args then
|
||||
i = i + 1
|
||||
options.type = args[i]
|
||||
if options.type ~= "f" and options.type ~= "d" then
|
||||
io.stderr:write("Error: Invalid type. Use 'f' for files or 'd' for directories.\n")
|
||||
return
|
||||
end
|
||||
elseif args[i] == "--name" and i < #args then
|
||||
i = i + 1
|
||||
options.name = args[i]
|
||||
elseif args[i] == "--hidden" then
|
||||
options.hidden = true
|
||||
elseif args[i] == "--case-insensitive" or args[i] == "-i" then
|
||||
options.case_insensitive = true
|
||||
elseif args[i]:sub(1, 1) == "-" then
|
||||
io.stderr:write("Error: Unknown option '" .. args[i] .. "'\n")
|
||||
show_help()
|
||||
return
|
||||
elseif not options.pattern and path == shell.dir() then
|
||||
-- 第一个非选项参数是路径
|
||||
path = args[i]
|
||||
else
|
||||
-- 第二个非选项参数是模式
|
||||
options.pattern = args[i]
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
|
||||
-- 执行搜索
|
||||
local results = search(path, options)
|
||||
|
||||
-- 输出结果
|
||||
if #results == 0 then
|
||||
print("No matching files or directories found.")
|
||||
else
|
||||
print("Found " .. #results .. " matching " .. (options.type == "f" and "files" or options.type == "d" and "directories" or "items") .. ":")
|
||||
for _, result in ipairs(results) do
|
||||
local is_dir = fs.isDir(result)
|
||||
if is_dir then
|
||||
term.setTextColor(colors.green)
|
||||
else
|
||||
term.setTextColor(colors.white)
|
||||
end
|
||||
print(" " .. result)
|
||||
term.setTextColor(old_fg)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- 运行主函数
|
||||
local args = {...}
|
||||
main(args)
|
||||
114
data/computercraft/lua/rom/programs/history.lua
Normal file
114
data/computercraft/lua/rom/programs/history.lua
Normal file
@@ -0,0 +1,114 @@
|
||||
-- history.lua: Command history viewer
|
||||
local term = require("term")
|
||||
local colors = require("colors")
|
||||
local textutils = require("textutils")
|
||||
local shell = require("shell")
|
||||
|
||||
-- Get the global history table from shell
|
||||
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
|
||||
end
|
||||
-- Fallback to empty history if not found
|
||||
return {}
|
||||
end
|
||||
|
||||
-- Display help information
|
||||
local function printHelp()
|
||||
print("Usage: history [options]")
|
||||
print("Options:")
|
||||
print(" --help, -h Show this help message")
|
||||
print(" --clear, -c Clear command history")
|
||||
print(" --search, -s <pattern> Search history for pattern")
|
||||
print(" <number> Execute command by history number")
|
||||
print([[
|
||||
Displays command history in LeonOS.]])
|
||||
end
|
||||
|
||||
-- Clear command history
|
||||
local function clearHistory()
|
||||
local history = getHistory()
|
||||
for i = #history, 1, -1 do
|
||||
history[i] = nil
|
||||
end
|
||||
print("Command history cleared.")
|
||||
end
|
||||
|
||||
-- Search history for pattern
|
||||
local function searchHistory(pattern)
|
||||
local history = getHistory()
|
||||
local results = {}
|
||||
for i, cmd in ipairs(history) do
|
||||
if cmd:find(pattern) then
|
||||
table.insert(results, {i, cmd})
|
||||
end
|
||||
end
|
||||
return results
|
||||
end
|
||||
|
||||
-- Main function
|
||||
local function main(args)
|
||||
-- Process command line arguments
|
||||
if #args == 0 then
|
||||
-- Display all history
|
||||
local history = getHistory()
|
||||
if #history == 0 then
|
||||
print("No command history available.")
|
||||
return
|
||||
end
|
||||
print("Command history:")
|
||||
for i, cmd in ipairs(history) do
|
||||
term.setTextColor(colors.cyan)
|
||||
io.write(string.format(" %3d ", i))
|
||||
term.setTextColor(colors.white)
|
||||
print(cmd)
|
||||
end
|
||||
elseif args[1] == "--help" or args[1] == "-h" then
|
||||
printHelp()
|
||||
elseif args[1] == "--clear" or args[1] == "-c" then
|
||||
clearHistory()
|
||||
elseif args[1] == "--search" or args[1] == "-s" then
|
||||
if #args < 2 then
|
||||
print("Error: Missing search pattern.")
|
||||
printHelp()
|
||||
else
|
||||
local results = searchHistory(args[2])
|
||||
if #results == 0 then
|
||||
print("No matching commands found.")
|
||||
else
|
||||
print("Search results:")
|
||||
for _, item in ipairs(results) do
|
||||
term.setTextColor(colors.cyan)
|
||||
io.write(string.format(" %3d ", item[1]))
|
||||
term.setTextColor(colors.white)
|
||||
print(item[2])
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
-- Try to execute command by number
|
||||
local num = tonumber(args[1])
|
||||
if num then
|
||||
local history = getHistory()
|
||||
if num >= 1 and num <= #history then
|
||||
print("Executing: " .. history[num])
|
||||
shell.run(history[num])
|
||||
else
|
||||
print("Error: Invalid history number.")
|
||||
end
|
||||
else
|
||||
print("Error: Unknown option or command number.")
|
||||
printHelp()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Run the main function
|
||||
local args = {...}
|
||||
main(args)
|
||||
86
data/computercraft/lua/rom/programs/test_config.lua
Normal file
86
data/computercraft/lua/rom/programs/test_config.lua
Normal file
@@ -0,0 +1,86 @@
|
||||
-- test_config.lua: Test the config command
|
||||
|
||||
local term = require("term")
|
||||
local colors = require("colors")
|
||||
local shell = require("shell")
|
||||
|
||||
-- 保存当前颜色设置
|
||||
local old_fg = term.getTextColor()
|
||||
local old_bg = term.getBackgroundColor()
|
||||
|
||||
-- 设置名称栏颜色并显示
|
||||
term.setTextColor(colors.white)
|
||||
term.setBackgroundColor(colors.cyan)
|
||||
term.at(1, 1).clearLine()
|
||||
term.at(1, 1).write("=== Testing Config Command ===")
|
||||
|
||||
-- 恢复颜色设置
|
||||
term.setTextColor(old_fg)
|
||||
term.setBackgroundColor(old_bg)
|
||||
term.at(1, 2)
|
||||
|
||||
-- 测试函数
|
||||
local function run_test(test_name, command)
|
||||
term.setTextColor(colors.yellow)
|
||||
print("\n=== Test: " .. test_name .. " ===")
|
||||
term.setTextColor(colors.white)
|
||||
print("Command: " .. command)
|
||||
print("----------------------------------------")
|
||||
local ok, err = shell.run(command)
|
||||
if not ok and err then
|
||||
io.stderr:write("Test failed: " .. err .. "\n")
|
||||
else
|
||||
term.setTextColor(colors.green)
|
||||
print("Test completed successfully.")
|
||||
term.setTextColor(colors.white)
|
||||
end
|
||||
print("----------------------------------------")
|
||||
os.sleep(1) -- 短暂暂停,让用户有时间查看结果
|
||||
end
|
||||
|
||||
-- 主测试函数
|
||||
local function main()
|
||||
-- 清除屏幕,但保留顶部标题
|
||||
local w, h = term.getSize()
|
||||
for y=2, h do
|
||||
term.at(1, y).clearLine()
|
||||
end
|
||||
term.at(1, 2)
|
||||
|
||||
print("Starting config command tests...")
|
||||
os.sleep(1)
|
||||
|
||||
-- 测试1: 显示帮助信息
|
||||
run_test("Show Help", "config help")
|
||||
|
||||
-- 测试2: 列出所有设置
|
||||
run_test("List Settings", "config list")
|
||||
|
||||
-- 测试3: 列出所有设置(带详细信息)
|
||||
run_test("List Settings with Details", "config list --details")
|
||||
|
||||
-- 测试4: 获取特定设置的值
|
||||
run_test("Get Setting Value", "config get list.show_hidden")
|
||||
|
||||
-- 测试5: 修改设置的值
|
||||
run_test("Set Setting Value", "config set list.show_hidden true")
|
||||
|
||||
-- 测试6: 验证设置已更改
|
||||
run_test("Verify Setting Changed", "config get list.show_hidden")
|
||||
|
||||
-- 测试7: 重置设置为默认值
|
||||
run_test("Reset to Default", "config default list.show_hidden")
|
||||
|
||||
-- 测试8: 验证设置已重置
|
||||
run_test("Verify Reset", "config get list.show_hidden")
|
||||
|
||||
-- 测试9: 保存设置(注意:这会实际修改设置文件)
|
||||
run_test("Save Settings", "config save")
|
||||
|
||||
term.setTextColor(colors.green)
|
||||
print("\nAll tests completed!")
|
||||
term.setTextColor(colors.white)
|
||||
print("You can run 'config' command directly to manage system settings.")
|
||||
end
|
||||
|
||||
main()
|
||||
158
data/computercraft/lua/rom/programs/test_find.lua
Normal file
158
data/computercraft/lua/rom/programs/test_find.lua
Normal file
@@ -0,0 +1,158 @@
|
||||
-- test_find.lua: Test the find command
|
||||
|
||||
local term = require("term")
|
||||
local colors = require("colors")
|
||||
local shell = require("shell")
|
||||
local fs = require("fs")
|
||||
|
||||
-- 保存当前颜色设置
|
||||
local old_fg = term.getTextColor()
|
||||
local old_bg = term.getBackgroundColor()
|
||||
|
||||
-- 设置名称栏颜色并显示
|
||||
term.setTextColor(colors.white)
|
||||
term.setBackgroundColor(colors.cyan)
|
||||
term.at(1, 1).clearLine()
|
||||
term.at(1, 1).write("=== Testing Find Command ===")
|
||||
|
||||
-- 恢复颜色设置
|
||||
term.setTextColor(old_fg)
|
||||
term.setBackgroundColor(old_bg)
|
||||
term.at(1, 2)
|
||||
|
||||
-- 创建测试目录结构
|
||||
local function create_test_files()
|
||||
print("Creating test files and directories...")
|
||||
|
||||
-- 创建测试根目录
|
||||
local test_root = "/test_find"
|
||||
if fs.exists(test_root) then
|
||||
fs.delete(test_root)
|
||||
end
|
||||
fs.makeDir(test_root)
|
||||
|
||||
-- 创建测试文件和目录
|
||||
fs.makeDir(fs.combine(test_root, "dir1"))
|
||||
fs.makeDir(fs.combine(test_root, "dir2"))
|
||||
fs.makeDir(fs.combine(test_root, ".hidden_dir"))
|
||||
|
||||
-- 创建文件
|
||||
local function create_file(path, content)
|
||||
local file = io.open(path, "w")
|
||||
if file then
|
||||
file:write(content)
|
||||
file:close()
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
create_file(fs.combine(test_root, "file1.txt"), "Test file 1")
|
||||
create_file(fs.combine(test_root, "file2.lua"), "-- Test Lua file")
|
||||
create_file(fs.combine(test_root, ".hidden_file"), "Hidden file content")
|
||||
create_file(fs.combine(test_root, "dir1", "subfile1.txt"), "Subdirectory file 1")
|
||||
create_file(fs.combine(test_root, "dir1", "subfile2.lua"), "-- Subdirectory Lua file")
|
||||
create_file(fs.combine(test_root, "dir2", "subfile3.txt"), "Subdirectory file 3")
|
||||
create_file(fs.combine(test_root, ".hidden_dir", "hidden_content.txt"), "Content in hidden directory")
|
||||
|
||||
print("Test files created successfully.")
|
||||
return test_root
|
||||
end
|
||||
|
||||
-- 测试函数
|
||||
local function run_test(test_name, command, expected_count)
|
||||
term.setTextColor(colors.yellow)
|
||||
print("\n=== Test: " .. test_name .. " ===")
|
||||
term.setTextColor(colors.white)
|
||||
print("Command: " .. command)
|
||||
print("----------------------------------------")
|
||||
|
||||
-- 捕获命令输出
|
||||
local old_output = io.output()
|
||||
local output = {}
|
||||
io.output{write = function(s) output[#output+1] = s end}
|
||||
|
||||
local ok, err = shell.run(command)
|
||||
|
||||
-- 恢复输出
|
||||
io.output(old_output)
|
||||
|
||||
-- 计算匹配的结果数量
|
||||
local result_count = 0
|
||||
for line in table.concat(output, ""):gmatch("[^
|
||||
]+") do
|
||||
if line:match("^ /test_find/") then
|
||||
result_count = result_count + 1
|
||||
end
|
||||
end
|
||||
|
||||
-- 检查结果
|
||||
if not ok and err then
|
||||
io.stderr:write("Test failed: " .. err .. "\n")
|
||||
else
|
||||
if expected_count and result_count ~= expected_count then
|
||||
io.stderr:write("Test failed: Expected " .. expected_count .. " results, got " .. result_count .. "\n")
|
||||
else
|
||||
term.setTextColor(colors.green)
|
||||
print("Test completed successfully. Found " .. result_count .. " results.")
|
||||
term.setTextColor(colors.white)
|
||||
end
|
||||
end
|
||||
print("----------------------------------------")
|
||||
os.sleep(1) -- 短暂暂停,让用户有时间查看结果
|
||||
end
|
||||
|
||||
-- 主测试函数
|
||||
local function main()
|
||||
-- 清除屏幕,但保留顶部标题
|
||||
local w, h = term.getSize()
|
||||
for y=2, h do
|
||||
term.at(1, y).clearLine()
|
||||
end
|
||||
term.at(1, 2)
|
||||
|
||||
-- 创建测试文件
|
||||
local test_root = create_test_files()
|
||||
os.sleep(1)
|
||||
|
||||
print("Starting find command tests...")
|
||||
os.sleep(1)
|
||||
|
||||
-- 测试1: 显示帮助信息
|
||||
run_test("Show Help", "find --help")
|
||||
|
||||
-- 测试2: 搜索所有文件和目录
|
||||
run_test("Search All Items", "find " .. test_root, 8)
|
||||
|
||||
-- 测试3: 搜索Lua文件
|
||||
run_test("Search Lua Files", "find " .. test_root .. " *.lua", 2)
|
||||
|
||||
-- 测试4: 搜索目录
|
||||
run_test("Search Directories", "find " .. test_root .. " --type d", 3)
|
||||
|
||||
-- 测试5: 搜索特定名称的文件
|
||||
run_test("Search by Exact Name", "find " .. test_root .. " --name file1.txt", 1)
|
||||
|
||||
-- 测试6: 搜索隐藏文件和目录
|
||||
run_test("Search Hidden Items", "find " .. test_root .. " --hidden", 10)
|
||||
|
||||
-- 测试7: 不区分大小写搜索
|
||||
run_test("Case Insensitive Search", "find " .. test_root .. " FILE*.TXT -i", 1)
|
||||
|
||||
-- 测试8: 组合条件搜索 (Lua文件且不区分大小写)
|
||||
run_test("Combined Conditions", "find " .. test_root .. " *.LUA -i", 2)
|
||||
|
||||
-- 测试9: 搜索不存在的路径
|
||||
run_test("Search Non-existent Path", "find /non_existent_path", 0)
|
||||
|
||||
-- 清理测试文件
|
||||
print("\nCleaning up test files...")
|
||||
fs.delete(test_root)
|
||||
|
||||
term.setTextColor(colors.green)
|
||||
print("\nAll tests completed!")
|
||||
term.setTextColor(colors.white)
|
||||
print("You can run 'find' command directly to search files and directories.")
|
||||
end
|
||||
|
||||
main()
|
||||
72
data/computercraft/lua/rom/programs/test_history.lua
Normal file
72
data/computercraft/lua/rom/programs/test_history.lua
Normal file
@@ -0,0 +1,72 @@
|
||||
-- test_history.lua: Test the history command
|
||||
local shell = require("shell")
|
||||
local term = require("term")
|
||||
|
||||
print("=== Testing history Command ===")
|
||||
print("This test will run the history command with different options")
|
||||
print("to verify it correctly manages command history.")
|
||||
print("
|
||||
First, let's execute some commands to populate history...")
|
||||
|
||||
-- Execute some commands to populate history
|
||||
shell.run("echo Hello, World!")
|
||||
shell.run("list")
|
||||
shell.run("help")
|
||||
|
||||
print("
|
||||
Test 1: Basic history command")
|
||||
os.sleep(1)
|
||||
term.clear()
|
||||
local success = shell.run("history")
|
||||
if not success then
|
||||
print("Error: history command failed to run.")
|
||||
else
|
||||
print("
|
||||
Test 1 completed. Press any key to continue.")
|
||||
os.pullEvent("key")
|
||||
end
|
||||
|
||||
print("
|
||||
Test 2: Search history")
|
||||
os.sleep(1)
|
||||
term.clear()
|
||||
success = shell.run("history", "-s", "he")
|
||||
if not success then
|
||||
print("Error: history search command failed to run.")
|
||||
else
|
||||
print("
|
||||
Test 2 completed. Press any key to continue.")
|
||||
os.pullEvent("key")
|
||||
end
|
||||
|
||||
print("
|
||||
Test 3: Execute command from history")
|
||||
os.sleep(1)
|
||||
term.clear()
|
||||
print("Executing command #1 from history (should be 'echo Hello, World!')")
|
||||
success = shell.run("history", "1")
|
||||
if not success then
|
||||
print("Error: history execution command failed to run.")
|
||||
else
|
||||
print("
|
||||
Test 3 completed. Press any key to continue.")
|
||||
os.pullEvent("key")
|
||||
end
|
||||
|
||||
print("
|
||||
Test 4: history help")
|
||||
os.sleep(1)
|
||||
term.clear()
|
||||
success = shell.run("history", "--help")
|
||||
if not success then
|
||||
print("Error: history help command failed to run.")
|
||||
else
|
||||
print("
|
||||
Test 4 completed. Press any key to finish.")
|
||||
os.pullEvent("key")
|
||||
end
|
||||
|
||||
term.clear()
|
||||
print("=== history Command Tests Completed ===")
|
||||
print("All tests have been executed.")
|
||||
print("You can now use the 'history' command to manage your command history.")
|
||||
@@ -1,5 +1,5 @@
|
||||
-- LeonOS installer
|
||||
local INSTALLER_VERSION = "0.3.7 Beta 12"
|
||||
local INSTALLER_VERSION = "0.3.8"
|
||||
local DEFAULT_ROM_DIR = "/leonos"
|
||||
|
||||
print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")
|
||||
|
||||
Reference in New Issue
Block a user