Files
Leonmmcoset fb33b53b42 feat(包管理器): 实现LeonOS包管理系统基础功能
添加包管理器核心程序(pkg.lua)及相关支持文件
- 实现包安装、更新、移除、列表、搜索等功能
- 添加包元数据结构和本地存储管理
- 包含示例包和命令补全支持
- 更新系统版本至0.3.0
2025-09-01 22:03:22 +08:00

89 lines
2.3 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 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")