From 94da18cc24c68b9f253b63c0a42ca8a8fb4b50af Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Mon, 1 Sep 2025 20:28:38 +0800 Subject: [PATCH] =?UTF-8?q?feat(shell):=20=E6=B7=BB=E5=8A=A0=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86=E7=AA=97=E5=8F=A3=E5=B9=B6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E7=89=88=E6=9C=AC=E5=8F=B7=E8=87=B30.2.6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在shell程序中添加图形化错误处理窗口,支持错误信息展示和重新运行功能 更新installer.lua和bios.lua中的版本号至0.2.6 将peripherals.lua中的中文提示改为英文 --- data/computercraft/lua/bios.lua | 4 +- .../lua/rom/programs/peripherals.lua | 2 +- data/computercraft/lua/rom/programs/shell.lua | 94 ++++++++++++++++++- installer.lua | 2 +- 4 files changed, 97 insertions(+), 5 deletions(-) diff --git a/data/computercraft/lua/bios.lua b/data/computercraft/lua/bios.lua index 1797d15..0ed2c2b 100644 --- a/data/computercraft/lua/bios.lua +++ b/data/computercraft/lua/bios.lua @@ -1,4 +1,4 @@ -_G._HOST = _G._HOST .. " (LeonOS 0.2.5)" +_G._HOST = _G._HOST .. " (LeonOS 0.2.6)" 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 = 2, - patch = 5 + patch = 6 }, queueEvent = pull(os, "queueEvent"), startTimer = pull(os, "startTimer"), diff --git a/data/computercraft/lua/rom/programs/peripherals.lua b/data/computercraft/lua/rom/programs/peripherals.lua index 06f6f56..f099c63 100644 --- a/data/computercraft/lua/rom/programs/peripherals.lua +++ b/data/computercraft/lua/rom/programs/peripherals.lua @@ -20,7 +20,7 @@ term.at(1, 2) local peripheral = require("peripheral") term.setTextColor(colors.yellow) -print("已连接的外设") +print("Connected Peripherals:") term.setTextColor(colors.white) local names = peripheral.getNames() diff --git a/data/computercraft/lua/rom/programs/shell.lua b/data/computercraft/lua/rom/programs/shell.lua index 9f2cfd6..1cd5967 100644 --- a/data/computercraft/lua/rom/programs/shell.lua +++ b/data/computercraft/lua/rom/programs/shell.lua @@ -3,12 +3,14 @@ -- 程序顶部名称栏 local term = require("term") local colors = require("colors") +local window = require("window") -- 保存当前颜色设置 local old_fg = term.getTextColor() local old_bg = term.getBackgroundColor() -- 设置名称栏颜色并显示 + term.setCursorPos(1, 1) term.setTextColor(colors.white) term.setBackgroundColor(colors.cyan) term.at(1, 1).clearLine() @@ -100,9 +102,99 @@ while true do end term.at(1, 2) + -- 定义错误处理函数 + local function showErrorWindow(errorMessage, command) + local w, h = term.getSize() + local winWidth, winHeight = 50, 12 + local x = math.floor((w - winWidth) / 2) + local y = math.floor((h - winHeight) / 2) + + -- 创建错误窗口 + local errWindow = window.create(term.native(), x, y, winWidth, winHeight) + errWindow.setVisible(true) + + -- 绘制窗口边框和标题 + errWindow.setBackgroundColor(colors.red) + errWindow.setTextColor(colors.white) + errWindow.at(1, 1).clearLine() + errWindow.at(1, 1).write("=== 程序错误 ===") + + -- 绘制窗口内容背景 + errWindow.setBackgroundColor(colors.black) + for line=2, winHeight-1 do + errWindow.at(1, line).clearLine() + end + + -- 显示错误信息(自动换行) + errWindow.setTextColor(colors.red) + errWindow.at(2, 2).write("错误信息:") + errWindow.setTextColor(colors.white) + + local lineNum = 3 + local maxLineLength = winWidth - 4 + local messageLines = {} + local currentLine = "" + + for word in errorMessage:gmatch("[^\s]+") do + if #currentLine + #word + 1 > maxLineLength then + table.insert(messageLines, currentLine) + currentLine = word + else + if currentLine == "" then + currentLine = word + else + currentLine = currentLine .. " " .. word + end + end + end + if currentLine ~= "" then + table.insert(messageLines, currentLine) + end + + for _, line in ipairs(messageLines) do + if lineNum < winHeight - 2 then + errWindow.at(2, lineNum).write(line) + lineNum = lineNum + 1 + else + errWindow.at(2, lineNum).write("...") + break + end + end + + -- 绘制按钮 + errWindow.setBackgroundColor(colors.gray) + errWindow.setTextColor(colors.white) + errWindow.at(5, winHeight-1).write(" 确认 ") + errWindow.at(30, winHeight-1).write(" 重新运行 ") + + -- 处理用户输入 + while true do + local event, button, xPos, yPos = os.pullEvent("mouse_click") + if yPos == winHeight-1 + y - 1 then + -- 点击确认按钮 + if xPos >= x + 5 and xPos <= x + 12 then + errWindow.setVisible(false) + break + end + -- 点击重新运行按钮 + if xPos >= x + 30 and xPos <= x + 42 then + errWindow.setVisible(false) + return true + end + end + end + return false + end + local ok, err = shell.run(text) if not ok and err then - io.stderr:write("Application has a error when running and system has stop it. Error:\n", err, "\n") + local shouldRetry = showErrorWindow(err, text) + if shouldRetry then + history[#history] = nil -- 移除当前命令,避免历史记录重复 + term.at(1, 2).clearLine() + goto continue -- 重新运行命令 + end end + ::continue:: end end \ No newline at end of file diff --git a/installer.lua b/installer.lua index 7deabb4..b366c7a 100644 --- a/installer.lua +++ b/installer.lua @@ -1,5 +1,5 @@ -- LeonOS installer -local INSTALLER_VERSION = "0.2.5" +local INSTALLER_VERSION = "0.2.6" local DEFAULT_ROM_DIR = "/leonos" print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")