mirror of
https://github.com/CCLeonOS/LeonOS.git
synced 2026-03-03 06:47:00 +00:00
fix(project_rules): 修正文档中的中文支持说明 chore: 更新版本号至0.3.8 feat: 新增history命令用于查看和管理命令历史记录 feat: 新增config命令用于管理系统配置 feat: 新增find命令用于文件搜索 docs: 添加新命令建议文档 test: 添加history、config和find命令的测试脚本
114 lines
3.0 KiB
Lua
114 lines
3.0 KiB
Lua
-- 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) |