mirror of
https://github.com/CCLeonOS/LeonOS.git
synced 2026-03-03 15:17:01 +00:00
feat: 添加计算器程序并更新版本至0.2.7
添加新的计算器程序calc.lua,包含完整的计算功能和界面 移除shell.lua中的错误窗口功能,改为直接输出错误信息 更新installer.lua和bios.lua中的版本号至0.2.7 添加calc命令的自动补全功能
This commit is contained in:
@@ -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")
|
local fs = rawget(_G, "fs")
|
||||||
|
|
||||||
_G._RC_ROM_DIR = _RC_ROM_DIR or (...) and fs.exists("/leonos") and "/leonos" or "/rom"
|
_G._RC_ROM_DIR = _RC_ROM_DIR or (...) and fs.exists("/leonos") and "/leonos" or "/rom"
|
||||||
@@ -32,7 +32,7 @@ local rc = {
|
|||||||
_VERSION = {
|
_VERSION = {
|
||||||
major = 0,
|
major = 0,
|
||||||
minor = 2,
|
minor = 2,
|
||||||
patch = 6
|
patch = 7
|
||||||
},
|
},
|
||||||
queueEvent = pull(os, "queueEvent"),
|
queueEvent = pull(os, "queueEvent"),
|
||||||
startTimer = pull(os, "startTimer"),
|
startTimer = pull(os, "startTimer"),
|
||||||
|
|||||||
8
data/computercraft/lua/rom/completions/calc.lua
Normal file
8
data/computercraft/lua/rom/completions/calc.lua
Normal file
@@ -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)
|
||||||
89
data/computercraft/lua/rom/programs/calc.lua
Normal file
89
data/computercraft/lua/rom/programs/calc.lua
Normal file
@@ -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")
|
||||||
@@ -3,14 +3,12 @@
|
|||||||
-- 程序顶部名称栏
|
-- 程序顶部名称栏
|
||||||
local term = require("term")
|
local term = require("term")
|
||||||
local colors = require("colors")
|
local colors = require("colors")
|
||||||
local window = require("window")
|
|
||||||
|
|
||||||
-- 保存当前颜色设置
|
-- 保存当前颜色设置
|
||||||
local old_fg = term.getTextColor()
|
local old_fg = term.getTextColor()
|
||||||
local old_bg = term.getBackgroundColor()
|
local old_bg = term.getBackgroundColor()
|
||||||
|
|
||||||
-- 设置名称栏颜色并显示
|
-- 设置名称栏颜色并显示
|
||||||
term.setCursorPos(1, 1)
|
|
||||||
term.setTextColor(colors.white)
|
term.setTextColor(colors.white)
|
||||||
term.setBackgroundColor(colors.cyan)
|
term.setBackgroundColor(colors.cyan)
|
||||||
term.at(1, 1).clearLine()
|
term.at(1, 1).clearLine()
|
||||||
@@ -102,99 +100,9 @@ while true do
|
|||||||
end
|
end
|
||||||
term.at(1, 2)
|
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)
|
local ok, err = shell.run(text)
|
||||||
if not ok and err then
|
if not ok and err then
|
||||||
local shouldRetry = showErrorWindow(err, text)
|
io.stderr:write("Application has a error when running and system has stop it. Error:\n", err, "\n")
|
||||||
if shouldRetry then
|
|
||||||
history[#history] = nil -- 移除当前命令,避免历史记录重复
|
|
||||||
term.at(1, 2).clearLine()
|
|
||||||
goto continue -- 重新运行命令
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
::continue::
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
-- LeonOS installer
|
-- LeonOS installer
|
||||||
local INSTALLER_VERSION = "0.2.6"
|
local INSTALLER_VERSION = "0.2.7"
|
||||||
local DEFAULT_ROM_DIR = "/leonos"
|
local DEFAULT_ROM_DIR = "/leonos"
|
||||||
|
|
||||||
print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")
|
print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")
|
||||||
|
|||||||
Reference in New Issue
Block a user