From acad8982aa4a4b7b7f91f714ff07a11d42c67150 Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Sun, 31 Aug 2025 17:52:37 +0800 Subject: [PATCH] =?UTF-8?q?feat(list):=20=E5=A2=9E=E5=BC=BA=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=91=BD=E4=BB=A4=E5=8A=9F=E8=83=BD=E5=B9=B6=E6=94=B9?= =?UTF-8?q?=E8=BF=9B=E6=98=BE=E7=A4=BA=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 `-l` 参数支持显示文件详细信息 - 改进文件列表的排序和分类显示 - 为不同文件类型添加颜色标识 - 添加统计信息和提示文本 - 修复重启命令别名从 "restart" 改为 "reboot" - 更新系统版本号显示为 LeonOS 1.0 --- data/computercraft/lua/bios.lua | 4 +- data/computercraft/lua/rom/programs/list.lua | 137 ++++++++++++++++-- data/computercraft/lua/rom/programs/shell.lua | 5 +- 3 files changed, 127 insertions(+), 19 deletions(-) diff --git a/data/computercraft/lua/bios.lua b/data/computercraft/lua/bios.lua index 703f2b7..688a740 100644 --- a/data/computercraft/lua/bios.lua +++ b/data/computercraft/lua/bios.lua @@ -1,4 +1,4 @@ -_G._HOST = _G._HOST .. " (LeonOS 1.6.0)" +_G._HOST = _G._HOST .. " (LeonOS 1.0)" local fs = rawget(_G, "fs") @@ -27,7 +27,7 @@ end local expect = function(_, _, _, _) end local shutdown = pull(os, "shutdown") -local reboot = pull(os, "restart") +local reboot = pull(os, "reboot") -- `os` extras go in here now. local rc = { diff --git a/data/computercraft/lua/rom/programs/list.lua b/data/computercraft/lua/rom/programs/list.lua index 5b08a1c..da70f57 100644 --- a/data/computercraft/lua/rom/programs/list.lua +++ b/data/computercraft/lua/rom/programs/list.lua @@ -11,36 +11,143 @@ local textutils = require("textutils") if #args == 0 then args[1] = shell.dir() end local show_hidden = settings.get("list.show_hidden") +local show_details = false + +-- 检查是否有显示详细信息的参数 +for i=1, #args, 1 do + if args[i] == "-l" then + show_details = true + table.remove(args, i) + break + end +end + +-- 如果没有参数了,默认为当前目录 +if #args == 0 then args[1] = shell.dir() end + +-- 获取文件大小的格式化函数 +local function format_size(size) + if size < 1024 then + return size .. " B" + elseif size < 1024 * 1024 then + return string.format("%.1f KB", size / 1024) + 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 + indicator = "" + end + + return color, indicator +end local function list_dir(dir) if not fs.exists(dir) then - error(dir .. ": that directory does not exist", 0) + error("\"" .. dir .. "\" that directory does not exist", 0) elseif not fs.isDir(dir) then - error(dir .. ": not a directory", 0) + error("\"" .. dir .. "\" is not a directory", 0) end local raw_files = fs.list(dir) - local files, dirs = {}, {} + local items = {} + -- 收集文件信息 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] + 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 + + table.insert(items, { + name = name, + color = color, + indicator = indicator, + size = size, + is_dir = fs.isDir(full) + }) end end + + -- 先排序目录,再排序文件 + table.sort(items, function(a, b) + if a.is_dir and not b.is_dir then return true end + if not a.is_dir and b.is_dir then return false end + return a.name < b.name + end) - textutils.pagedTabulate(colors.green, dirs, colors.white, files) + -- 显示标题 + local w = term.getSize() + local title = dir + 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 display_items = {} + local max_name_len = 0 + + -- 计算最长文件名长度 + for _, item in ipairs(items) do + local display_name = item.name .. item.indicator + max_name_len = math.max(max_name_len, #display_name) + end + + -- 创建格式化的显示项 + for _, item in ipairs(items) do + local display_name = item.name .. item.indicator + if show_details then + local padding = string.rep(" ", max_name_len - #display_name + 2) + display_name = display_name .. padding .. item.size + 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 - if #args > 1 then - textutils.coloredPrint(colors.yellow, args[i]..":\n", colors.white) - end list_dir(args[i]) + if i < #args then + print() -- 不同目录之间添加空行 + end end diff --git a/data/computercraft/lua/rom/programs/shell.lua b/data/computercraft/lua/rom/programs/shell.lua index 9aae7bb..b18e0d2 100644 --- a/data/computercraft/lua/rom/programs/shell.lua +++ b/data/computercraft/lua/rom/programs/shell.lua @@ -49,7 +49,8 @@ local aliases = { rm = "delete", rs = "redstone", sh = "shell", - ps = "threads" + ps = "threads", + restart = "reboot" } for k, v in pairs(aliases) do @@ -65,7 +66,7 @@ local history = {} while true do term.setTextColor(colors.yellow) term.setBackgroundColor(colors.black) - rc.write(colors.yellow.."$"..shell.dir()..colors.green.." >>> ") + rc.write(textutils.coloredPrint(colors.yellow, "$"..shell.dir(), colors.green, " >>> ")) term.setTextColor(colors.white) local text = term.read(nil, history, shell.complete)