mirror of
https://github.com/CCLeonOS/LeonOS.git
synced 2026-03-03 15:17:01 +00:00
refactor(rom): 重写list.lua为rc.shell并添加shell初始化逻辑
将原有的list.lua程序完全重构为rc.shell,实现以下改进: 1. 添加shell初始化流程,包括运行startup脚本 2. 实现命令别名和自动补全功能 3. 添加交互式shell循环和历史记录功能
This commit is contained in:
@@ -1,154 +1,79 @@
|
|||||||
-- list
|
-- rc.shell
|
||||||
|
|
||||||
local args = {...}
|
|
||||||
|
|
||||||
|
local rc = require("rc")
|
||||||
local fs = require("fs")
|
local fs = require("fs")
|
||||||
|
local term = require("term")
|
||||||
local shell = require("shell")
|
local shell = require("shell")
|
||||||
local colors = require("colors")
|
local colors = require("colors")
|
||||||
local settings = require("settings")
|
local thread = require("rc.thread")
|
||||||
local textutils = require("textutils")
|
local textutils = require("textutils")
|
||||||
local term = require("term")
|
|
||||||
|
|
||||||
if #args == 0 then args[1] = shell.dir() end
|
if os.version then
|
||||||
|
textutils.coloredPrint(colors.yellow, os.version(), colors.white)
|
||||||
local show_hidden = settings.get("list.show_hidden")
|
else
|
||||||
local show_details = false
|
textutils.coloredPrint(colors.yellow, rc.version(), colors.white)
|
||||||
|
|
||||||
-- 检查是否有显示详细信息的参数
|
|
||||||
for i=1, #args, 1 do
|
|
||||||
if args[i] == "-l" then
|
|
||||||
show_details = true
|
|
||||||
table.remove(args, i)
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 如果没有参数了,默认为当前目录
|
thread.vars().parentShell = thread.id()
|
||||||
if #args == 0 then args[1] = shell.dir() end
|
shell.init(_ENV)
|
||||||
|
|
||||||
-- 获取文件大小的格式化函数
|
if not shell.__has_run_startup then
|
||||||
local function format_size(size)
|
shell.__has_run_startup = true
|
||||||
if size < 1024 then
|
if fs.exists("/startup.lua") then
|
||||||
return size .. " B"
|
local ok, err = pcall(dofile, "/startup.lua")
|
||||||
elseif size < 1024 * 1024 then
|
if not ok and err then
|
||||||
return string.format("%.1f KB", size / 1024)
|
io.stderr:write(err, "\n")
|
||||||
else
|
|
||||||
return string.format("%.1f MB", size / (1024 * 1024))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- 获取文件类型和对应的颜色
|
|
||||||
local function get_file_info(filename, full_path)
|
|
||||||
local is_dir = fs.isDir(full_path)
|
|
||||||
local color, indicator
|
|
||||||
|
|
||||||
if is_dir then
|
|
||||||
color = colors.green
|
|
||||||
indicator = "/"
|
|
||||||
else
|
|
||||||
-- 根据文件扩展名设置不同颜色
|
|
||||||
local ext = filename:match("%.(%w+)$") or ""
|
|
||||||
ext = ext:lower()
|
|
||||||
|
|
||||||
if ext == "lua" then
|
|
||||||
color = colors.cyan
|
|
||||||
elseif ext == "txt" or ext == "md" or ext == "hlp" then
|
|
||||||
color = colors.white
|
|
||||||
elseif ext == "png" or ext == "jpg" or ext == "gif" then
|
|
||||||
color = colors.magenta
|
|
||||||
else
|
|
||||||
color = colors.lightGray
|
|
||||||
end
|
end
|
||||||
indicator = ""
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return color, indicator
|
if fs.exists("/startup") and fs.isDir("/startup") then
|
||||||
end
|
local files = fs.list("/startup/")
|
||||||
|
table.sort(files)
|
||||||
|
|
||||||
local function list_dir(dir)
|
for f=1, #files, 1 do
|
||||||
if not fs.exists(dir) then
|
local ok, err = pcall(dofile, "/startup/"..files[f])
|
||||||
error("\"" .. dir .. "\" that directory does not exist", 0)
|
if not ok and err then
|
||||||
elseif not fs.isDir(dir) then
|
io.stderr:write(err, "\n")
|
||||||
error("\"" .. dir .. "\" is not a directory", 0)
|
|
||||||
end
|
|
||||||
|
|
||||||
local raw_files = fs.list(dir)
|
|
||||||
local items = {}
|
|
||||||
|
|
||||||
-- 收集文件信息
|
|
||||||
for i=1, #raw_files, 1 do
|
|
||||||
local name = raw_files[i]
|
|
||||||
local full = fs.combine(dir, name)
|
|
||||||
|
|
||||||
if name:sub(1,1) ~= "." or show_hidden then
|
|
||||||
local color, indicator = get_file_info(name, full)
|
|
||||||
local size = ""
|
|
||||||
|
|
||||||
if show_details and not fs.isDir(full) then
|
|
||||||
local size_bytes = fs.getSize(full)
|
|
||||||
size = format_size(size_bytes)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(items, {
|
|
||||||
name = name,
|
|
||||||
color = color,
|
|
||||||
indicator = indicator,
|
|
||||||
size = size,
|
|
||||||
is_dir = fs.isDir(full)
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- 先排序目录,再排序文件
|
local aliases = {
|
||||||
table.sort(items, function(a, b)
|
background = "bg",
|
||||||
if a.is_dir and not b.is_dir then return true end
|
clr = "clear",
|
||||||
if not a.is_dir and b.is_dir then return false end
|
cp = "copy",
|
||||||
return a.name < b.name
|
dir = "list",
|
||||||
end)
|
foreground = "fg",
|
||||||
|
mv = "move",
|
||||||
|
rm = "delete",
|
||||||
|
rs = "redstone",
|
||||||
|
sh = "shell",
|
||||||
|
ps = "threads"
|
||||||
|
}
|
||||||
|
|
||||||
-- 显示标题
|
for k, v in pairs(aliases) do
|
||||||
local w = term.getSize()
|
shell.setAlias(k, v)
|
||||||
local title = dir
|
end
|
||||||
textutils.coloredPrint(colors.yellow, string.rep("=", w), colors.white)
|
|
||||||
textutils.coloredPrint(colors.yellow, title, colors.white)
|
|
||||||
textutils.coloredPrint(colors.yellow, string.rep("=", w), colors.white)
|
|
||||||
|
|
||||||
-- 显示文件列表
|
local completions = "/rc/completions"
|
||||||
local display_items = {}
|
for _, prog in ipairs(fs.list(completions)) do
|
||||||
local max_name_len = 0
|
dofile(fs.combine(completions, prog))
|
||||||
|
end
|
||||||
|
|
||||||
-- 计算最长文件名长度
|
local history = {}
|
||||||
for _, item in ipairs(items) do
|
while true do
|
||||||
local display_name = item.name .. item.indicator
|
term.setTextColor(colors.yellow)
|
||||||
max_name_len = math.max(max_name_len, #display_name)
|
term.setBackgroundColor(colors.black)
|
||||||
end
|
rc.write(colors.yellow.."$"..shell.dir()..colors.green.." >>> ")
|
||||||
|
term.setTextColor(colors.white)
|
||||||
|
|
||||||
-- 创建格式化的显示项
|
local text = term.read(nil, history, shell.complete)
|
||||||
for _, item in ipairs(items) do
|
if #text > 0 then
|
||||||
local display_name = item.name .. item.indicator
|
history[#history+1] = text
|
||||||
if show_details then
|
local ok, err = shell.run(text)
|
||||||
local padding = string.rep(" ", max_name_len - #display_name + 2)
|
if not ok and err then
|
||||||
display_name = display_name .. padding .. item.size
|
io.stderr:write(err, "\n")
|
||||||
end
|
end
|
||||||
table.insert(display_items, {item.color, display_name, colors.white})
|
|
||||||
end
|
|
||||||
|
|
||||||
-- 使用分页表格显示
|
|
||||||
textutils.pagedTabulate(unpack(display_items))
|
|
||||||
|
|
||||||
-- 显示统计信息
|
|
||||||
textutils.coloredPrint(colors.yellow, "\nTotal: " .. #items .. " items", colors.white)
|
|
||||||
|
|
||||||
-- 显示提示
|
|
||||||
if not show_details then
|
|
||||||
textutils.coloredPrint(colors.lightGray, "Type 'list -l' for detailed information", colors.white)
|
|
||||||
end
|
|
||||||
textutils.coloredPrint(colors.yellow, string.rep("=", w), colors.white)
|
|
||||||
end
|
|
||||||
|
|
||||||
for i=1, #args, 1 do
|
|
||||||
list_dir(args[i])
|
|
||||||
if i < #args then
|
|
||||||
print() -- 不同目录之间添加空行
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user