Advertisement
LDDestroier

BlitWrap CC

Feb 15th, 2017
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.11 KB | None | 0 0
  1. local colors_names = { --this has been modified to use the Paint colors rather than minecraft formatting
  2.     ["0"] = colors.white,
  3.     ["1"] = colors.orange,
  4.     ["2"] = colors.magenta,
  5.     ["3"] = colors.lightBlue,
  6.     ["4"] = colors.yellow,
  7.     ["5"] = colors.lime,
  8.     ["6"] = colors.pink,
  9.     ["7"] = colors.gray,
  10.     ["8"] = colors.lightGray,
  11.     ["9"] = colors.cyan,
  12.     ["a"] = colors.purple,
  13.     ["b"] = colors.blue,
  14.     ["c"] = colors.brown,
  15.     ["d"] = colors.green,
  16.     ["e"] = colors.red,
  17.     ["f"] = colors.black,
  18. }
  19.  
  20. local blit_names = {}
  21. for k,v in pairs(colors_names) do
  22.     blit_names[v] = k
  23. end
  24.  
  25. local function explode(div,str)
  26.     if (div=='') then return false end
  27.     local pos,arr = 0,{}
  28.     for st,sp in function() return string.find(str,div,pos,true) end do
  29.         table.insert(arr,string.sub(str,pos,st-1))
  30.         pos = sp + 1
  31.     end
  32.     table.insert(arr,string.sub(str,pos))
  33.     return arr
  34. end
  35.  
  36. blitWrap = function(text,txt,bg)
  37.     local wordNo = 1
  38.     local words = explode(" ",text)
  39.     local lines = 0
  40.     local scr_x, scr_y = term.getSize()
  41.     local cx,cy
  42.     for a = 1, #text do
  43.         cx,cy = term.getCursorPos()
  44.         if text:sub(a,a) == " " and text:sub(a-1,a-1) ~= " " and a > 1 then
  45.             wordNo = wordNo + 1
  46.             if cx + #words[wordNo] > scr_x then
  47.                 term.setCursorPos(1,cy+1)
  48.                 lines = lines + 1
  49.             end
  50.         end
  51.         cx,cy = term.getCursorPos()
  52.         if text:sub(a,a) == "\n" then
  53.             term.setCursorPos(1,cy+1)
  54.             lines = lines + 1
  55.         elseif not (cx == 1 and text:sub(a,a) == " ") then
  56.             term.blit(text:sub(a,a),txt:sub(a,a),bg:sub(a,a))
  57.         end
  58.         if cx == scr_x then
  59.             term.setCursorPos(1,cy+1)
  60.             lines = lines + 1
  61.         end
  62.     end
  63.     return lines
  64. end
  65.  
  66. local codeNames = { --shaddap
  67.     ["r"] = "reset"
  68. }
  69.  
  70. local textToBlit = function(str) --returns output for term.blit, or blitWrap, with formatting codes for color selection.
  71.     local p = 1
  72.     local output = ""
  73.     local txcolorout = ""
  74.     local bgcolorout = ""
  75.     local txcode = "&"
  76.     local bgcode = "~"
  77.     local txcol = blit_names[term.getTextColor()]
  78.     local bgcol = blit_names[term.getBackgroundColor()]
  79.     local cx,cy
  80.     while p <= #str do
  81.         if str:sub(p,p) == txcode then
  82.             if colors_names[str:sub(p+1,p+1)] then
  83.                 txcol = str:sub(p+1,p+1)
  84.                 p = p + 1
  85.             elseif codeNames[str:sub(p+1,p+1)] then
  86.                 if str:sub(p+1,p+1) == "r" then
  87.                     txcol = blit_names[term.getTextColor()]
  88.                     bgcol = blit_names[term.getBackgroundColor()]
  89.                 end
  90.                 p = p + 1
  91.             end
  92.             p = p + 1
  93.         elseif str:sub(p,p) == bgcode then
  94.             if colors_names[str:sub(p+1,p+1)] then
  95.                 bgcol = str:sub(p+1,p+1)
  96.                 p = p + 1
  97.             elseif codeNames[str:sub(p+1,p+1)] then
  98.                 if str:sub(p+1,p+1) == "r" then
  99.                     txcol = blit_names[term.getTextColor()]
  100.                     bgcol = blit_names[term.getBackgroundColor()]
  101.                 end
  102.                 p = p + 1
  103.             end
  104.             p = p + 1
  105.         else
  106.             output = output..str:sub(p,p)
  107.             txcolorout = txcolorout..txcol
  108.             bgcolorout = bgcolorout..bgcol
  109.             p = p + 1
  110.         end
  111.     end
  112.     return output, txcolorout, bgcolorout
  113. end
  114.  
  115. writef = function(txt)
  116.     local text, textCol, bgCol = textToBlit(txt)
  117.     local out = blitWrap(text,textCol,bgCol)
  118.     return out
  119. end
  120.  
  121. printf = function(txt)
  122.     return writef(tostring(txt.."\n"))
  123. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement