Advertisement
LDDestroier

Minecraft Formatting Codes for ComputerCraft (printf/writef)

Aug 1st, 2016
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.03 KB | None | 0 0
  1. -- pastebin get z9i36a06 colcode
  2.  
  3. local colors_names = { --for use with colors api, you see.
  4.     ["0"] = colors.black,
  5.     ["1"] = colors.blue,
  6.     ["2"] = colors.green,
  7.     ["3"] = colors.cyan,
  8.     ["4"] = colors.red,
  9.     ["5"] = colors.purple,
  10.     ["6"] = colors.orange,
  11.     ["7"] = colors.lightGray,
  12.     ["8"] = colors.gray,
  13.     ["9"] = colors.blue, --they don't translate perfectly, okay??
  14.     ["a"] = colors.lime,
  15.     ["b"] = colors.lightBlue,
  16.     ["c"] = colors.red, --two reds?? but the colors API has as many colors!
  17.     ["d"] = colors.magenta,
  18.     ["e"] = colors.yellow,
  19.     ["f"] = colors.white,
  20. }
  21.  
  22. local codeNames = { --just for checking, not for any translation
  23.     ["k"] = "obfuscate",   
  24.     ["o"] = "italic",
  25.     ["l"] = "bold",
  26.     ["m"] = "strikethrough",
  27.     ["n"] = "underline",
  28.     ["r"] = "reset",
  29. }
  30.  
  31. filterColors = function(str,doprint) --takes all color codes out of a string when appropriate. having second argument true makes it write!
  32.     local p = 1
  33.     local output = ""
  34.     local colorout = "" --for term.blit'ing
  35.     local code = "&"
  36.     local col = "0"
  37.     local cx,cy
  38.     while p <= #str do
  39.         if str:sub(p,p) == code then
  40.             if colors_names[str:sub(p+1,p+1)] then
  41.                 col = str:sub(p+1,p+1)
  42.                 p = p + 1
  43.             elseif codeNames[str:sub(p+1,p+1)] then
  44.                 if str:sub(p+1,p+1) == "r" then
  45.                     col = "0"
  46.                 end
  47.                 p = p + 1
  48.             else
  49.                 if doprint then
  50.                     term.setTextColor(colors_names[col])
  51.                     cx,cy = term.getCursorPos()
  52.                     if cx+#words[wordNo] > scr_x then
  53.                         term.setCursorPos(1,cy+1)
  54.                     end
  55.                     write(str:sub(p,p))
  56.                 end
  57.             end
  58.             p = p + 1
  59.         else
  60.             output = output..str:sub(p,p)
  61.             colorout = colorout..col
  62.             if doprint then
  63.                 term.setTextColor(colors_names[col])
  64.                 cx,cy = term.getCursorPos()
  65.                 if cx+#words[wordNo] > scr_x then
  66.                     term.setCursorPos(1,cy+1)
  67.                 end
  68.                 write(str:sub(p,p))
  69.             end
  70.             p = p + 1
  71.         end
  72.     end
  73.     return output, colorout
  74. end
  75.  
  76. local colnames = {
  77.     ["0"] = "black",
  78.     ["1"] = "dark_blue",
  79.     ["2"] = "dark_green",
  80.     ["3"] = "dark_aqua",
  81.     ["4"] = "dark_red",
  82.     ["5"] = "dark_purple",
  83.     ["6"] = "gold",
  84.     ["7"] = "gray",
  85.     ["8"] = "dark_gray",
  86.     ["9"] = "blue",
  87.     ["a"] = "green",
  88.     ["b"] = "aqua",
  89.     ["c"] = "red",
  90.     ["d"] = "light_purple",
  91.     ["e"] = "yellow",
  92.     ["f"] = "white",
  93. }
  94.  
  95. colorFormat = function(str,returnTable) --returns a LARGE table in JSON format for use with commands.tellraw()
  96.     local color = false
  97.     local obfuscated = false
  98.     local bold = false
  99.     local strikethrough = false
  100.     local underline = false
  101.     local italic = false
  102.    
  103.     local code = "&" --ONE CHARACTER
  104.     local pos = 1
  105.     local opos = 1
  106.     local output = {}
  107.    
  108.     while pos <= #str do
  109.         output[opos] = {}
  110.         if str:sub(pos,pos) == code and pos < #str then
  111.             local changed = false
  112.             if colnames[str:sub(pos+1,pos+1)] then
  113.                 color = str:sub(pos+1,pos+1)
  114.                 changed = true
  115.             else
  116.                 if str:sub(pos+1,pos+1) == "r" then
  117.                     color = false
  118.                     obfuscated = false
  119.                     bold = false
  120.                     strikethrough = false
  121.                     underline = false
  122.                     italic = false
  123.                     changed = true
  124.                 end
  125.                 if str:sub(pos+1,pos+1) == "k" then
  126.                     obfuscated = true
  127.                     changed = true
  128.                 end
  129.                 if str:sub(pos+1,pos+1) == "l" then
  130.                     bold = true
  131.                     changed = true
  132.                 end
  133.                 if str:sub(pos+1,pos+1) == "m" then
  134.                     strikethrough = true
  135.                     changed = true
  136.                 end
  137.                 if str:sub(pos+1,pos+1) == "n" then
  138.                     underline = true
  139.                     changed = true
  140.                 end
  141.                 if str:sub(pos+1,pos+1) == "o" then
  142.                     italic = true
  143.                     changed = true
  144.                 end
  145.             end
  146.             if changed then
  147.                 output[opos].text = ""
  148.                 pos = pos + 2
  149.             else
  150.                 output[opos].text = str:sub(pos,pos)
  151.                 pos = pos + 1
  152.             end
  153.         else
  154.             output[opos].text = str:sub(pos,pos)
  155.             pos = pos + 1
  156.         end
  157.         output[opos].color = colnames[color or "f"]
  158.         output[opos].obfuscated = obfuscated
  159.         output[opos].bold = bold
  160.         output[opos].strikethrough = strikethrough
  161.         output[opos].underline = underline
  162.         output[opos].italic = italic
  163.         opos = opos + 1
  164.     end
  165.     if returnTable then
  166.         return output
  167.     else
  168.         return textutils.serialiseJSON(output)
  169.     end
  170. end
  171.  
  172. blitWrap = function(text,txt,bg)
  173.     local wordNo = 1
  174.     local words = explode(" ",text)
  175.     local lines = 0
  176.     local cx,cy
  177.     for a = 1, #text do
  178.         cx,cy = term.getCursorPos()
  179.         if text:sub(a,a) == " " and text:sub(a-1,a-1) ~= " " and a > 1 then
  180.             wordNo = wordNo + 1
  181.             if cx + #words[wordNo] > scr_x then
  182.                 term.setCursorPos(1,cy+1)
  183.                 lines = lines + 1
  184.             end
  185.         end
  186.         cx,cy = term.getCursorPos()
  187.         if text:sub(a,a) == "\n" then
  188.             term.setCursorPos(1,cy+1)
  189.             lines = lines + 1
  190.         elseif not (cx == 1 and text:sub(a,a) == " ") then
  191.             term.blit(text:sub(a,a),txt:sub(a,a),bg:sub(a,a))
  192.         end
  193.     end
  194.     return lines
  195. end
  196.  
  197. writef = function(txt)
  198.     --Write Formatted (accepts color codes / ignores formatting codes). Returns lines.
  199.     --Wraps text, finally!
  200.     local tx = term.getTextColor()
  201.     local text, textCol = filterColors(txt)
  202.     local bgCol = blit_names[term.getBackgroundColor()]:rep(#text)
  203.     local out = blitWrap(text,textCol,bgCol)
  204.     term.setTextColor(tx)
  205.     return out
  206. end
  207.  
  208. printf = function(txt)
  209.     return writef(tostring(txt.."\n"))
  210. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement