Files
LeonOS/data/computercraft/lua/rom/modules/main/cc/shell/completion.lua
Leonmmcoset 90a901f58e feat: 初始提交 LeonOS 实现
添加 LeonOS 的基本实现,包括:
- 核心 API 模块(colors, disk, gps, keys, multishell, parallel, rednet, redstone, settings, vector)
- 命令行程序(about, alias, bg, clear, copy, delete, edit, fg, help, list, lua, mkdir, move, paint, peripherals, programs, reboot, set, shutdown, threads)
- 系统启动脚本和包管理
- 文档(README.md, LICENSE)
- 开发工具(devbin)和更新程序

实现功能:
- 完整的线程管理系统
- 兼容 ComputerCraft 的 API 设计
- 改进的 shell 和命令补全系统
- 多标签终端支持
- 设置管理系统
2025-08-31 16:54:18 +08:00

114 lines
2.5 KiB
Lua

-- cc.shell.completion
-- XXX incompatible: functions do not accept a 'shell' argument
local completion = require("cc.completion")
local expect = require("cc.expect").expect
local shell = require("shell")
local fs = require("fs")
local c = {}
function c.file(text)
expect(1, text, "string")
return fs.complete(text, shell.dir(), true, false)
end
function c.dir(text)
expect(1, text, "string")
return fs.complete(text, shell.dir(), false)
end
function c.dirOrFile(text, previous, add_space)
expect(1, text, "string")
expect(2, previous, "table")
expect(3, add_space, "boolean", "nil")
local completed = fs.complete(text, shell.dir(), true, true)
if add_space then
for i=1, #completed, 1 do
completed[i] = completed[i] .. " "
end
end
return completed
end
function c.program(text, add_space)
expect(1, text, "string")
expect(2, add_space, "boolean", "table", "nil")
local progs = shell.programs()
local files = c.file(text)
local seen = {}
for i=1, #files, 1 do
local full = text..files[i]
if fs.isDir(full) and full:sub(-1) ~= "/" then
full = full .. "/"
end
if not seen[full] then
progs[#progs+1] = full
end
seen[full] = true
end
table.sort(progs, function(a,b) return #a < #b end)
return completion.choice(text, progs, add_space)
end
function c.programWithArgs(text, previous, starting)
expect(1, text, "string")
expect(2, previous, "table")
expect(3, starting, "number")
if not previous[starting] then
return shell.completeProgram(text)
end
local command = previous[starting]
command = shell.aliases()[command] or command
local complete = shell.getCompletionInfo()[command]
if complete then
return complete(#previous - starting + 1, text,
{table.unpack(previous, starting)})
end
end
for k,v in pairs(completion) do
c[k] = function(text, _, ...) return v(text, ...) end
end
function c.build(...)
local args = table.pack(...)
for i=1, args.n, 1 do
expect(i, args[i], "function", "table", "nil")
end
return function(index, current, previous)
local complete
if args.n < index then
if type(args[args.n]) == "table" and args[args.n].many then
complete = args[args.n]
end
else
complete = args[index]
end
if not complete then
return {}
end
if type(complete) == "function" then
return complete(current, previous)
elseif type(complete) == "table" then
return complete[1](current, previous, table.unpack(complete, 2))
end
end
end
return c