Advertisement
GravityScore

Modified Read Function

Feb 15th, 2013
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.15 KB | None | 0 0
  1.  
  2. --  
  3. --  Modified Read
  4. --  By Gravity Score
  5. --  
  6.  
  7.  
  8. local w, h = term.getSize()
  9.  
  10. local modifiedRead = function(properties)
  11.     -- Properties:
  12.     -- - replaceCharacter
  13.     -- - displayLength
  14.     -- - maxLength
  15.     -- - onEvent
  16.     -- - startingText
  17.  
  18.     local text = ""
  19.     local startX, startY = term.getCursorPos()
  20.     local pos = 0
  21.  
  22.     if not properties then
  23.         properties = {}
  24.     end
  25.     if properties.displayLength then
  26.         properties.displayLength = math.min(properties.displayLength, w - 2)
  27.     end
  28.     if properties.startingText then
  29.         text = properties.startingText
  30.         pos = text:len()
  31.     end
  32.  
  33.     local draw = function(replaceCharacter)
  34.         local scroll = 0
  35.         if properties.displayLength and pos > properties.displayLength then
  36.             scroll = pos - properties.displayLength
  37.         end
  38.  
  39.         local repl = replaceCharacter or properties.replaceCharacter
  40.         term.setTextColor(theme.text)
  41.         term.setCursorPos(startX, startY)
  42.         if repl then
  43.             term.write(string.rep(repl:sub(1, 1), text:len() - scroll))
  44.         else
  45.             term.write(text:sub(scroll + 1))
  46.         end
  47.  
  48.         term.setCursorPos(startX + pos - scroll, startY)
  49.     end
  50.  
  51.     term.setCursorBlink(true)
  52.     draw()
  53.     while true do
  54.         local event, key, x, y, param4, param5 = os.pullEvent()
  55.  
  56.         if properties.onEvent then
  57.             -- Actions:
  58.             -- - exit (bool)
  59.             -- - text
  60.  
  61.             term.setCursorBlink(false)
  62.             local action = properties.onEvent(text, event, key, x, y, param4, param5)
  63.             if action then
  64.                 if action.text then
  65.                     draw(" ")
  66.                     text = action.text
  67.                     pos = text:len()
  68.                 end if action.exit then
  69.                     break
  70.                 end
  71.             end
  72.             draw()
  73.         end
  74.  
  75.         term.setCursorBlink(true)
  76.         if event == "char" then
  77.             local canType = true
  78.             if properties.maxLength and text:len() >= properties.maxLength then
  79.                 canType = false
  80.             end
  81.  
  82.             if canType then
  83.                 text = text:sub(1, pos) .. key .. text:sub(pos + 1, -1)
  84.                 pos = pos + 1
  85.                 draw()
  86.             end
  87.         elseif event == "key" then
  88.             if key == keys.enter then
  89.                 break
  90.             elseif key == keys.left and pos > 0 then
  91.                 pos = pos - 1
  92.                 draw()
  93.             elseif key == keys.right and pos < text:len() then
  94.                 pos = pos + 1
  95.                 draw()
  96.             elseif key == keys.backspace and pos > 0 then
  97.                 draw(" ")
  98.                 text = text:sub(1, pos - 1) .. text:sub(pos + 1, -1)
  99.                 pos = pos - 1
  100.                 draw()
  101.             elseif key == keys.delete and pos < text:len() then
  102.                 draw(" ")
  103.                 text = text:sub(1, pos) .. text:sub(pos + 2, -1)
  104.                 draw()
  105.             elseif key == keys.home then
  106.                 pos = 0
  107.                 draw()
  108.             elseif key == keys["end"] then
  109.                 pos = text:len()
  110.                 draw()
  111.             end
  112.         elseif event == "mouse_click" then
  113.             local scroll = 0
  114.             if properties.displayLength and pos > properties.displayLength then
  115.                 scroll = pos - properties.displayLength
  116.             end
  117.  
  118.             if y == startY and x >= startX and x <= math.min(startX + text:len(), startX + (properties.displayLength or 10000)) then
  119.                 pos = x - startX + scroll
  120.                 draw()
  121.             elseif y == startY then
  122.                 if x < startX then
  123.                     pos = scroll
  124.                     draw()
  125.                 elseif x > math.min(startX + text:len(), startX + (properties.displayLength or 10000)) then
  126.                     pos = text:len()
  127.                     draw()
  128.                 end
  129.             end
  130.         end
  131.     end
  132.  
  133.     term.setCursorBlink(false)
  134.     print("")
  135.     return text
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement