Advertisement
incinirate

Limited Reading

Jul 7th, 2014
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.76 KB | None | 0 0
  1. function limitRead( nlim, cOverlay)
  2.   cOverlay=cOverlay or nil
  3.   local px,py = term.getCursorPos()
  4.   local txt = ""
  5.   local boxOffset = 0
  6.   local strPos = 1
  7.   term.setCursorBlink(true)
  8.   while true do
  9.     e,p1 = os.pullEvent()
  10.     if e=="key" then
  11.       local key = p1
  12.       if key==keys.left then
  13.         if strPos>1 then
  14.           strPos=strPos-1
  15.           if strPos-boxOffset==0 then
  16.             boxOffset=boxOffset-1
  17.           end
  18.         end
  19.       elseif key==keys.right then
  20.         if strPos<=#txt then
  21.           strPos=strPos+1
  22.           if strPos-boxOffset>nlim then
  23.             boxOffset=boxOffset+1
  24.           end
  25.         end
  26.       elseif key==keys.enter then
  27.         break
  28.       elseif key==keys.backspace then
  29.         if strPos~=1 then
  30.           txt=txt:sub(1,strPos-2)..txt:sub(strPos,#txt)
  31.           strPos=strPos-1
  32.           if strPos-boxOffset==0 then
  33.             boxOffset=boxOffset-1
  34.           end
  35.         end
  36.       elseif key==keys.delete then
  37.         if strPos-1~=#txt then
  38.           txt=txt:sub(1,strPos-1)..txt:sub(strPos+1,#txt)
  39.         end
  40.       end
  41.     elseif e=="char" then
  42.       txt = txt:sub(1,strPos-1)..p1..txt:sub(strPos,#txt)
  43.       strPos=strPos+1
  44.       if strPos-1-boxOffset>=nlim then
  45.         boxOffset=boxOffset+1
  46.       end
  47.     end
  48.     term.setCursorPos(px,py)
  49.     write(string.rep(" ",nlim))
  50.     term.setCursorPos(px,py)
  51.  
  52.     --print(cOverlay==nil and "Yep" or "Nope")
  53.     --read()
  54.     if cOverlay==nil then
  55.     --  term.setTextColor(colors.white)
  56.       write(txt:sub(boxOffset+1,boxOffset+nlim))
  57.     --  write(txt)
  58.     else
  59.       write(string.rep(cOverlay,#txt):sub(boxOffset+1,nlim+boxOffset))
  60.     end
  61.     term.setCursorPos(px+strPos-1-boxOffset,py)
  62.   end
  63.   term.setCursorBlink(false)
  64.   return txt
  65. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement