This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
H4X0RZ's profile picture

I need help with a read function

Started by H4X0RZ, 19 July 2015 - 08:26 PM
H4X0RZ #1
Posted 19 July 2015 - 10:26 PM
I wrote this custom read function, but it doesn't really work right.

http://pastebin.com/sP85n651 (#EDIT: Because the code block indentation kinda sucks, the function is now on pastebin.)

Expected behavior: I can write text, and move the "cursor" around using the left and right arrow key. Also it should be able to delete the char the the current cursor position.

Actual Behavior: I can write text, and also can move the cursor around. Also I can delete the text, but then it freaks out. I'm not sure if it's a visual problem or if it really "blocks" some text though. When I delete text while the cursor is not at the end of the string, then I'm unable to move the cursor to the end.

I hope someone can help me :)/>

Greetings,
H4X0RZ
Edited on 19 July 2015 - 08:29 PM
HPWebcamAble #2
Posted 20 July 2015 - 12:25 AM
A few things I saw:

local mask = mask or nil
lol wat


if(evt[1] == "char") then
  str[#str+1] = evt[2] 
  cursor = #str
  redraw()
elseif(evt[1] == "key") then

So when you hit a key (Well, a char), it always adds it to the end of the input?
Wouldn't you want it to add the new char where the cursor is?


I assume 'backKey' is delete (why not just keys.delete? You assign it as 14 above)

if(evt[2] == backKey) then
  str[cursor] = nil  --# Remove the character at the cursors position
  cursor = cursor-1  --# Move the cursor left
  if(cursor>#str) then cursor = #str end  --# Move it back if its somehow past where the string ends
  if(cursor<1) then cursor = 1 end  --# Move it right if it got moved too far left
  redraw()
elseif(evt[2] == keys.left) then

I think your problem is there is now a random 'nil' value in your 'str' table

str = { "t" , "h", "i", "s", "  ",  "i", "s", "a", "t", nil, "s", "t"}
--# this is a test

Try 'table.remove(str,cursor)'


For comparison, heres a scrolling read function I wrote a while ago:
Spoilerx and y or the pos on the screen

lenght is maximum lenght of the text. When you input more than that, it starts scrolling.

insertText is just text you want the box to start with

It doesn't support moving the cursor with the arrow keys, but it DOES scroll so yeah.

function scrollRead(x,y,length,insertText)
  if insertText then
    cPos = #insertText+1
    cInput = insertText
  else
    cPos = 1
    cInput = ""
  end
  term.setCursorBlink(true)
  while true do
    term.setCursorPos(x,y)
    term.write(string.rep(" ",length))
    term.setCursorPos(x,y)
    if string.len(cInput) > length-1 then
      term.write(string.sub(cInput,(length-1)*-1))
    else
      term.write(cInput)
    end
    if cPos > length-1 then
      term.setCursorPos(x+length-1,y)
    else  
     term.setCursorPos(x+cPos-1,y)
    end
    local event,p1 = os.pullEvent()
    if event == "char" then
      cInput = string.sub(cInput,1,cPos)..p1..string.sub(cInput,cPos+1)
      cPos = cPos + 1                  
    elseif event == "key" then
      if p1 == keys.enter then
        break
      elseif p1 == keys.backspace then
        if cPos > 1 then
          cInput = string.sub(cInput,1,cPos-2)..string.sub(cInput,cPos)
          cPos = cPos - 1
        end
      elseif p1 == keys["end"] then
        cPos = string.len(cInput)+1
      end    
    end
  end
  term.setCursorBlink(false)
  return cInput
end