Fix lua prompt and add arrow key support

This commit is contained in:
Ryan T
2026-02-16 20:37:13 -05:00
parent ecef2c6cb0
commit 371954373e
5 changed files with 220 additions and 51 deletions

View File

@@ -71,7 +71,7 @@ builtinCmds.cd = function(path)
end end
function getUserInput() local function getUserInput()
syscall.devctl(1,"sfgc",3) syscall.devctl(1,"sfgc",3)
printInline(userhost) printInline(userhost)
syscall.devctl(1,"sfgc",1) syscall.devctl(1,"sfgc",1)
@@ -92,15 +92,15 @@ function getUserInput()
while true do while true do
local key=syscall.read(0) local key=syscall.read(0)
if key then if key then
if key == "[" then --TODO: REPLACE WITH LEFT ARROW if key == "\19" then --TODO: REPLACE WITH LEFT ARROW
if cursorPos > 1 then if cursorPos > 1 then
cursorPos = cursorPos - 1 cursorPos = cursorPos - 1
end end
elseif key == "\\" then --TODO: REPLACE WITH RIGHT ARROW elseif key == "\20" then --TODO: REPLACE WITH RIGHT ARROW
if cursorPos <= #input then if cursorPos <= #input then
cursorPos = cursorPos + 1 cursorPos = cursorPos + 1
end end
elseif key == "=" then --TODO: REPLACE WITH UP ARROW elseif key == "\17" then --TODO: REPLACE WITH UP ARROW
if history < #commandHistory then if history < #commandHistory then
syscall.devctl(1,"spos",curOffsetX,curOffsetY) syscall.devctl(1,"spos",curOffsetX,curOffsetY)
printInline((" "):rep(#input + 1)) printInline((" "):rep(#input + 1))
@@ -108,7 +108,7 @@ function getUserInput()
input = commandHistory[#commandHistory - history + 1] input = commandHistory[#commandHistory - history + 1]
cursorPos = #input + 1 cursorPos = #input + 1
end end
elseif key == "]" then --TODO: REPLACE WITH DOWN ARROW elseif key == "\18" then --TODO: REPLACE WITH DOWN ARROW
if history > 1 then if history > 1 then
syscall.devctl(1,"spos",curOffsetX,curOffsetY) syscall.devctl(1,"spos",curOffsetX,curOffsetY)
printInline((" "):rep(#input + 1)) printInline((" "):rep(#input + 1))
@@ -172,7 +172,7 @@ function getUserInput()
end end
end end
function runCommand(command) local function runCommand(command)
terminate = false terminate = false
local args = string.split(command, " ") local args = string.split(command, " ")
if builtinCmds[args[1]] then if builtinCmds[args[1]] then
@@ -248,6 +248,7 @@ function runCommand(command)
if terminate then if terminate then
local success, err = syscall.kill(proc) local success, err = syscall.kill(proc)
if success then if success then
syscall.devctl(1,"sbgc",16)
syscall.devctl(1,"sfgc",2) syscall.devctl(1,"sfgc",2)
print("\nProgram Terminated.") print("\nProgram Terminated.")
syscall.devctl(1,"sfgc",1) syscall.devctl(1,"sfgc",1)

View File

@@ -1,7 +1,22 @@
local args = {...} local args = {...}
local file = syscall.open(args[1], "r") local name = syscall.getTask(syscall.getpid()).name
local fs = require("sys.fs")
local filePath = (args[1] or "")
if filePath:sub(1, 1) ~= "/" then
filePath = syscall.getcwd().."/"..filePath
end
if not fs.exists(filePath) and args[1] then
print(name..": Cannot access '"..args[1].."': No such file.")
return
end
local file = 0
if args[1] then
file = syscall.open(filePath, "r")
end
local content="" local content=""
while content~=nil do while content~=nil or file == 0 do
content=syscall.read(file, 1024) content=syscall.read(file, 1024)
printInline(content) printInline(content)
end end

View File

@@ -1,50 +1,149 @@
--:Minify:-- syscall.devctl(1,"sfgc",7)
print("HyperionOS lua") print("HyperionOS Lua prompt.")
local str="" print("Call exit() to exit.")
local stopInput=false
local timeout=false local commandHistory = {}
local luaEnv=setmetatable({},{__index=_ENV}) local luaEnv=setmetatable({
printInline("> ") ["exit"] = setmetatable({}, {
while true do __tostring = function() return "Call exit() to exit." end,
local input=syscall.read(0) __call = function() syscall.exit() end,
if input then }),
if input=="\b" then ["_echo"] = function(...)
if #str>0 then return ...
str=str:sub(1,#str-1) end,
printInline("\b") },{__index=_ENV})
end
elseif input=="\n" then local function getUserInput()
print("") syscall.devctl(1,"sfgc",1)
stopInput=true printInline("lua> ")
if str == "" then local curOffsetStr = syscall.devctl(1, "gpos")
printInline("> ") local curOffsetX = tonumber(curOffsetStr:sub(1, curOffsetStr:find(";")-1))
stopInput=false local curOffsetY = tonumber(curOffsetStr:sub(curOffsetStr:find(";")+1))
elseif str == "exit()" then
break local input = ""
else local blinkState = false
local func=load(str,"@Lua","t",luaEnv) local cursorPos = 1
local ok,err = xpcall(func, debug.traceback) local history = 0
if not ok then
print(err) while true do
local key=syscall.read(0)
if key then
if key == "\19" then --TODO: REPLACE WITH LEFT ARROW
if cursorPos > 1 then
cursorPos = cursorPos - 1
end end
printInline("\n> ") elseif key == "\20" then --TODO: REPLACE WITH RIGHT ARROW
str="" if cursorPos <= #input then
cursorPos = cursorPos + 1
end
elseif key == "\17" then --TODO: REPLACE WITH UP ARROW
if history < #commandHistory then
syscall.devctl(1,"spos",curOffsetX,curOffsetY)
printInline((" "):rep(#input + 1))
history = history + 1
input = commandHistory[#commandHistory - history + 1]
cursorPos = #input + 1
end
elseif key == "\18" then --TODO: REPLACE WITH DOWN ARROW
if history > 1 then
syscall.devctl(1,"spos",curOffsetX,curOffsetY)
printInline((" "):rep(#input + 1))
history = history - 1
input = commandHistory[#commandHistory - history + 1]
cursorPos = #input + 1
elseif history == 1 then
syscall.devctl(1,"spos",curOffsetX,curOffsetY)
printInline((" "):rep(#input + 1))
history = 0
input = ""
cursorPos = 1
end
elseif key == "\b" then
if cursorPos > 1 then
syscall.devctl(1,"spos",curOffsetX,curOffsetY)
printInline((" "):rep(#input + 1))
input = string.sub(input, 1, cursorPos-2)..string.sub(input, cursorPos)
cursorPos = cursorPos - 1
end
elseif key == "\n" then
syscall.devctl(1,"spos",curOffsetX,curOffsetY)
print(input.." ")
return input
else
input = string.sub(input, 1, cursorPos-1)..key..string.sub(input, cursorPos)
cursorPos = cursorPos + 1
end
local screenSizeStr = syscall.devctl(1, "size")
local sizeX = tonumber(screenSizeStr:sub(1, screenSizeStr:find(";")-1))
local sizeY = tonumber(screenSizeStr:sub(screenSizeStr:find(";")+1))
local totalChars = sizeX * sizeY
local eocCharNum = ((curOffsetY - 1) * sizeX) + curOffsetX + #input
if eocCharNum >= totalChars then
syscall.devctl(1,"spos",sizeX,sizeY)
printInline(" ")
curOffsetY = curOffsetY - 1
end end
str=""
else
str=str..input
printInline(input)
end end
timeout=false syscall.devctl(1,"spos",curOffsetX,curOffsetY)
else printInline(string.sub(input, 1, cursorPos-1))
timeout=true if blinkState then
syscall.devctl(1,"sfgc",16)
syscall.devctl(1,"sbgc",1)
end
if cursorPos > #input then
printInline(" ")
else
printInline(string.sub(input, cursorPos, cursorPos))
end
syscall.devctl(1,"sfgc",1)
syscall.devctl(1,"sbgc",16)
printInline(string.sub(input, cursorPos+1))
if cursorPos <= #input then
printInline(" ")
end
local curBlink = ((math.floor(syscall.getUptime() / 500) % 2) == 0)
if curBlink ~= blinkState then
blinkState = curBlink
end
end
end
local function runCode(code)
local func, err = load(code, "@lua", "t", luaEnv)
local isReturn = false
if load("return "..code) then
func, err = load("return _echo("..code.."\n)", "@lua", "t", luaEnv)
isReturn = true
end
if not func then
local errSL = string.sub(err, string.find(err, ":") + 1)
syscall.devctl(1,"sfgc",2)
printInline("@lua: Load error on line ")
print(string.sub(errSL, 1, string.find(errSL, ":") - 1))
syscall.devctl(1,"sfgc",1)
print(string.sub(errSL, string.find(errSL, ":") + 1))
return
end end
if timeout then local success, msg = xpcall(func, debug.traceback)
if stopInput then if not success then
sleep(.5) local errSL = string.sub(msg, string.find(msg, ":") + 1)
else syscall.devctl(1,"sfgc",2)
sleep(.05) printInline("@lua: Runtime error on line ")
print(string.sub(errSL, 1, string.find(errSL, ":") - 1))
syscall.devctl(1,"sfgc",1)
print(string.sub(errSL, string.find(errSL, ":") + 1))
elseif isReturn then
print(tostring(msg))
end
end
while true do
local code = getUserInput()
if code ~= "" then
if code ~= commandHistory[#commandHistory] then
table.insert(commandHistory, code)
end end
runCode(code)
end end
end end

View File

@@ -0,0 +1,50 @@
--:Minify:--
print("HyperionOS lua")
local str=""
local stopInput=false
local timeout=false
local luaEnv=setmetatable({},{__index=_ENV})
printInline("> ")
while true do
local input=syscall.read(0)
if input then
if input=="\b" then
if #str>0 then
str=str:sub(1,#str-1)
printInline("\b")
end
elseif input=="\n" then
print("")
stopInput=true
if str == "" then
printInline("> ")
stopInput=false
elseif str == "exit()" then
break
else
local func=load(str,"@Lua","t",luaEnv)
local ok,err = xpcall(func, debug.traceback)
if not ok then
print(err)
end
printInline("\n> ")
str=""
end
str=""
else
str=str..input
printInline(input)
end
timeout=false
else
timeout=true
end
if timeout then
if stopInput then
sleep(.5)
else
sleep(.05)
end
end
end

View File

@@ -107,7 +107,11 @@ local ok, err = xpcall(function()
local acekeys={ local acekeys={
[apis.keys.enter]="\n", [apis.keys.enter]="\n",
[apis.keys.tab]="\t", [apis.keys.tab]="\t",
[apis.keys.backspace]="\b" [apis.keys.backspace]="\b",
[apis.keys.up]="\17",
[apis.keys.down]="\18",
[apis.keys.left]="\19",
[apis.keys.right]="\20",
} }
function sleep(time) function sleep(time)