Advertisement
LDDestroier

Smart Read (funcread mod) (cc)

May 15th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.75 KB | None | 0 0
  1. -- pastebin get B53NGJ2a frt
  2. split = function(txt,splitPt) --such a useful function
  3.     local output = {}
  4.     local buffer = ""
  5.     for a = 1, #txt do
  6.         if (txt:sub(a,a) == "\n") or (#buffer >= splitPt) then
  7.             output[#output+1] = buffer
  8.             buffer = ""
  9.         end
  10.         buffer = (buffer..txt:sub(a,a)):gsub("\n","")
  11.         if a == #txt then
  12.             output[#output+1] = buffer
  13.         end
  14.     end
  15.     return output
  16. end
  17. funcread = function(x,y,xlimit,linelimit,txtcolor,bgcolor,repchar,rHistory,doFunc,noNewLine,writeFunc,cursorAdjFunc,doFuncEvent,charLimit,preInsertedText,preCursorPos)
  18.     local scr_x,scr_y = term.getSize()
  19.     local sx,sy = term.getCursorPos()
  20.     x,y,xlimit,linelimit,txtcolor,bgcolor = x or sx, y or sy, xlimit or scr_x, linelimit or 1, txtcolor or term.getTextColor(), bgcolor or term.getBackgroundColor()
  21.     rHistory = rHistory or {}
  22.     local prevTX, prevBG = term.getTextColor(), term.getBackgroundColor()
  23.     local cursor = preCursorPos or 1
  24.     local rCursor = #rHistory+1
  25.     local output = preInsertedText or ""
  26.     term.setCursorBlink(true)
  27.     local keysDown = {}
  28.     local rite = writeFunc or term.write
  29.     cursorAdjFunc = cursorAdjFunc or function() return 0 end
  30.     local tOut,pOut,wOut
  31.     local pleaseDoDraw = true
  32.     tOut = split(output,(xlimit-x))
  33.     local drawIt = function()
  34.         for a = 1, math.max(1,linelimit) do
  35.             term.setCursorPos(x,y+(a-1))
  36.             term.setTextColor(txtcolor)
  37.             term.setBackgroundColor(bgcolor)
  38.             rite((" "):rep(xlimit-x))
  39.             if tOut[a] then
  40.                 term.setCursorPos(x,y+(a-1))
  41.                 rite(tOut[a])
  42.             end
  43.         end
  44.         --rite(" ")
  45.     end
  46.     drawIt()
  47.     while true do
  48.         local evt,key,mx,my = os.pullEvent()
  49.         tOut = split(output,(xlimit-x)) --table output, split between x=1 and x=(xlimit-x)
  50.         if evt == doFuncEvent then
  51.             pleaseDoFunc = true
  52.         elseif evt == "key" then
  53.             keysDown[key] = true
  54.             if key == keys.enter then
  55.                 if not keysDown[keys.leftShift] or keysDown[keys.rightShift] then
  56.                     if not noNewLine then
  57.                         write("\n")
  58.                     end
  59.                     term.setCursorBlink(false)
  60.                     return output
  61.                 elseif #tOut < linelimit then
  62.                     output = (output:sub(1,cursor-1).."\n"..output:sub(cursor)):sub(1,charLimit or -1)
  63.                     cursor = math.min(#output+1,cursor+1)
  64.                     pleaseDoFunc = true
  65.                 end
  66.             elseif key == keys.left then
  67.                 if cursor-1 >= 1 then
  68.                     cursor = cursor - 1
  69.                 end
  70.             elseif key == keys.right then
  71.                 if cursor <= #output then
  72.                     cursor = cursor + 1
  73.                 end
  74.             elseif key == keys.up then
  75.                 if rCursor > 1 then
  76.                     rCursor = rCursor - 1
  77.                     term.setCursorPos(x,y)
  78.                     rite((" "):rep(#output))
  79.                     output = (rHistory[rCursor] or ""):sub(1,charLimit or -1)
  80.                     cursor = #output+1
  81.                     pleaseDoFunc = true
  82.                 end
  83.             elseif key == keys.down then
  84.                 term.setCursorPos(x,y)
  85.                 rite((" "):rep(#output))
  86.                 if rCursor < #rHistory then
  87.                     rCursor = rCursor + 1
  88.                     output = (rHistory[rCursor] or ""):sub(1,charLimit or -1)
  89.                     cursor = #output+1
  90.                     pleaseDoFunc = true
  91.                 else
  92.                     rCursor = #rHistory+1
  93.                     output = ""
  94.                     cursor = 1
  95.                 end
  96.             elseif key == keys.backspace then
  97.                 if cursor > 1 and #output > 0 then
  98.                     output = (output:sub(1,cursor-2)..output:sub(cursor)):sub(1,charLimit or -1)
  99.                     cursor = cursor - 1
  100.                     pleaseDoFunc = true
  101.                 end
  102.             elseif key == keys.delete then
  103.                 if #output:sub(cursor,cursor) == 1 then
  104.                     output = (output:sub(1,cursor-1)..output:sub(cursor+1)):sub(1,charLimit or -1)
  105.                     pleaseDoFunc = true
  106.                 end
  107.             end
  108.         elseif evt == "key_up" then
  109.             keysDown[key] = false
  110.         elseif evt == "char" or evt == "paste" then
  111.             output = (output:sub(1,cursor-1)..key..output:sub(cursor+(#key-1))):sub(1,charLimit or -1)
  112.             cursor = math.min(#output+1,cursor+#key)
  113.             pleaseDoFunc = true
  114.         elseif evt == "mouse_click" then
  115.             if linelimit > 1 then
  116.                 if (my < y) or (my > y+linelimit) or (mx < x) or (mx > x+xlimit) then
  117.                     term.setCursorBlink(false)
  118.                     return output
  119.                 end
  120.             end
  121.             if tOut[my-y] then
  122.                 cursor = 0
  123.                 for a = 1, (my-y)-1 do
  124.                     cursor = cursor + #tOut[a]
  125.                 end
  126.                 cursor = cursor + math.max(#tOut[a],mx-x)
  127.             end
  128.         end
  129.         tOut = split(output,(xlimit-x)) --table output, split between x=1 and x=(xlimit-x)
  130.         pOut = (output or ""):sub(math.max( 1,(#output+x)-scr_x) ) --text output between x=1 and x=scr_x
  131.         wOut = repchar and repchar:sub(1,1):rep(#pOut) or pOut
  132.         if pleaseDoFunc or pleaseDraw then
  133.             if pleaseDoFunc then
  134.                 if type(doFunc) == "function" then
  135.                     doFunc(output:sub(1,charLimit or -1))
  136.                 end
  137.             end
  138.             drawIt()
  139.             pleaseDoFunc = false
  140.             pleaseDraw = false
  141.         end
  142.         term.setCursorPos(x+(cursorAdjFunc and cursorAdjFunc(wOut) or 0)+cursor-math.max( 1,(#output+x)-scr_x),y)
  143.     end
  144. end
  145.  
  146. local simtype = function(txt)
  147.     for a = 1, #txt do
  148.         os.queueEvent("key",keys[txt:sub(a,a)])
  149.     end
  150. end
  151.  
  152. simtype("funcread()")
  153. shell.run("/rom/programs/lua")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement