1
0
mirror of https://github.com/20kdc/OC-KittenOS.git synced 2024-11-23 10:58:06 +11:00

A bit of terminal polish

This commit is contained in:
20kdc 2020-04-02 01:52:25 +01:00
parent a585ce4a75
commit 7d1f6d2cae
2 changed files with 105 additions and 48 deletions

View File

@ -32,45 +32,61 @@ event.listen("k.procdie", function (_, _, pid)
end
end)
TERM.write("KittenOS NEO Lua Shell\r\n")
TERM.write(([[
KittenOS NEO Shell Usage Notes
print = function (...)
local n = {}
local s = {...}
for i = 1, #s do
local v = s[i]
if v == nil then
v = "nil"
Prefixing = is an alias for 'return '.
io.read(): Reads a line.
print: 'print with table dumping' impl.
TERM: Your terminal. (see us-termi doc.)
os.execute(): launch terminal apps!
tries '*', 'sys-t-*', 'svc-t-*', 'app-*'
example: os.execute("luashell")
os.exit(): quit the shell
=listCmdApps(): -t- (terminal) apps
event: useful for setting up listeners
without breaking shell functionality
]]):gsub("[\r]*\n", "\r\n"))
function listCmdApps()
local apps = {}
for _, v in ipairs(neo.listApps()) do
if v:sub(4, 6) == "-t-" then
table.insert(apps, v)
end
table.insert(n, tostring(v))
end
TERM.write(table.concat(n, " ") .. "\r\n")
return apps
end
run = function (x, ...)
local subPid = neo.executeAsync(x, ...)
if not subPid then
subPid = neo.executeAsync("sys-t-" .. x, TERM.id, ...)
end
if not subPid then
subPid = neo.executeAsync("svc-t-" .. x, TERM.id, ...)
end
if not subPid then
subPid = neo.executeAsync("app-" .. x, TERM.id, ...)
end
if not subPid then
error("cannot find " .. x)
end
while true do
local e = {event.pull()}
if e[1] == "k.procdie" then
if e[3] == subPid then
return
local function vPrint(slike, ...)
local s = {...}
if #s > 1 then
for i = 1, #s do
if i ~= 1 then TERM.write("\t") end
vPrint(slike, s[i])
end
elseif slike and type(s[1]) == "string" then
TERM.write("\"" .. s[1] .. "\"")
elseif type(s[1]) ~= "table" then
TERM.write(tostring(s[1]))
else
TERM.write("{")
for k, v in pairs(s[1]) do
TERM.write("[")
vPrint(true, k)
TERM.write("] = ")
vPrint(true, v)
TERM.write(", ")
end
TERM.write("}")
end
end
print = function (...)
vPrint(false, ...)
TERM.write("\r\n")
end
local ioBuffer = ""
io = {
@ -98,10 +114,34 @@ os = setmetatable({}, {
__index = originalOS
})
os.exit = function ()
function os.exit()
alive = false
end
function os.execute(x, ...)
local subPid = neo.executeAsync(x, TERM.id, ...)
if not subPid then
subPid = neo.executeAsync("sys-t-" .. x, TERM.id, ...)
end
if not subPid then
subPid = neo.executeAsync("svc-t-" .. x, TERM.id, ...)
end
if not subPid then
subPid = neo.executeAsync("app-" .. x, TERM.id, ...)
end
if not subPid then
error("cannot find " .. x)
end
while true do
local e = {event.pull()}
if e[1] == "k.procdie" then
if e[3] == subPid then
return
end
end
end
end
while alive do
TERM.write("> ")
local code = io.read()

View File

@ -127,6 +127,10 @@ local function writeData(data)
local char = unicode.sub(data, 1, 1)
data = unicode.sub(data, 2)
-- handle character
if char == "\t" then
-- not ideal, but allowed
char = " "
end
if char == "\r" then
conCX = 1
elseif char == "\n" then
@ -254,21 +258,45 @@ do
end
end
local control = false
local function key(a, c)
if control then
if e[5] == 203 and conW > 8 then
setSize(conW - 1, #console)
return
elseif e[5] == 200 and #console > 1 then
setSize(conW, #console - 1)
return
elseif e[5] == 205 then
setSize(conW + 1, #console)
return
elseif e[5] == 208 then
setSize(conW, #console + 1)
return
end
end
-- so with the reserved ones dealt with...
if not leText then
-- Line Editing not active.
-- For now support a bare minimum.
for _, v in pairs(sendSigs) do
if a == "\r" then
if control then
if a == 99 then
v("telnet", "\xFF\xF4")
end
elseif a == "\r" then
v("data", "\r\n")
elseif a then
v("data", a)
end
end
else
-- Line Editing active
elseif not control then
-- Line Editing active and control isn't involved
if c == 200 or c == 208 then
-- History cursor up (history down)
leText = leHistory[#leHistory]
leCX = unicode.len(leText)
leCX = unicode.len(leText) + 1
if c == 208 then
cycleHistoryUp()
else
@ -296,7 +324,6 @@ local function key(a, c)
end
end
local control = false
while not closeNow do
local e = {coroutine.yield()}
if e[1] == "k.procdie" then
@ -319,18 +346,8 @@ while not closeNow do
if e[5] == 29 or e[5] == 157 then
control = e[6]
elseif e[6] then
if not control then
key(e[4] ~= 0 and unicode.char(e[4]), e[5])
draw(#console + 1)
elseif e[5] == 203 and sW > 8 then
setSize(conW - 1, #console)
elseif e[5] == 200 and #console > 1 then
setSize(conW, #console - 1)
elseif e[5] == 205 then
setSize(conW + 1, #console)
elseif e[5] == 208 then
setSize(conW, #console + 1)
end
end
elseif e[3] == "line" then
draw(e[4])