Guest User

Untitled

a guest
Jul 19th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.42 KB | None | 0 0
  1. local function read(mask,history,normal,selected)
  2.     local continue = true
  3.     local stopKey = keys.enter
  4.     local backKey = 14
  5.  
  6.     local W,H = term.getSize()
  7.  
  8.     local mask = mask or nil
  9.     local history = history or {}
  10.     local normal = normal or colors.black
  11.     local selected = selected or colors.gray
  12.  
  13.     local cursor = 1
  14.     local str = {}
  15.     local lastLength = 0
  16.  
  17.     local sX,sY = term.getCursorPos()
  18.  
  19.     function redraw()
  20.         term.setCursorPos(sX,sY)
  21.         term.write(string.rep(" ",lastLength))
  22.         term.setCursorPos(sX,sY)
  23.         if(#str==0) then
  24.             term.setTextColor(selected)
  25.             term.write("_")
  26.         else
  27.             for k,v in pairs(str) do
  28.                 term.setTextColor(normal)
  29.                 if(k == cursor) then
  30.                     term.setTextColor(selected)
  31.                 end
  32.                 term.write(v)
  33.             end
  34.         end
  35.         lastLength = W
  36.     end
  37.  
  38.     redraw()
  39.  
  40.     while continue do
  41.         local evt = {os.pullEvent()}
  42.         if(evt[1] == "char") then
  43.             str[#str+1] = evt[2]
  44.             cursor = #str
  45.             redraw()
  46.         elseif(evt[1] == "key") then
  47.             if(evt[2] == backKey) then
  48.                 str[cursor] = nil
  49.                 cursor = cursor-1
  50.                 if(cursor>#str) then cursor = #str end
  51.                 if(cursor<1) then cursor = 1 end
  52.                 redraw()
  53.             elseif(evt[2] == keys.left) then
  54.                 if(cursor > 1) then cursor = cursor-1 end
  55.                 redraw()
  56.             elseif(evt[2] == keys.right) then
  57.                 if(cursor < #str) then cursor = cursor+1 end
  58.                 redraw()
  59.             elseif(evt[2] == keys.enter) then
  60.                 continue = false
  61.             end
  62.         end
  63.     end
  64.     return table.concat(str,"")
  65. end
Advertisement
Add Comment
Please, Sign In to add comment