v0.8.2 - Added arrow keys to navigate shell history as well as a blinking cursor which you cannot yet move.

This commit is contained in:
TheWahlolly
2025-04-27 21:16:50 +03:00
parent f478048976
commit 64a6a26404
4 changed files with 55 additions and 6 deletions
+2 -1
View File
@@ -1,8 +1,9 @@
print("\27[44m".._VERSION.."\27[0m shell") print("\27[44m".._VERSION.."\27[0m shell")
print('Type "exit" to exit.') print('Type "exit" to exit.')
termlib.readHistory["lua"] = {""}
while true do while true do
print("\27[44mlua>\27[0m ", false) print("\27[44mlua>\27[0m ", false)
local command = read() local command = read("lua")
if command == "exit" then if command == "exit" then
return return
else else
+1 -1
View File
@@ -1,7 +1,7 @@
local loadfile = ... local loadfile = ...
local filesystem = loadfile("/halyde/lib/filesystem.lua")(loadfile) local filesystem = loadfile("/halyde/lib/filesystem.lua")(loadfile)
_G._OSVERSION = "Halyde 0.8.1" _G._OSVERSION = "Halyde 0.8.2"
function _G.import(module, ...) function _G.import(module, ...)
local args = table.pack(...) local args = table.pack(...)
+1 -1
View File
@@ -90,6 +90,6 @@ while true do
print(shellcfg["prompt"]:format(shell.workingDirectory),false) print(shellcfg["prompt"]:format(shell.workingDirectory),false)
-- termlib.cursorPosX = #(shell.workingDirectory .. " > ") -- termlib.cursorPosX = #(shell.workingDirectory .. " > ")
-- termlib.cursorPosY = termlib.cursorPosY - 1 -- termlib.cursorPosY = termlib.cursorPosY - 1
local shellCommand = read() local shellCommand = read("shell")
shell.run(shellCommand) shell.run(shellCommand)
end end
+50 -2
View File
@@ -5,6 +5,7 @@ local gpu = component.proxy(component.list("gpu")()) -- replace with component.g
_G.termlib = {} _G.termlib = {}
termlib.cursorPosX = 1 termlib.cursorPosX = 1
termlib.cursorPosY = 1 termlib.cursorPosY = 1
termlib.readHistory = {}
local width, height = gpu.getResolution() local width, height = gpu.getResolution()
termlib.width = width termlib.width = width
@@ -165,17 +166,59 @@ function _G.clear()
termlib.cursorPosX, termlib.cursorPosY = 1, 1 termlib.cursorPosX, termlib.cursorPosY = 1, 1
end end
function _G.read() function _G.read(readHistoryType)
checkArg(1, readHistoryType, "string", "nil")
local curtext = "" local curtext = ""
local RHIndex
if readHistoryType then
if not termlib.readHistory[readHistoryType] then
termlib.readHistory[readHistoryType] = {curtext}
elseif termlib.readHistory[readHistoryType][#termlib.readHistory[readHistoryType]] ~= "" then
table.insert(termlib.readHistory[readHistoryType], curtext)
end
RHIndex = #termlib.readHistory[readHistoryType] -- read history index
end
local cursorPosX, cursorPosY = termlib.cursorPosX, termlib.cursorPosY local cursorPosX, cursorPosY = termlib.cursorPosX, termlib.cursorPosY
local cursorWhite = true local cursorWhite = true
while true do while true do
if cursorWhite then
print("\27[107m ", false)
else
print(" ", false)
end
termlib.cursorPosX = termlib.cursorPosX - 1
local args = {event.pull("key_down", 0.5)} local args = {event.pull("key_down", 0.5)}
if args[4] then if args[4] then
cursorWhite = true
local keycode = args[4] local keycode = args[4]
local key = keyboard.keys[keycode] local key = keyboard.keys[keycode]
if key == "up" and readHistoryType then
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY
print(curtext .. " ", false)
RHIndex = RHIndex - 1
if RHIndex <= 0 then
RHIndex = 1
end
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY
print(termlib.readHistory[readHistoryType][RHIndex] .. string.rep(" ", unicode.wlen(curtext) - unicode.wlen(termlib.readHistory[readHistoryType][RHIndex])), false)
curtext = termlib.readHistory[readHistoryType][RHIndex]
end
if key == "down" and readHistoryType then
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY
print(curtext .. " ", false)
RHIndex = RHIndex + 1
if RHIndex > #termlib.readHistory[readHistoryType] then
RHIndex = #termlib.readHistory[readHistoryType]
end
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY
print(termlib.readHistory[readHistoryType][RHIndex] .. string.rep(" ", unicode.wlen(curtext) - unicode.wlen(termlib.readHistory[readHistoryType][RHIndex])), false)
curtext = termlib.readHistory[readHistoryType][RHIndex]
end
if args[3] >= 32 and args[3] <= 126 then if args[3] >= 32 and args[3] <= 126 then
curtext = curtext .. (unicode.char(args[3]) or "") curtext = curtext .. (unicode.char(args[3]) or "")
if readHistoryType then
termlib.readHistory[readHistoryType][RHIndex] = curtext
end
else else
if key == "back" then if key == "back" then
curtext = curtext:sub(1, #curtext-1) curtext = curtext:sub(1, #curtext-1)
@@ -183,7 +226,12 @@ function _G.read()
print(curtext.." ", false) print(curtext.." ", false)
elseif key == "enter" then elseif key == "enter" then
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY
print(curtext) print(curtext .. " ")
if readHistoryType then
while #termlib.readHistory[readHistoryType] > 50 do
table.remove(termlib.readHistory[readHistoryType], 1)
end
end
return curtext return curtext
end end
end end