termlib cursor movement

This commit is contained in:
fluxdrive
2025-05-31 18:38:55 +02:00
committed by GitHub
parent eb9716ba68
commit 5dbe3337ff
+181 -74
View File
@@ -187,86 +187,193 @@ function _G.clear()
termlib.cursorPosX, termlib.cursorPosY = 1, 1 termlib.cursorPosX, termlib.cursorPosY = 1, 1
end end
-- god i hope this silly claude code works first try
function _G.read(readHistoryType, prefix, defaultText) function _G.read(readHistoryType, prefix, defaultText)
checkArg(1, readHistoryType, "string", "nil") checkArg(1, readHistoryType, "string", "nil")
checkArg(2, prefix, "string", "nil") checkArg(2, prefix, "string", "nil")
checkArg(3, defaultText, "string", "nil") checkArg(3, defaultText, "string", "nil")
local curtext = defaultText or "" local curtext = defaultText or ""
local prefix = prefix or "" local prefix = prefix or ""
local RHIndex local textCursorPos = unicode.wlen(curtext) + 1 -- Position within the text (1-based)
if readHistoryType then
if not termlib.readHistory[readHistoryType] then local RHIndex
termlib.readHistory[readHistoryType] = {curtext} if readHistoryType then
if not termlib.readHistory[readHistoryType] then
termlib.readHistory[readHistoryType] = {curtext}
elseif termlib.readHistory[readHistoryType][#termlib.readHistory[readHistoryType]] ~= "" then elseif termlib.readHistory[readHistoryType][#termlib.readHistory[readHistoryType]] ~= "" then
table.insert(termlib.readHistory[readHistoryType], curtext) table.insert(termlib.readHistory[readHistoryType], curtext)
end end
RHIndex = #termlib.readHistory[readHistoryType] -- read history index RHIndex = #termlib.readHistory[readHistoryType]
end end
local cursorPosX, cursorPosY = termlib.cursorPosX, termlib.cursorPosY
termlib.write(prefix .. curtext .. "\27[107m ") local cursorPosX, cursorPosY = termlib.cursorPosX, termlib.cursorPosY
local cursorWhite = true
while true do -- Track maximum text length to ensure proper clearing across wrapped lines
--ocelot.log(curtext) local maxTextLength = unicode.wlen(prefix .. curtext)
termlib.cursorPosX = termlib.cursorPosX - 1
local args = {event.pull("key_down", "clipboard", 0.5)} -- Function to calculate how many lines text will occupy
if args[1] == "key_down" and args[4] then local function calculateLines(text)
cursorWhite = true local totalWidth = unicode.wlen(prefix .. text)
local keycode = args[4] local width = gpu.getResolution()
local key = keyboard.keys[keycode] return math.ceil(totalWidth / width)
if key == "up" and readHistoryType then end
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY
termlib.write(prefix .. curtext .. " ") -- Track maximum lines used
RHIndex = RHIndex - 1 local maxLinesUsed = calculateLines(curtext)
if RHIndex <= 0 then
RHIndex = 1 -- Function to redraw the input line with cursor
local function redrawLine()
local startX, startY = cursorPosX, cursorPosY
-- Calculate current and max lines needed
local currentLines = calculateLines(curtext)
local linesToClear = math.max(maxLinesUsed, currentLines)
-- Clear all potentially used lines
for i = 0, linesToClear - 1 do
termlib.cursorPosX, termlib.cursorPosY = 1, startY + i
if startY + i <= height then
local width = gpu.getResolution()
termlib.write(string.rep(" ", width))
end end
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY
termlib.write(prefix .. termlib.readHistory[readHistoryType][RHIndex] .. string.rep(" ", unicode.wlen(curtext) - unicode.wlen(termlib.readHistory[readHistoryType][RHIndex])))
curtext = termlib.readHistory[readHistoryType][RHIndex]
end
if key == "down" and readHistoryType then
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY
termlib.write(prefix .. curtext .. " ")
RHIndex = RHIndex + 1
if RHIndex > #termlib.readHistory[readHistoryType] then
RHIndex = #termlib.readHistory[readHistoryType]
end end
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY
termlib.write(prefix .. termlib.readHistory[readHistoryType][RHIndex] .. string.rep(" ", unicode.wlen(curtext) - unicode.wlen(termlib.readHistory[readHistoryType][RHIndex]))) -- Reset cursor to start position
curtext = termlib.readHistory[readHistoryType][RHIndex] termlib.cursorPosX, termlib.cursorPosY = startX, startY
end
if key == "back" then -- Update tracking variables
curtext = curtext:sub(1, #curtext-1) maxTextLength = math.max(maxTextLength, unicode.wlen(prefix .. curtext))
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY maxLinesUsed = math.max(maxLinesUsed, currentLines)
termlib.write(prefix .. curtext.." ")
end -- Draw text with cursor positioned correctly
if key == "enter" then local beforeCursor = curtext:sub(1, utf8.offset(curtext, textCursorPos) - 1 or 0)
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY local afterCursor = curtext:sub(utf8.offset(curtext, textCursorPos) or (#curtext + 1))
print(prefix .. curtext .. " ")
if readHistoryType then termlib.write(prefix .. beforeCursor)
while #termlib.readHistory[readHistoryType] > 50 do termlib.write("\27[107m" .. (afterCursor:sub(1, 1) ~= "" and afterCursor:sub(1, 1) or " ") .. "\27[0m")
table.remove(termlib.readHistory[readHistoryType], 1) termlib.write(afterCursor:sub(2))
end
redrawLine()
local cursorWhite = true
while true do
local args = {event.pull("key_down", "clipboard", 0.5)}
if args[1] == "key_down" and args[4] then
cursorWhite = true
local keycode = args[4]
local key = keyboard.keys[keycode]
-- Handle arrow keys
if key == "up" and readHistoryType then
RHIndex = RHIndex - 1
if RHIndex <= 0 then
RHIndex = 1
end
curtext = termlib.readHistory[readHistoryType][RHIndex]
textCursorPos = unicode.wlen(curtext) + 1
redrawLine()
elseif key == "down" and readHistoryType then
RHIndex = RHIndex + 1
if RHIndex > #termlib.readHistory[readHistoryType] then
RHIndex = #termlib.readHistory[readHistoryType]
end end
end curtext = termlib.readHistory[readHistoryType][RHIndex]
return curtext textCursorPos = unicode.wlen(curtext) + 1
end redrawLine()
if args[3] >= 32 and args[3] <= 126 then
curtext = curtext .. (unicode.char(args[3]) or "") elseif key == "left" then
if readHistoryType then -- Move cursor left
termlib.readHistory[readHistoryType][RHIndex] = curtext if textCursorPos > 1 then
end textCursorPos = textCursorPos - 1
end redrawLine()
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY end
termlib.write(prefix .. curtext)
elseif args[1] == "clipboard" then elseif key == "right" then
-- Move cursor right
else if textCursorPos <= unicode.wlen(curtext) then
cursorWhite = not cursorWhite textCursorPos = textCursorPos + 1
end redrawLine()
if cursorWhite then end
termlib.write("\27[107m ")
else elseif key == "home" then
termlib.write(" ") -- Move to beginning of line
end textCursorPos = 1
redrawLine()
elseif key == "end" then
-- Move to end of line
textCursorPos = unicode.wlen(curtext) + 1
redrawLine()
elseif key == "back" then
-- Backspace - delete character before cursor
if textCursorPos > 1 then
local beforeCursor = curtext:sub(1, utf8.offset(curtext, textCursorPos - 1) - 1 or 0)
local afterCursor = curtext:sub(utf8.offset(curtext, textCursorPos) or (#curtext + 1))
curtext = beforeCursor .. afterCursor
textCursorPos = textCursorPos - 1
if readHistoryType then
termlib.readHistory[readHistoryType][RHIndex] = curtext
end
redrawLine()
end
elseif key == "delete" then
-- Delete - delete character at cursor
if textCursorPos <= unicode.wlen(curtext) then
local beforeCursor = curtext:sub(1, utf8.offset(curtext, textCursorPos) - 1 or 0)
local afterCursor = curtext:sub(utf8.offset(curtext, textCursorPos + 1) or (#curtext + 1))
curtext = beforeCursor .. afterCursor
if readHistoryType then
termlib.readHistory[readHistoryType][RHIndex] = curtext
end
redrawLine()
end
elseif key == "enter" then
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY
print(prefix .. curtext .. " ")
if readHistoryType then
while #termlib.readHistory[readHistoryType] > 50 do
table.remove(termlib.readHistory[readHistoryType], 1)
end
end
return curtext
elseif args[3] >= 32 and args[3] <= 126 then
-- Insert character at cursor position
local char = unicode.char(args[3]) or ""
local beforeCursor = curtext:sub(1, utf8.offset(curtext, textCursorPos) - 1 or 0)
local afterCursor = curtext:sub(utf8.offset(curtext, textCursorPos) or (#curtext + 1))
curtext = beforeCursor .. char .. afterCursor
textCursorPos = textCursorPos + 1
if readHistoryType then
termlib.readHistory[readHistoryType][RHIndex] = curtext
end
redrawLine()
end
elseif args[1] == "clipboard" then
-- Handle clipboard paste here if needed
else
-- Cursor blink timing
cursorWhite = not cursorWhite
if cursorWhite then
redrawLine()
else
-- Show cursor as normal character or space
termlib.cursorPosX, termlib.cursorPosY = cursorPosX, cursorPosY
local beforeCursor = curtext:sub(1, utf8.offset(curtext, textCursorPos) - 1 or 0)
local afterCursor = curtext:sub(utf8.offset(curtext, textCursorPos) or (#curtext + 1))
termlib.write(prefix .. beforeCursor)
termlib.write(afterCursor:sub(1, 1) ~= "" and afterCursor:sub(1, 1) or " ")
termlib.write(afterCursor:sub(2))
end
end
end end
end end