mirror of
https://github.com/CCLeonOS/LeonOS.git
synced 2026-03-03 15:17:01 +00:00
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 和命令补全系统 - 多标签终端支持 - 设置管理系统
This commit is contained in:
36
data/computercraft/lua/rom/modules/main/rc/copy.lua
Normal file
36
data/computercraft/lua/rom/modules/main/rc/copy.lua
Normal file
@@ -0,0 +1,36 @@
|
||||
-- rc.copy - table copier
|
||||
|
||||
-- from https://lua-users.org/wiki/CopyTable
|
||||
local function deepcopy(orig, copies, dont_copy)
|
||||
copies = copies or {}
|
||||
local orig_type = type(orig)
|
||||
local copy
|
||||
|
||||
if orig_type == 'table' and not dont_copy[orig] then
|
||||
if copies[orig] then
|
||||
copy = copies[orig]
|
||||
else
|
||||
copy = {}
|
||||
copies[orig] = copy
|
||||
|
||||
for orig_key, orig_value in next, orig, nil do
|
||||
copy[deepcopy(orig_key, copies, dont_copy)] = deepcopy(orig_value,
|
||||
copies, dont_copy)
|
||||
end
|
||||
|
||||
setmetatable(copy, deepcopy(getmetatable(orig), copies, dont_copy))
|
||||
end
|
||||
else -- number, string, boolean, etc
|
||||
copy = orig
|
||||
end
|
||||
|
||||
return copy
|
||||
end
|
||||
|
||||
return { copy = function(original, ...)
|
||||
local dont_copy = {}
|
||||
for _, thing in pairs({...}) do
|
||||
dont_copy[thing] = true
|
||||
end
|
||||
return deepcopy(original, nil, dont_copy)
|
||||
end }
|
||||
231
data/computercraft/lua/rom/modules/main/rc/io.lua
Normal file
231
data/computercraft/lua/rom/modules/main/rc/io.lua
Normal file
@@ -0,0 +1,231 @@
|
||||
-- rc.io
|
||||
|
||||
local expect = require("cc.expect").expect
|
||||
local thread = require("rc.thread")
|
||||
local colors = require("colors")
|
||||
local term = require("term")
|
||||
local fs = require("fs")
|
||||
local rc = require("rc")
|
||||
|
||||
local io = {}
|
||||
|
||||
local _file = {}
|
||||
function _file:read(...)
|
||||
local args = table.pack(...)
|
||||
local ret = {}
|
||||
|
||||
if args.n == 0 then
|
||||
args[1] = "l"
|
||||
args.n = 1
|
||||
end
|
||||
|
||||
if not (self.handle.read and pcall(self.handle.read, 0)) then
|
||||
return nil, "bad file descriptor"
|
||||
end
|
||||
|
||||
if self.handle.flush then self.handle.flush() end
|
||||
|
||||
for i=1, args.n, 1 do
|
||||
local format = args[i]
|
||||
if format:sub(1,1) == "*" then
|
||||
format = format:sub(2)
|
||||
end
|
||||
|
||||
if format == "a" then
|
||||
ret[#ret+1] = self.handle.readAll()
|
||||
|
||||
elseif format == "l" or format == "L" then
|
||||
ret[#ret+1] = self.handle.readLine(format == "L")
|
||||
|
||||
elseif type(format) == "number" then
|
||||
ret[#ret+1] = self.handle.read(format)
|
||||
|
||||
else
|
||||
error("invalid format '"..format.."'", 2)
|
||||
end
|
||||
end
|
||||
|
||||
return table.unpack(ret, 1, args.n)
|
||||
end
|
||||
|
||||
function _file:lines(...)
|
||||
local formats = {...}
|
||||
if #formats == 0 then
|
||||
formats[1] = "l"
|
||||
end
|
||||
return function()
|
||||
return self:read(table.unpack(formats))
|
||||
end
|
||||
end
|
||||
|
||||
function _file:write(...)
|
||||
local args = table.pack(...)
|
||||
|
||||
if not (self.handle.write and pcall(self.handle.write, "")) then
|
||||
return nil, "bad file descriptor"
|
||||
end
|
||||
|
||||
for i=1, args.n, 1 do
|
||||
self.handle.write(args[i])
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
function _file:seek(whence, offset)
|
||||
expect(1, whence, "string", "nil")
|
||||
expect(2, offset, "number", "nil")
|
||||
if self.handle.seek then
|
||||
return self.handle.seek(whence, offset)
|
||||
else
|
||||
return nil, "bad file descriptor"
|
||||
end
|
||||
end
|
||||
|
||||
function _file:flush()
|
||||
if self.handle.flush then self.handle.flush() end
|
||||
return self
|
||||
end
|
||||
|
||||
function _file:close()
|
||||
self.closed = true
|
||||
pcall(self.handle.close)
|
||||
end
|
||||
|
||||
local function iofile(handle)
|
||||
return setmetatable({handle = handle, closed = false}, {__index = _file})
|
||||
end
|
||||
|
||||
local stdin_rbuf = ""
|
||||
io.stdin = iofile {
|
||||
read = function(n)
|
||||
while #stdin_rbuf < n do
|
||||
stdin_rbuf = stdin_rbuf .. term.read() .. "\n"
|
||||
end
|
||||
local ret = stdin_rbuf:sub(1, n)
|
||||
stdin_rbuf = stdin_rbuf:sub(#ret+1)
|
||||
return ret
|
||||
end,
|
||||
|
||||
readLine = function(trail)
|
||||
local nl = stdin_rbuf:find("\n")
|
||||
|
||||
if nl then
|
||||
local ret = stdin_rbuf:sub(1, nl+1)
|
||||
if not trail then ret = ret:sub(1, -2) end
|
||||
stdin_rbuf = stdin_rbuf:sub(#ret+1)
|
||||
return ret
|
||||
|
||||
else
|
||||
return stdin_rbuf .. term.read() .. (trail and "\n" or "")
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
io.stdout = iofile {
|
||||
write = rc.write
|
||||
}
|
||||
|
||||
io.stderr = iofile {
|
||||
write = function(text)
|
||||
local old = term.getTextColor()
|
||||
term.setTextColor(colors.red)
|
||||
rc.write(text)
|
||||
term.setTextColor(old)
|
||||
end
|
||||
}
|
||||
|
||||
function io.open(file, mode)
|
||||
expect(1, file, "string")
|
||||
expect(2, mode, "string", "nil")
|
||||
|
||||
mode = (mode or "r"):match("[rwa]") .. "b"
|
||||
|
||||
local handle, err = fs.open(file, mode)
|
||||
if not handle then
|
||||
return nil, err
|
||||
end
|
||||
|
||||
return iofile(handle)
|
||||
end
|
||||
|
||||
function io.input(file)
|
||||
expect(1, file, "string", "table", "nil")
|
||||
local vars = thread.vars()
|
||||
if type(file) == "string" then file = assert(io.open(file, "r")) end
|
||||
if file then vars.input = file end
|
||||
return vars.input or io.stdin
|
||||
end
|
||||
|
||||
function io.output(file)
|
||||
expect(1, file, "string", "table", "nil")
|
||||
local vars = thread.vars()
|
||||
if type(file) == "string" then file = assert(io.open(file, "w")) end
|
||||
if file then vars.output = file end
|
||||
return vars.output or io.stdout
|
||||
end
|
||||
|
||||
function io.read(...)
|
||||
return io.input():read(...)
|
||||
end
|
||||
|
||||
function io.write(...)
|
||||
return io.output():write(...)
|
||||
end
|
||||
|
||||
function io.flush(file)
|
||||
expect(1, file, "table", "nil")
|
||||
return (file or io.output):flush()
|
||||
end
|
||||
|
||||
function io.close(file)
|
||||
expect(1, file, "table", "nil")
|
||||
return (file or io.output):close()
|
||||
end
|
||||
|
||||
function io.lines(file, ...)
|
||||
expect(1, file, "string", "nil")
|
||||
if file then file = assert(io.open(file, "r")) end
|
||||
local formats = table.pack(...)
|
||||
return (file or io.stdin):lines(table.unpack(formats, 1, formats.n))
|
||||
end
|
||||
|
||||
function io.type(obj)
|
||||
if type(obj) == "table" then
|
||||
local is_file = true
|
||||
for k, v in pairs(_file) do
|
||||
if (not obj[k]) or v ~= obj[k] then
|
||||
is_file = false
|
||||
end
|
||||
end
|
||||
|
||||
if is_file then
|
||||
return obj.closed and "closed file" or "file"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- loadfile and dofile here as well
|
||||
function _G.loadfile(file, mode, env)
|
||||
expect(1, file, "string")
|
||||
expect(2, mode, "string", "nil")
|
||||
expect(3, env, "table", "nil")
|
||||
local handle, err = io.open(file, "r")
|
||||
if not handle then
|
||||
return nil, file .. ": " .. err
|
||||
end
|
||||
local data = handle:read("a")
|
||||
handle:close()
|
||||
return load(data, "="..file, mode or "bt", env)
|
||||
end
|
||||
|
||||
function _G.dofile(file, ...)
|
||||
expect(1, file, "string")
|
||||
local func, err = loadfile(file)
|
||||
if not func then
|
||||
error(err)
|
||||
end
|
||||
return func(...)
|
||||
end
|
||||
|
||||
return io
|
||||
388
data/computercraft/lua/rom/modules/main/rc/json.lua
Normal file
388
data/computercraft/lua/rom/modules/main/rc/json.lua
Normal file
@@ -0,0 +1,388 @@
|
||||
--
|
||||
-- json.lua
|
||||
--
|
||||
-- Copyright (c) 2020 rxi
|
||||
--
|
||||
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
-- this software and associated documentation files (the "Software"), to deal in
|
||||
-- the Software without restriction, including without limitation the rights to
|
||||
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
-- of the Software, and to permit persons to whom the Software is furnished to do
|
||||
-- so, subject to the following conditions:
|
||||
--
|
||||
-- The above copyright notice and this permission notice shall be included in all
|
||||
-- copies or substantial portions of the Software.
|
||||
--
|
||||
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
-- SOFTWARE.
|
||||
--
|
||||
|
||||
local json = { _version = "0.1.2" }
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Encode
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
local encode
|
||||
|
||||
local escape_char_map = {
|
||||
[ "\\" ] = "\\",
|
||||
[ "\"" ] = "\"",
|
||||
[ "\b" ] = "b",
|
||||
[ "\f" ] = "f",
|
||||
[ "\n" ] = "n",
|
||||
[ "\r" ] = "r",
|
||||
[ "\t" ] = "t",
|
||||
}
|
||||
|
||||
local escape_char_map_inv = { [ "/" ] = "/" }
|
||||
for k, v in pairs(escape_char_map) do
|
||||
escape_char_map_inv[v] = k
|
||||
end
|
||||
|
||||
|
||||
local function escape_char(c)
|
||||
return "\\" .. (escape_char_map[c] or string.format("u%04x", c:byte()))
|
||||
end
|
||||
|
||||
|
||||
local function encode_nil(val)
|
||||
return "null"
|
||||
end
|
||||
|
||||
|
||||
local function encode_table(val, stack)
|
||||
local res = {}
|
||||
stack = stack or {}
|
||||
|
||||
-- Circular reference?
|
||||
if stack[val] then error("circular reference") end
|
||||
|
||||
stack[val] = true
|
||||
|
||||
if rawget(val, 1) ~= nil or next(val) == nil then
|
||||
-- Treat as array -- check keys are valid and it is not sparse
|
||||
local n = 0
|
||||
for k in pairs(val) do
|
||||
if type(k) ~= "number" then
|
||||
error("invalid table: mixed or invalid key types")
|
||||
end
|
||||
n = n + 1
|
||||
end
|
||||
if n ~= #val then
|
||||
error("invalid table: sparse array")
|
||||
end
|
||||
-- Encode
|
||||
for i, v in ipairs(val) do
|
||||
table.insert(res, encode(v, stack))
|
||||
end
|
||||
stack[val] = nil
|
||||
return "[" .. table.concat(res, ",") .. "]"
|
||||
|
||||
else
|
||||
-- Treat as an object
|
||||
for k, v in pairs(val) do
|
||||
if type(k) ~= "string" then
|
||||
error("invalid table: mixed or invalid key types")
|
||||
end
|
||||
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
|
||||
end
|
||||
stack[val] = nil
|
||||
return "{" .. table.concat(res, ",") .. "}"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
local function encode_string(val)
|
||||
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
|
||||
end
|
||||
|
||||
|
||||
local function encode_number(val)
|
||||
-- Check for NaN, -inf and inf
|
||||
if val ~= val or val <= -math.huge or val >= math.huge then
|
||||
error("unexpected number value '" .. tostring(val) .. "'")
|
||||
end
|
||||
return string.format("%.14g", val)
|
||||
end
|
||||
|
||||
|
||||
local type_func_map = {
|
||||
[ "nil" ] = encode_nil,
|
||||
[ "table" ] = encode_table,
|
||||
[ "string" ] = encode_string,
|
||||
[ "number" ] = encode_number,
|
||||
[ "boolean" ] = tostring,
|
||||
}
|
||||
|
||||
|
||||
encode = function(val, stack)
|
||||
local t = type(val)
|
||||
local f = type_func_map[t]
|
||||
if f then
|
||||
return f(val, stack)
|
||||
end
|
||||
error("unexpected type '" .. t .. "'")
|
||||
end
|
||||
|
||||
|
||||
function json.encode(val)
|
||||
return ( encode(val) )
|
||||
end
|
||||
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Decode
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
local parse
|
||||
|
||||
local function create_set(...)
|
||||
local res = {}
|
||||
for i = 1, select("#", ...) do
|
||||
res[ select(i, ...) ] = true
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
local space_chars = create_set(" ", "\t", "\r", "\n")
|
||||
local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",")
|
||||
local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u")
|
||||
local literals = create_set("true", "false", "null")
|
||||
|
||||
local literal_map = {
|
||||
[ "true" ] = true,
|
||||
[ "false" ] = false,
|
||||
[ "null" ] = nil,
|
||||
}
|
||||
|
||||
|
||||
local function next_char(str, idx, set, negate)
|
||||
for i = idx, #str do
|
||||
if set[str:sub(i, i)] ~= negate then
|
||||
return i
|
||||
end
|
||||
end
|
||||
return #str + 1
|
||||
end
|
||||
|
||||
|
||||
local function decode_error(str, idx, msg)
|
||||
local line_count = 1
|
||||
local col_count = 1
|
||||
for i = 1, idx - 1 do
|
||||
col_count = col_count + 1
|
||||
if str:sub(i, i) == "\n" then
|
||||
line_count = line_count + 1
|
||||
col_count = 1
|
||||
end
|
||||
end
|
||||
error( string.format("%s at line %d col %d", msg, line_count, col_count) )
|
||||
end
|
||||
|
||||
|
||||
local function codepoint_to_utf8(n)
|
||||
-- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa
|
||||
local f = math.floor
|
||||
if n <= 0x7f then
|
||||
return string.char(n)
|
||||
elseif n <= 0x7ff then
|
||||
return string.char(f(n / 64) + 192, n % 64 + 128)
|
||||
elseif n <= 0xffff then
|
||||
return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
|
||||
elseif n <= 0x10ffff then
|
||||
return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
|
||||
f(n % 4096 / 64) + 128, n % 64 + 128)
|
||||
end
|
||||
error( string.format("invalid unicode codepoint '%x'", n) )
|
||||
end
|
||||
|
||||
|
||||
local function parse_unicode_escape(s)
|
||||
local n1 = tonumber( s:sub(1, 4), 16 )
|
||||
local n2 = tonumber( s:sub(7, 10), 16 )
|
||||
-- Surrogate pair?
|
||||
if n2 then
|
||||
return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
|
||||
else
|
||||
return codepoint_to_utf8(n1)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
local function parse_string(str, i)
|
||||
local res = ""
|
||||
local j = i + 1
|
||||
local k = j
|
||||
|
||||
while j <= #str do
|
||||
local x = str:byte(j)
|
||||
|
||||
if x < 32 then
|
||||
decode_error(str, j, "control character in string")
|
||||
|
||||
elseif x == 92 then -- `\`: Escape
|
||||
res = res .. str:sub(k, j - 1)
|
||||
j = j + 1
|
||||
local c = str:sub(j, j)
|
||||
if c == "u" then
|
||||
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
|
||||
or str:match("^%x%x%x%x", j + 1)
|
||||
or decode_error(str, j - 1, "invalid unicode escape in string")
|
||||
res = res .. parse_unicode_escape(hex)
|
||||
j = j + #hex
|
||||
else
|
||||
if not escape_chars[c] then
|
||||
decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
|
||||
end
|
||||
res = res .. escape_char_map_inv[c]
|
||||
end
|
||||
k = j + 1
|
||||
|
||||
elseif x == 34 then -- `"`: End of string
|
||||
res = res .. str:sub(k, j - 1)
|
||||
return res, j + 1
|
||||
end
|
||||
|
||||
j = j + 1
|
||||
end
|
||||
|
||||
decode_error(str, i, "expected closing quote for string")
|
||||
end
|
||||
|
||||
|
||||
local function parse_number(str, i)
|
||||
local x = next_char(str, i, delim_chars)
|
||||
local s = str:sub(i, x - 1)
|
||||
local n = tonumber(s)
|
||||
if not n then
|
||||
decode_error(str, i, "invalid number '" .. s .. "'")
|
||||
end
|
||||
return n, x
|
||||
end
|
||||
|
||||
|
||||
local function parse_literal(str, i)
|
||||
local x = next_char(str, i, delim_chars)
|
||||
local word = str:sub(i, x - 1)
|
||||
if not literals[word] then
|
||||
decode_error(str, i, "invalid literal '" .. word .. "'")
|
||||
end
|
||||
return literal_map[word], x
|
||||
end
|
||||
|
||||
|
||||
local function parse_array(str, i)
|
||||
local res = {}
|
||||
local n = 1
|
||||
i = i + 1
|
||||
while 1 do
|
||||
local x
|
||||
i = next_char(str, i, space_chars, true)
|
||||
-- Empty / end of array?
|
||||
if str:sub(i, i) == "]" then
|
||||
i = i + 1
|
||||
break
|
||||
end
|
||||
-- Read token
|
||||
x, i = parse(str, i)
|
||||
res[n] = x
|
||||
n = n + 1
|
||||
-- Next token
|
||||
i = next_char(str, i, space_chars, true)
|
||||
local chr = str:sub(i, i)
|
||||
i = i + 1
|
||||
if chr == "]" then break end
|
||||
if chr ~= "," then decode_error(str, i, "expected ']' or ','") end
|
||||
end
|
||||
return res, i
|
||||
end
|
||||
|
||||
|
||||
local function parse_object(str, i)
|
||||
local res = {}
|
||||
i = i + 1
|
||||
while 1 do
|
||||
local key, val
|
||||
i = next_char(str, i, space_chars, true)
|
||||
-- Empty / end of object?
|
||||
if str:sub(i, i) == "}" then
|
||||
i = i + 1
|
||||
break
|
||||
end
|
||||
-- Read key
|
||||
if str:sub(i, i) ~= '"' then
|
||||
decode_error(str, i, "expected string for key")
|
||||
end
|
||||
key, i = parse(str, i)
|
||||
-- Read ':' delimiter
|
||||
i = next_char(str, i, space_chars, true)
|
||||
if str:sub(i, i) ~= ":" then
|
||||
decode_error(str, i, "expected ':' after key")
|
||||
end
|
||||
i = next_char(str, i + 1, space_chars, true)
|
||||
-- Read value
|
||||
val, i = parse(str, i)
|
||||
-- Set
|
||||
res[key] = val
|
||||
-- Next token
|
||||
i = next_char(str, i, space_chars, true)
|
||||
local chr = str:sub(i, i)
|
||||
i = i + 1
|
||||
if chr == "}" then break end
|
||||
if chr ~= "," then decode_error(str, i, "expected '}' or ','") end
|
||||
end
|
||||
return res, i
|
||||
end
|
||||
|
||||
|
||||
local char_func_map = {
|
||||
[ '"' ] = parse_string,
|
||||
[ "0" ] = parse_number,
|
||||
[ "1" ] = parse_number,
|
||||
[ "2" ] = parse_number,
|
||||
[ "3" ] = parse_number,
|
||||
[ "4" ] = parse_number,
|
||||
[ "5" ] = parse_number,
|
||||
[ "6" ] = parse_number,
|
||||
[ "7" ] = parse_number,
|
||||
[ "8" ] = parse_number,
|
||||
[ "9" ] = parse_number,
|
||||
[ "-" ] = parse_number,
|
||||
[ "t" ] = parse_literal,
|
||||
[ "f" ] = parse_literal,
|
||||
[ "n" ] = parse_literal,
|
||||
[ "[" ] = parse_array,
|
||||
[ "{" ] = parse_object,
|
||||
}
|
||||
|
||||
|
||||
parse = function(str, idx)
|
||||
local chr = str:sub(idx, idx)
|
||||
local f = char_func_map[chr]
|
||||
if f then
|
||||
return f(str, idx)
|
||||
end
|
||||
decode_error(str, idx, "unexpected character '" .. chr .. "'")
|
||||
end
|
||||
|
||||
|
||||
function json.decode(str)
|
||||
if type(str) ~= "string" then
|
||||
error("expected argument of type string, got " .. type(str))
|
||||
end
|
||||
local res, idx = parse(str, next_char(str, 1, space_chars, true))
|
||||
idx = next_char(str, idx, space_chars, true)
|
||||
if idx <= #str then
|
||||
decode_error(str, idx, "trailing garbage")
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
|
||||
return json
|
||||
398
data/computercraft/lua/rom/modules/main/rc/thread.lua
Normal file
398
data/computercraft/lua/rom/modules/main/rc/thread.lua
Normal file
@@ -0,0 +1,398 @@
|
||||
-- New scheduler.
|
||||
-- Tabs are integral to the design of this scheduler; Multishell cannot
|
||||
-- be disabled.
|
||||
|
||||
local rc = require("rc")
|
||||
local fs = require("fs")
|
||||
local keys = require("keys")
|
||||
local term = require("term")
|
||||
local colors = require("colors")
|
||||
local window = require("window")
|
||||
local expect = require("cc.expect")
|
||||
local copy = require("rc.copy").copy
|
||||
|
||||
local getfenv
|
||||
if rc.lua51 then
|
||||
getfenv = rc.lua51.getfenv
|
||||
else
|
||||
getfenv = function() return _ENV or _G end
|
||||
end
|
||||
|
||||
local tabs = { {} }
|
||||
local threads = {}
|
||||
local current, wrappedNative
|
||||
|
||||
local focused = 1
|
||||
|
||||
local api = {}
|
||||
|
||||
function api.launchTab(x, name)
|
||||
expect(1, x, "string", "function")
|
||||
name = expect(2, name, "string", "nil") or tostring(x)
|
||||
|
||||
local newTab = {
|
||||
term = window.create(wrappedNative, 1, 1, wrappedNative.getSize()),
|
||||
id = #tabs + 1
|
||||
}
|
||||
|
||||
tabs[newTab.id] = newTab
|
||||
|
||||
local _f = focused
|
||||
focused = newTab.id
|
||||
local id = (type(x) == "string" and api.load or api.spawn)(x, name)
|
||||
focused = _f
|
||||
|
||||
return newTab.id, id
|
||||
end
|
||||
|
||||
function api.setFocusedTab(f)
|
||||
expect(1, f, "number")
|
||||
if tabs[focused] then focused = f end
|
||||
return not not tabs[f]
|
||||
end
|
||||
|
||||
function api.getFocusedTab()
|
||||
return focused
|
||||
end
|
||||
|
||||
function api.getCurrentTab()
|
||||
return current.tab.id
|
||||
end
|
||||
|
||||
function api.load(file, name)
|
||||
expect(1, file, "string")
|
||||
name = expect(2, name, "string", "nil") or file
|
||||
|
||||
local env = copy(current and current.env or _ENV or _G, package.loaded)
|
||||
|
||||
local func, err = loadfile(file, "t", env)
|
||||
if not func then
|
||||
return nil, err
|
||||
end
|
||||
|
||||
return api.spawn(func, name, tabs[focused])
|
||||
end
|
||||
|
||||
function api.spawn(func, name, _)
|
||||
expect(1, func, "function")
|
||||
expect(2, name, "string")
|
||||
|
||||
local new = {
|
||||
name = name,
|
||||
coro = coroutine.create(function()
|
||||
assert(xpcall(func, debug.traceback))
|
||||
end),
|
||||
vars = setmetatable({}, {__index = current and current.vars}),
|
||||
env = getfenv(func) or _ENV or _G,
|
||||
tab = _ or tabs[focused],
|
||||
id = #threads + 1,
|
||||
dir = current and current.dir or "/"
|
||||
}
|
||||
|
||||
new.tab[new.id] = true
|
||||
threads[new.id] = new
|
||||
|
||||
new.tab.name = name
|
||||
|
||||
return new.id
|
||||
end
|
||||
|
||||
function api.exists(id)
|
||||
expect(1, id, "number")
|
||||
return not not threads[id]
|
||||
end
|
||||
|
||||
function api.id()
|
||||
return current.id
|
||||
end
|
||||
|
||||
function api.dir()
|
||||
return current.dir or "/"
|
||||
end
|
||||
|
||||
function api.setDir(dir)
|
||||
expect(1, dir, "string")
|
||||
|
||||
if not fs.exists(dir) then
|
||||
return nil, "that directory does not exist"
|
||||
|
||||
elseif not fs.isDir(dir) then
|
||||
return nil, "not a directory"
|
||||
end
|
||||
|
||||
current.dir = dir
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function api.vars()
|
||||
return current.vars
|
||||
end
|
||||
|
||||
function api.getTerm()
|
||||
return current and current.tab and current.tab.term or term.native()
|
||||
end
|
||||
|
||||
function api.setTerm(new)
|
||||
if tabs[focused] then
|
||||
local old = tabs[focused].term
|
||||
tabs[focused].term = new
|
||||
return old
|
||||
end
|
||||
end
|
||||
|
||||
local w, h
|
||||
local function getName(tab)
|
||||
local highest = 0
|
||||
|
||||
for k in pairs(tab) do
|
||||
if type(k) == "number" then highest = math.max(highest, k) end
|
||||
end
|
||||
|
||||
return threads[highest] and threads[highest].name or "???"
|
||||
end
|
||||
|
||||
function api.info()
|
||||
local running = {}
|
||||
for i, thread in pairs(threads) do
|
||||
running[#running+1] = { id = i, name = thread.name, tab = thread.tab.id }
|
||||
end
|
||||
|
||||
table.sort(running, function(a,b) return a.id < b.id end)
|
||||
|
||||
return running
|
||||
end
|
||||
|
||||
function api.remove(id)
|
||||
expect(1, id, "number", "nil")
|
||||
threads[id or current.id] = nil
|
||||
end
|
||||
|
||||
local scroll = 0
|
||||
local totalNameLength = 0
|
||||
local function redraw()
|
||||
w, h = wrappedNative.getSize()
|
||||
|
||||
wrappedNative.setVisible(false)
|
||||
|
||||
local names = {}
|
||||
totalNameLength = 0
|
||||
for i=1, #tabs, 1 do
|
||||
names[i] = " " .. getName(tabs[i]) .. " "
|
||||
totalNameLength = totalNameLength + #names[i]
|
||||
end
|
||||
|
||||
if #tabs > 1 then
|
||||
local len = -scroll + 1
|
||||
wrappedNative.setCursorPos(1, 1)
|
||||
wrappedNative.setTextColor(colors.black)
|
||||
wrappedNative.setBackgroundColor(colors.gray)
|
||||
wrappedNative.clearLine()
|
||||
|
||||
for i=1, #tabs, 1 do
|
||||
local tab = tabs[i]
|
||||
local name = names[i]
|
||||
|
||||
wrappedNative.setCursorPos(len, 1)
|
||||
len = len + #name
|
||||
|
||||
if i == focused then
|
||||
wrappedNative.setTextColor(colors.yellow)
|
||||
wrappedNative.setBackgroundColor(colors.black)
|
||||
wrappedNative.write(name)
|
||||
|
||||
else
|
||||
wrappedNative.setTextColor(colors.black)
|
||||
wrappedNative.setBackgroundColor(colors.gray)
|
||||
wrappedNative.write(name)
|
||||
end
|
||||
|
||||
tab.term.setVisible(false)
|
||||
tab.term.reposition(1, 2, w, h - 1)
|
||||
end
|
||||
|
||||
if totalNameLength > w-2 then
|
||||
wrappedNative.setTextColor(colors.black)
|
||||
wrappedNative.setBackgroundColor(colors.gray)
|
||||
if scroll > 0 then
|
||||
wrappedNative.setCursorPos(1, 1)
|
||||
wrappedNative.write("<")
|
||||
end
|
||||
if totalNameLength - scroll > w-1 then
|
||||
wrappedNative.setCursorPos(w, 1)
|
||||
wrappedNative.write(">")
|
||||
end
|
||||
end
|
||||
|
||||
tabs[focused].term.setVisible(true)
|
||||
|
||||
elseif #tabs > 0 then
|
||||
local tab = tabs[1]
|
||||
tab.term.reposition(1, 1, w, h)
|
||||
tab.term.setVisible(true)
|
||||
end
|
||||
|
||||
wrappedNative.setVisible(true)
|
||||
end
|
||||
|
||||
local inputEvents = {
|
||||
key = true,
|
||||
char = true,
|
||||
key_up = true,
|
||||
mouse_up = true,
|
||||
mouse_drag = true,
|
||||
mouse_click = true,
|
||||
mouse_scroll = true,
|
||||
terminate = true,
|
||||
}
|
||||
|
||||
local altIsDown
|
||||
|
||||
local function processEvent(event)
|
||||
if inputEvents[event[1]] then
|
||||
if #event > 3 then -- mouse event
|
||||
|
||||
if #tabs > 1 then
|
||||
if event[4] == 1 then
|
||||
local curX = -scroll
|
||||
|
||||
if event[1] == "mouse_scroll" then
|
||||
scroll = math.max(0, math.min(totalNameLength-w+1,
|
||||
scroll - event[2]))
|
||||
return false
|
||||
end
|
||||
|
||||
for i=1, #tabs, 1 do
|
||||
local tab = tabs[i]
|
||||
curX = curX + #getName(tab) + 2
|
||||
|
||||
if event[3] <= curX then
|
||||
focused = i
|
||||
redraw()
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
|
||||
else
|
||||
event[4] = event[4] - 1
|
||||
end
|
||||
end
|
||||
|
||||
elseif event[1] == "key" then
|
||||
if event[2] == keys.rightAlt then
|
||||
altIsDown = event[2]
|
||||
return false
|
||||
|
||||
elseif altIsDown then
|
||||
local num = tonumber(keys.getName(event[2]))
|
||||
if num then
|
||||
if tabs[num] then
|
||||
focused = num
|
||||
redraw()
|
||||
return false
|
||||
end
|
||||
|
||||
elseif event[2] == keys.left then
|
||||
focused = math.max(1, focused - 1)
|
||||
redraw()
|
||||
return false
|
||||
|
||||
elseif event[2] == keys.right then
|
||||
focused = math.min(#tabs, focused + 1)
|
||||
redraw()
|
||||
return false
|
||||
|
||||
elseif event[2] == keys.up then
|
||||
scroll = math.max(0, math.min(totalNameLength-w+1,
|
||||
scroll + 1))
|
||||
return false
|
||||
|
||||
elseif event[2] == keys.down then
|
||||
scroll = math.max(0, math.min(totalNameLength-w+1,
|
||||
scroll - 1))
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
elseif event[1] == "key_up" then
|
||||
if event[2] == keys.rightAlt then
|
||||
altIsDown = false
|
||||
return false
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
local function cleanTabs()
|
||||
for t=#tabs, 1, -1 do
|
||||
local tab = tabs[t]
|
||||
|
||||
local count, removed = 0, 0
|
||||
for i in pairs(tab) do
|
||||
if type(i) == "number" then
|
||||
count = count + 1
|
||||
if not threads[i] then
|
||||
removed = removed + 1
|
||||
tab[i] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if count == removed then
|
||||
table.remove(tabs, t)
|
||||
end
|
||||
end
|
||||
|
||||
for i=1, #tabs, 1 do
|
||||
tabs[i].id = i
|
||||
end
|
||||
|
||||
focused = math.max(1, math.min(#tabs, focused))
|
||||
end
|
||||
|
||||
function api.start()
|
||||
api.start = nil
|
||||
|
||||
local _native = term.native()
|
||||
wrappedNative = window.create(_native, 1, 1, _native.getSize())
|
||||
api.launchTab("/rc/programs/shell.lua", "shell")
|
||||
|
||||
rc.pushEvent("init")
|
||||
|
||||
while #tabs > 0 and next(threads) do
|
||||
cleanTabs()
|
||||
redraw()
|
||||
local event = table.pack(coroutine.yield())
|
||||
|
||||
if event[1] == "term_resize" then
|
||||
wrappedNative.reposition(1, 1, _native.getSize())
|
||||
end
|
||||
|
||||
if processEvent(event) then
|
||||
for tid, thread in pairs(threads) do
|
||||
if thread.tab == tabs[focused] or not inputEvents[event[1]] then
|
||||
current = thread
|
||||
local result = table.pack(coroutine.resume(thread.coro,
|
||||
table.unpack(event, 1, event.n)))
|
||||
|
||||
if not result[1] then
|
||||
io.stderr:write(result[2].."\n")
|
||||
threads[tid] = nil
|
||||
|
||||
elseif coroutine.status(thread.coro) == "dead" then
|
||||
threads[tid] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
rc.shutdown()
|
||||
end
|
||||
|
||||
return api
|
||||
Reference in New Issue
Block a user