Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get B53NGJ2a frt
- split = function(txt,splitPt) --such a useful function
- local output = {}
- local buffer = ""
- for a = 1, #txt do
- if (txt:sub(a,a) == "\n") or (#buffer >= splitPt) then
- output[#output+1] = buffer
- buffer = ""
- end
- buffer = (buffer..txt:sub(a,a)):gsub("\n","")
- if a == #txt then
- output[#output+1] = buffer
- end
- end
- return output
- end
- funcread = function(x,y,xlimit,linelimit,txtcolor,bgcolor,repchar,rHistory,doFunc,noNewLine,writeFunc,cursorAdjFunc,doFuncEvent,charLimit,preInsertedText,preCursorPos)
- local scr_x,scr_y = term.getSize()
- local sx,sy = term.getCursorPos()
- 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()
- rHistory = rHistory or {}
- local prevTX, prevBG = term.getTextColor(), term.getBackgroundColor()
- local cursor = preCursorPos or 1
- local rCursor = #rHistory+1
- local output = preInsertedText or ""
- term.setCursorBlink(true)
- local keysDown = {}
- local rite = writeFunc or term.write
- cursorAdjFunc = cursorAdjFunc or function() return 0 end
- local tOut,pOut,wOut
- local pleaseDoDraw = true
- tOut = split(output,(xlimit-x))
- local drawIt = function()
- for a = 1, math.max(1,linelimit) do
- term.setCursorPos(x,y+(a-1))
- term.setTextColor(txtcolor)
- term.setBackgroundColor(bgcolor)
- rite((" "):rep(xlimit-x))
- if tOut[a] then
- term.setCursorPos(x,y+(a-1))
- rite(tOut[a])
- end
- end
- --rite(" ")
- end
- drawIt()
- while true do
- local evt,key,mx,my = os.pullEvent()
- tOut = split(output,(xlimit-x)) --table output, split between x=1 and x=(xlimit-x)
- if evt == doFuncEvent then
- pleaseDoFunc = true
- elseif evt == "key" then
- keysDown[key] = true
- if key == keys.enter then
- if not keysDown[keys.leftShift] or keysDown[keys.rightShift] then
- if not noNewLine then
- write("\n")
- end
- term.setCursorBlink(false)
- return output
- elseif #tOut < linelimit then
- output = (output:sub(1,cursor-1).."\n"..output:sub(cursor)):sub(1,charLimit or -1)
- cursor = math.min(#output+1,cursor+1)
- pleaseDoFunc = true
- end
- elseif key == keys.left then
- if cursor-1 >= 1 then
- cursor = cursor - 1
- end
- elseif key == keys.right then
- if cursor <= #output then
- cursor = cursor + 1
- end
- elseif key == keys.up then
- if rCursor > 1 then
- rCursor = rCursor - 1
- term.setCursorPos(x,y)
- rite((" "):rep(#output))
- output = (rHistory[rCursor] or ""):sub(1,charLimit or -1)
- cursor = #output+1
- pleaseDoFunc = true
- end
- elseif key == keys.down then
- term.setCursorPos(x,y)
- rite((" "):rep(#output))
- if rCursor < #rHistory then
- rCursor = rCursor + 1
- output = (rHistory[rCursor] or ""):sub(1,charLimit or -1)
- cursor = #output+1
- pleaseDoFunc = true
- else
- rCursor = #rHistory+1
- output = ""
- cursor = 1
- end
- elseif key == keys.backspace then
- if cursor > 1 and #output > 0 then
- output = (output:sub(1,cursor-2)..output:sub(cursor)):sub(1,charLimit or -1)
- cursor = cursor - 1
- pleaseDoFunc = true
- end
- elseif key == keys.delete then
- if #output:sub(cursor,cursor) == 1 then
- output = (output:sub(1,cursor-1)..output:sub(cursor+1)):sub(1,charLimit or -1)
- pleaseDoFunc = true
- end
- end
- elseif evt == "key_up" then
- keysDown[key] = false
- elseif evt == "char" or evt == "paste" then
- output = (output:sub(1,cursor-1)..key..output:sub(cursor+(#key-1))):sub(1,charLimit or -1)
- cursor = math.min(#output+1,cursor+#key)
- pleaseDoFunc = true
- elseif evt == "mouse_click" then
- if linelimit > 1 then
- if (my < y) or (my > y+linelimit) or (mx < x) or (mx > x+xlimit) then
- term.setCursorBlink(false)
- return output
- end
- end
- if tOut[my-y] then
- cursor = 0
- for a = 1, (my-y)-1 do
- cursor = cursor + #tOut[a]
- end
- cursor = cursor + math.max(#tOut[a],mx-x)
- end
- end
- tOut = split(output,(xlimit-x)) --table output, split between x=1 and x=(xlimit-x)
- pOut = (output or ""):sub(math.max( 1,(#output+x)-scr_x) ) --text output between x=1 and x=scr_x
- wOut = repchar and repchar:sub(1,1):rep(#pOut) or pOut
- if pleaseDoFunc or pleaseDraw then
- if pleaseDoFunc then
- if type(doFunc) == "function" then
- doFunc(output:sub(1,charLimit or -1))
- end
- end
- drawIt()
- pleaseDoFunc = false
- pleaseDraw = false
- end
- term.setCursorPos(x+(cursorAdjFunc and cursorAdjFunc(wOut) or 0)+cursor-math.max( 1,(#output+x)-scr_x),y)
- end
- end
- local simtype = function(txt)
- for a = 1, #txt do
- os.queueEvent("key",keys[txt:sub(a,a)])
- end
- end
- simtype("funcread()")
- shell.run("/rom/programs/lua")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement