diff --git a/data/computercraft/lua/bios.lua b/data/computercraft/lua/bios.lua index 0ed2c2b..2ac8510 100644 --- a/data/computercraft/lua/bios.lua +++ b/data/computercraft/lua/bios.lua @@ -1,4 +1,4 @@ -_G._HOST = _G._HOST .. " (LeonOS 0.2.6)" +_G._HOST = _G._HOST .. " (LeonOS 0.2.7)" 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 = 6 + patch = 7 }, queueEvent = pull(os, "queueEvent"), startTimer = pull(os, "startTimer"), diff --git a/data/computercraft/lua/rom/completions/calc.lua b/data/computercraft/lua/rom/completions/calc.lua new file mode 100644 index 0000000..2f44d52 --- /dev/null +++ b/data/computercraft/lua/rom/completions/calc.lua @@ -0,0 +1,8 @@ +-- calc command completion +local shell = require("shell") +local completion = require("cc.shell.completion") + +shell.setCompletionFunction("calc", function(shell, args) + -- Calculator doesn't need parameters, return empty list + return {} +end) \ No newline at end of file diff --git a/data/computercraft/lua/rom/programs/calc.lua b/data/computercraft/lua/rom/programs/calc.lua new file mode 100644 index 0000000..a6d400b --- /dev/null +++ b/data/computercraft/lua/rom/programs/calc.lua @@ -0,0 +1,89 @@ +-- Calculator program calc.lua +local term = require("term") +local keys = require("keys") +local colors = require("colors") + +-- Function to draw top bar +local function drawTopBar() + -- Get terminal size + local w, h = term.getSize() + + -- Save cursor position + local cx, cy = term.getCursorPos() + + -- Set top bar color + term.setTextColor(colors.yellow) + term.setBackgroundColor(colors.blue) + + -- Move to top-left corner + term.setCursorPos(1, 1) + + -- Draw top bar with centered title + local title = " LeonOS Calculator " + local padding = math.floor((w - #title) / 2) + term.write(string.rep(" ", padding) .. title .. string.rep(" ", padding)) + + -- Reset colors + term.setTextColor(colors.white) + term.setBackgroundColor(colors.black) + + -- Restore cursor position + term.setCursorPos(cx, cy) +end + +-- Clear screen and show UI +term.clear() +drawTopBar() + +-- Move cursor to below top bar and show instructions +term.setCursorPos(1, 3) +print("Enter an expression, press Enter to calculate (type 'q' to quit)") +print("Supports +, -, *, /, ^(exponent), %(modulus), and parentheses") +print("---------------------------") + +while true do + -- Show prompt + io.write("> ") + local input = io.read() + + -- Check for exit + if input == "q" or input == "quit" then + break + end + + -- Check for clear screen + if input == "clear" then + term.clear() +drawTopBar() + -- Move cursor to below top bar and show instructions + term.setCursorPos(1, 3) + print("Enter an expression, press Enter to calculate (type 'q' to quit)") + print("Supports +, -, *, /, ^(exponent), %(modulus), and parentheses") + print("---------------------------") + goto continue + end + + -- Calculate expression + local success, result = pcall(function() + -- Replace Chinese parentheses with English ones + input = input:gsub("(", "("):gsub(")", ")") + -- Safely execute expression + local func = load("return " .. input) + if func then + return func() + else + error("Invalid expression") + end + end) + + -- Show result + if success then + print("Result: " .. result) + else + print("Error: " .. tostring(result)) + end + + ::continue:: +end + +print("Calculator has exited") \ No newline at end of file diff --git a/data/computercraft/lua/rom/programs/shell.lua b/data/computercraft/lua/rom/programs/shell.lua index 1cd5967..9f2cfd6 100644 --- a/data/computercraft/lua/rom/programs/shell.lua +++ b/data/computercraft/lua/rom/programs/shell.lua @@ -3,14 +3,12 @@ -- 程序顶部名称栏 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() @@ -102,99 +100,9 @@ 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 - local shouldRetry = showErrorWindow(err, text) - if shouldRetry then - history[#history] = nil -- 移除当前命令,避免历史记录重复 - term.at(1, 2).clearLine() - goto continue -- 重新运行命令 - end + io.stderr:write("Application has a error when running and system has stop it. Error:\n", err, "\n") end - ::continue:: end end \ No newline at end of file diff --git a/installer.lua b/installer.lua index b366c7a..10ce940 100644 --- a/installer.lua +++ b/installer.lua @@ -1,5 +1,5 @@ -- LeonOS installer -local INSTALLER_VERSION = "0.2.6" +local INSTALLER_VERSION = "0.2.7" local DEFAULT_ROM_DIR = "/leonos" print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")