From 0ffb590516d8e092d81589f27dc42de38835a099 Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Wed, 3 Sep 2025 15:11:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=8C=85=E7=AE=A1=E7=90=86):=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=88=9B=E5=BB=BA=E6=96=B0=E5=8C=85=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=B9=B6=E6=9B=B4=E6=96=B0=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加pkg init命令用于创建新包,包括生成package.json和主代码文件模板 新增storage命令及相关帮助文档 更新安装器版本号并改进GUI事件处理 --- data/computercraft/lua/rom/apis/lgui.lua | 8 +- data/computercraft/lua/rom/help/pkg.hlp | 83 +++++++++++++++++ data/computercraft/lua/rom/help/storage.hlp | 23 +++++ data/computercraft/lua/rom/programs/pkg.lua | 88 +++++++++++++++++++ .../lua/rom/programs/storage.lua | 61 +++++++++++++ installer.lua | 2 +- 6 files changed, 262 insertions(+), 3 deletions(-) create mode 100644 data/computercraft/lua/rom/help/pkg.hlp create mode 100644 data/computercraft/lua/rom/help/storage.hlp create mode 100644 data/computercraft/lua/rom/programs/storage.lua diff --git a/data/computercraft/lua/rom/apis/lgui.lua b/data/computercraft/lua/rom/apis/lgui.lua index 1bf6f2b..c547805 100644 --- a/data/computercraft/lua/rom/apis/lgui.lua +++ b/data/computercraft/lua/rom/apis/lgui.lua @@ -592,10 +592,14 @@ end function GUIManager:handleEvents() while self.running do - -- Safely get the pullEvent function + -- Safely get the pullEvent function with fallback to rc.pullEvent local pullEventFunc = os.pullEvent if type(pullEventFunc) ~= "function" then - error("os.pullEvent is not a function") + -- Try using rc.pullEvent as fallback + pullEventFunc = rc and rc.pullEvent + if type(pullEventFunc) ~= "function" then + error("Neither os.pullEvent nor rc.pullEvent is available") + end end -- Get the next event diff --git a/data/computercraft/lua/rom/help/pkg.hlp b/data/computercraft/lua/rom/help/pkg.hlp new file mode 100644 index 0000000..4d49a13 --- /dev/null +++ b/data/computercraft/lua/rom/help/pkg.hlp @@ -0,0 +1,83 @@ +=== LeonOS Package Manager === + +The `pkg` command is LeonOS's lightweight package manager, allowing you to install, update, remove, and manage software packages. + +== Basic Usage == + +>>color yellow +pkg [options] +>>color white + +== Available Commands == + +- **install **: Install a package +- **update **: Update a package (leave empty to update all) +- **remove **: Remove a package +- **list**: List all installed packages +- **search **: Search for packages +- **info **: Show package information +- **init **: Create a new package +- **help**: Show help message + +== Command Options == + +The following options are available for most commands: + +- `-f`, `--force`: Force operation (install, update) +- `-l`, `--local`: Install from local file +- `-v`, `--verbose`: Show detailed output +- `-h`, `--help`: Show help information + +== Usage Examples == + +1. Install a package: +>>color yellow +pkg install example-pkg +>>color white + +2. Force install a package: +>>color yellow +pkg install --force example-pkg +>>color white + +3. Update all packages: +>>color yellow +pkg update +>>color white + +4. Remove a package: +>>color yellow +pkg remove example-pkg +>>color white + +5. List installed packages: +>>color yellow +pkg list +>>color white + +6. Search for packages: +>>color yellow +pkg search editor +>>color white + +7. Show package information: +>>color yellow +pkg info example-pkg +>>color white + +8. Create a new package: +>>color yellow +pkg init my-new-pkg +>>color white + +== Package Creation == + +When creating a new package with `pkg init `, the following structure is created: + +/packages/ + / + 1.0.0/ + package.json - Package metadata + .lua - Main package file + +You can edit these files to customize your package before installing it. \ No newline at end of file diff --git a/data/computercraft/lua/rom/help/storage.hlp b/data/computercraft/lua/rom/help/storage.hlp new file mode 100644 index 0000000..5d55930 --- /dev/null +++ b/data/computercraft/lua/rom/help/storage.hlp @@ -0,0 +1,23 @@ +=== Storage Command === + +Description: +Displays storage information for the computer, including total space, used space, +free space, and storage devices. + +Usage: +storage [options] + +Options: +--help, -h Show this help message +--verbose, -v Show detailed storage information + +Examples: + storage + Display basic storage information + + storage --verbose + Display detailed storage information including all drives + +Tips: +- Use the 'delete' command to free up space on your computer. +- Regularly check storage usage to prevent running out of space. \ No newline at end of file diff --git a/data/computercraft/lua/rom/programs/pkg.lua b/data/computercraft/lua/rom/programs/pkg.lua index 7a7b079..af3e011 100644 --- a/data/computercraft/lua/rom/programs/pkg.lua +++ b/data/computercraft/lua/rom/programs/pkg.lua @@ -31,6 +31,86 @@ local pkg_config = { cache_dir = "/packages/cache" -- 缓存目录 } +-- 创建新包 +local function create_package(pkg_name) + print("Creating new package: " .. pkg_name) + + -- 确保包目录存在 + local pkg_version = "1.0.0" + local pkg_dir = fs.combine(pkg_config.local_pkg_dir, pkg_name) + local version_dir = fs.combine(pkg_dir, pkg_version) + if not fs.exists(pkg_dir) then + fs.makeDir(pkg_dir) + end + if not fs.exists(version_dir) then + fs.makeDir(version_dir) + end + + -- 创建package.json文件 + local package_json = { + name = pkg_name, + version = pkg_version, + description = "A new package for LeonOS", + author = "LeonOS User", + license = "MIT", + dependencies = {}, + files = { + pkg_name .. ".lua" + } + } + local json_file = io.open(fs.combine(version_dir, "package.json"), "w") + if json_file then + json_file:write(textutils.serializeJSON(package_json, true)) + json_file:close() + print("Created package.json") + else + print("Error: Failed to create package.json") + return false + end + + -- 创建主代码文件 + local main_file_path = fs.combine(version_dir, pkg_name .. ".lua") + local main_file = io.open(main_file_path, "w") + if main_file then + local main_file_content = [[-- ]] .. pkg_name .. [[ Package +local colors = require('colors') +local term = require('term') + +function drawTopBar() + local w, h = term.getSize() + term.setBackgroundColor(colors.cyan) + term.setTextColor(colors.white) + term.setCursorPos(1, 1) + term.clearLine() + local title = "=== ]] .. pkg_name .. [[ v]] .. pkg_version .. [[ ===" + local pos = math.floor((w - #title) / 2) + 1 + term.setCursorPos(pos, 1) + term.write(title) + term.setBackgroundColor(colors.black) + term.setTextColor(colors.white) + term.setCursorPos(1, 3) +end + +drawTopBar() +print("\nThis is the ]] .. pkg_name .. [[ package for LeonOS.") +print("\nUsage:") +print(" pkg install ]] .. pkg_name .. [[ - Install this package") +print(" pkg remove ]] .. pkg_name .. [[ - Uninstall this package") +print(" pkg list - List installed packages") +]] + main_file:write(main_file_content) + main_file:close() + print("Created ]] .. pkg_name .. [[.lua") + else + print("Error: Failed to create ]] .. pkg_name .. [[.lua") + return false + end + + print("Package created successfully at: " .. version_dir) + print("You can now edit the package files and install it with 'pkg install ]] .. pkg_name .. [['") + return true +end + -- 确保必要的目录存在 local function ensure_dirs() if not fs.exists(pkg_config.local_pkg_dir) then @@ -80,6 +160,7 @@ local function show_help() print(" list List all installed packages") print(" search Search for packages") print(" info Show package information") + print(" init Create a new package") print(" help Show this help message") print("") print("Options:") @@ -378,6 +459,13 @@ local function main(args) else show_package_info(pkg_args[1]) end + elseif command == "init" then + if #pkg_args < 1 then + print("Error: Missing package name.") + show_help() + else + create_package(pkg_args[1]) + end elseif command == "help" then show_help() else diff --git a/data/computercraft/lua/rom/programs/storage.lua b/data/computercraft/lua/rom/programs/storage.lua new file mode 100644 index 0000000..d9f86b3 --- /dev/null +++ b/data/computercraft/lua/rom/programs/storage.lua @@ -0,0 +1,61 @@ +-- LeonOS storage command +local rc = require("rc") +local textutils = require("textutils") +local term = require("term") +local colors = require("colors") +local fs = require("fs") + +-- 保存当前颜色设置 +local old_fg = term.getTextColor() +local old_bg = term.getBackgroundColor() + +-- 设置名称栏颜色并显示 +term.setTextColor(colors.white) +term.setBackgroundColor(colors.cyan) +term.at(1, 1).clearLine() +term.at(1, 1).write("=== Storage Information ===") + +-- 恢复颜色设置 +term.setTextColor(old_fg) +term.setBackgroundColor(old_bg) +term.at(1, 2) + +-- 获取存储信息 +local total_space = fs.getSize("/") +local free_space = fs.getFreeSpace("/") +local used_space = total_space - free_space + +-- 格式化存储容量(转换为MB) +local function formatSize(bytes) + return string.format("%.2f MB", bytes / 1024 / 1024) +end + +-- 显示存储信息 +textutils.coloredPrint(colors.yellow, "Total Space:", colors.white, formatSize(total_space)) +textutils.coloredPrint(colors.yellow, "Used Space:", colors.white, formatSize(used_space)) +textutils.coloredPrint(colors.yellow, "Free Space:", colors.white, formatSize(free_space)) + +-- 显示存储空间使用百分比 +local usage_percent = (used_space / total_space) * 100 +textutils.coloredPrint(colors.yellow, "Usage:", colors.white, string.format("%.1f%%", usage_percent)) + +-- 显示存储设备信息(如果可用) +local function getDriveInfo() + local drives = fs.list("/") + if drives and #drives > 0 then + textutils.coloredPrint(colors.yellow, "Storage Devices:", colors.white) + for _, drive in ipairs(drives) do + if fs.isDir("/" .. drive) and drive ~= "rom" and drive ~= "tmp" then + local drive_size = fs.getSize("/" .. drive) + print(string.format(" - %s: %s", drive, formatSize(drive_size))) + end + end + end +end + +getDriveInfo() + +-- 提示信息 +term.setTextColor(colors.green) +print("\nTip: Use 'delete' command to free up space.") +term.setTextColor(colors.white) \ No newline at end of file diff --git a/installer.lua b/installer.lua index 4da2b88..af3fd29 100644 --- a/installer.lua +++ b/installer.lua @@ -1,5 +1,5 @@ -- LeonOS installer -local INSTALLER_VERSION = "0.3.8 Beta 4" +local INSTALLER_VERSION = "0.3.8 Beta 4 Alpha 1" local DEFAULT_ROM_DIR = "/leonos" print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")