Advertisement
LDDestroier

BlitWrap

Jun 19th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.53 KB | None | 0 0
  1. local explode = function(div,str,replstr,includeDiv)
  2.     if (div=='') then return false end
  3.     local pos,arr = 0,{}
  4.     for st,sp in function() return string.find(str,div,pos,false) end do
  5.         table.insert(arr,string.sub(replstr or str,pos,st-1+(includeDiv and #div or 0)))
  6.         pos = sp + 1
  7.     end
  8.     table.insert(arr,string.sub(replstr or str,pos))
  9.     return arr
  10. end
  11.  
  12. blitWrap = function(text,txt,bg,noWrite)
  13.     local words = explode(" ",text,nil,true)
  14.     local output, lines, wordNo = {{}}, 0, 1
  15.     local scr_x, scr_y = term.getSize()
  16.     local cx,cy
  17.     local startX,startY = term.getCursorPos()
  18.     for a = 1, #text do
  19.         cx,cy = term.getCursorPos()
  20.         if text:sub(a,a) == " " and text:sub(a-1,a-1) ~= " " and a > 1 then
  21.             wordNo = wordNo + 1
  22.             if cx + #words[wordNo] > scr_x then
  23.                 term.setCursorPos(1,cy+1)
  24.                 lines = lines + 1
  25.                 output[lines+1] = {}
  26.             end
  27.         end
  28.         cx,cy = term.getCursorPos()
  29.         if text:sub(a,a) == "\n" then
  30.             term.setCursorPos(1,cy+1)
  31.             lines = lines + 1
  32.             output[lines+1] = {}
  33.         elseif not (cx == 1 and text:sub(a,a) == " ") then
  34.             if noWrite == true then
  35.                 term.setCursorPos(cx+1,cy)
  36.             else
  37.                 term.blit(text:sub(a,a),txt:sub(a,a),bg:sub(a,a))
  38.             end
  39.             output[lines+1] = {
  40.                 (output[lines+1][1] or "")..text:sub(a,a),
  41.                 (output[lines+1][2] or "")..txt:sub(a,a),
  42.                 (output[lines+1][3] or "")..bg:sub(a,a)
  43.             }
  44.         end
  45.         if cx > scr_x then
  46.             term.setCursorPos(1,cy+1)
  47.             lines = lines + 1
  48.             output[lines+1] = {}
  49.         end
  50.     end
  51.     if noWrite then
  52.         term.setCursorPos(startX,startY)
  53.     end
  54.     return lines, output
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement