refactor(列表程序): 重构列表程序以简化代码并改进功能

- 移除不必要的启动检查和别名设置代码
- 实现新的目录列表功能,支持隐藏文件显示设置
- 使用分页表格显示目录内容,区分文件和目录
- 添加参数处理,支持多目录列表显示
This commit is contained in:
2025-08-31 19:52:49 +08:00
parent 95165d491d
commit 3541658c24

View File

@@ -1,79 +1,46 @@
-- rc.shell
-- list
local args = {...}
local rc = require("rc")
local fs = require("fs")
local term = require("term")
local shell = require("shell")
local colors = require("colors")
local thread = require("rc.thread")
local settings = require("settings")
local textutils = require("textutils")
if os.version then
textutils.coloredPrint(colors.yellow, os.version(), colors.white)
else
textutils.coloredPrint(colors.yellow, rc.version(), colors.white)
end
if #args == 0 then args[1] = shell.dir() end
thread.vars().parentShell = thread.id()
shell.init(_ENV)
local show_hidden = settings.get("list.show_hidden")
if not shell.__has_run_startup then
shell.__has_run_startup = true
if fs.exists("/startup.lua") then
local ok, err = pcall(dofile, "/startup.lua")
if not ok and err then
io.stderr:write(err, "\n")
end
local function list_dir(dir)
if not fs.exists(dir) then
error(dir .. ": that directory does not exist", 0)
elseif not fs.isDir(dir) then
error(dir .. ": not a directory", 0)
end
if fs.exists("/startup") and fs.isDir("/startup") then
local files = fs.list("/startup/")
table.sort(files)
local raw_files = fs.list(dir)
local files, dirs = {}, {}
for f=1, #files, 1 do
local ok, err = pcall(dofile, "/startup/"..files[f])
if not ok and err then
io.stderr:write(err, "\n")
for i=1, #raw_files, 1 do
local full = fs.combine(dir, raw_files[i])
if raw_files[i]:sub(1,1) ~= "." or show_hidden then
if fs.isDir(full) then
dirs[#dirs+1] = raw_files[i]
else
files[#files+1] = raw_files[i]
end
end
end
textutils.pagedTabulate(colors.green, dirs, colors.white, files)
end
local aliases = {
background = "bg",
clr = "clear",
cp = "copy",
dir = "list",
foreground = "fg",
mv = "move",
rm = "delete",
rs = "redstone",
sh = "shell",
ps = "threads"
}
for k, v in pairs(aliases) do
shell.setAlias(k, v)
end
local completions = "/rc/completions"
for _, prog in ipairs(fs.list(completions)) do
dofile(fs.combine(completions, prog))
end
local history = {}
while true do
term.setTextColor(colors.yellow)
term.setBackgroundColor(colors.black)
rc.write(colors.yellow.."$"..shell.dir()..colors.green.." >>> ")
term.setTextColor(colors.white)
local text = term.read(nil, history, shell.complete)
if #text > 0 then
history[#history+1] = text
local ok, err = shell.run(text)
if not ok and err then
io.stderr:write(err, "\n")
end
for i=1, #args, 1 do
if #args > 1 then
textutils.coloredPrint(colors.yellow, args[i]..":\n", colors.white)
end
list_dir(args[i])
end