Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get z9i36a06 colcode
- local colors_names = { --for use with colors api, you see.
- ["0"] = colors.black,
- ["1"] = colors.blue,
- ["2"] = colors.green,
- ["3"] = colors.cyan,
- ["4"] = colors.red,
- ["5"] = colors.purple,
- ["6"] = colors.orange,
- ["7"] = colors.lightGray,
- ["8"] = colors.gray,
- ["9"] = colors.blue, --they don't translate perfectly, okay??
- ["a"] = colors.lime,
- ["b"] = colors.lightBlue,
- ["c"] = colors.red, --two reds?? but the colors API has as many colors!
- ["d"] = colors.magenta,
- ["e"] = colors.yellow,
- ["f"] = colors.white,
- }
- local codeNames = { --just for checking, not for any translation
- ["k"] = "obfuscate",
- ["o"] = "italic",
- ["l"] = "bold",
- ["m"] = "strikethrough",
- ["n"] = "underline",
- ["r"] = "reset",
- }
- filterColors = function(str,doprint) --takes all color codes out of a string when appropriate. having second argument true makes it write!
- local p = 1
- local output = ""
- local colorout = "" --for term.blit'ing
- local code = "&"
- local col = "0"
- local cx,cy
- while p <= #str do
- if str:sub(p,p) == code then
- if colors_names[str:sub(p+1,p+1)] then
- col = str:sub(p+1,p+1)
- p = p + 1
- elseif codeNames[str:sub(p+1,p+1)] then
- if str:sub(p+1,p+1) == "r" then
- col = "0"
- end
- p = p + 1
- else
- if doprint then
- term.setTextColor(colors_names[col])
- cx,cy = term.getCursorPos()
- if cx+#words[wordNo] > scr_x then
- term.setCursorPos(1,cy+1)
- end
- write(str:sub(p,p))
- end
- end
- p = p + 1
- else
- output = output..str:sub(p,p)
- colorout = colorout..col
- if doprint then
- term.setTextColor(colors_names[col])
- cx,cy = term.getCursorPos()
- if cx+#words[wordNo] > scr_x then
- term.setCursorPos(1,cy+1)
- end
- write(str:sub(p,p))
- end
- p = p + 1
- end
- end
- return output, colorout
- end
- local colnames = {
- ["0"] = "black",
- ["1"] = "dark_blue",
- ["2"] = "dark_green",
- ["3"] = "dark_aqua",
- ["4"] = "dark_red",
- ["5"] = "dark_purple",
- ["6"] = "gold",
- ["7"] = "gray",
- ["8"] = "dark_gray",
- ["9"] = "blue",
- ["a"] = "green",
- ["b"] = "aqua",
- ["c"] = "red",
- ["d"] = "light_purple",
- ["e"] = "yellow",
- ["f"] = "white",
- }
- colorFormat = function(str,returnTable) --returns a LARGE table in JSON format for use with commands.tellraw()
- local color = false
- local obfuscated = false
- local bold = false
- local strikethrough = false
- local underline = false
- local italic = false
- local code = "&" --ONE CHARACTER
- local pos = 1
- local opos = 1
- local output = {}
- while pos <= #str do
- output[opos] = {}
- if str:sub(pos,pos) == code and pos < #str then
- local changed = false
- if colnames[str:sub(pos+1,pos+1)] then
- color = str:sub(pos+1,pos+1)
- changed = true
- else
- if str:sub(pos+1,pos+1) == "r" then
- color = false
- obfuscated = false
- bold = false
- strikethrough = false
- underline = false
- italic = false
- changed = true
- end
- if str:sub(pos+1,pos+1) == "k" then
- obfuscated = true
- changed = true
- end
- if str:sub(pos+1,pos+1) == "l" then
- bold = true
- changed = true
- end
- if str:sub(pos+1,pos+1) == "m" then
- strikethrough = true
- changed = true
- end
- if str:sub(pos+1,pos+1) == "n" then
- underline = true
- changed = true
- end
- if str:sub(pos+1,pos+1) == "o" then
- italic = true
- changed = true
- end
- end
- if changed then
- output[opos].text = ""
- pos = pos + 2
- else
- output[opos].text = str:sub(pos,pos)
- pos = pos + 1
- end
- else
- output[opos].text = str:sub(pos,pos)
- pos = pos + 1
- end
- output[opos].color = colnames[color or "f"]
- output[opos].obfuscated = obfuscated
- output[opos].bold = bold
- output[opos].strikethrough = strikethrough
- output[opos].underline = underline
- output[opos].italic = italic
- opos = opos + 1
- end
- if returnTable then
- return output
- else
- return textutils.serialiseJSON(output)
- end
- end
- blitWrap = function(text,txt,bg)
- local wordNo = 1
- local words = explode(" ",text)
- local lines = 0
- local cx,cy
- for a = 1, #text do
- cx,cy = term.getCursorPos()
- if text:sub(a,a) == " " and text:sub(a-1,a-1) ~= " " and a > 1 then
- wordNo = wordNo + 1
- if cx + #words[wordNo] > scr_x then
- term.setCursorPos(1,cy+1)
- lines = lines + 1
- end
- end
- cx,cy = term.getCursorPos()
- if text:sub(a,a) == "\n" then
- term.setCursorPos(1,cy+1)
- lines = lines + 1
- elseif not (cx == 1 and text:sub(a,a) == " ") then
- term.blit(text:sub(a,a),txt:sub(a,a),bg:sub(a,a))
- end
- end
- return lines
- end
- writef = function(txt)
- --Write Formatted (accepts color codes / ignores formatting codes). Returns lines.
- --Wraps text, finally!
- local tx = term.getTextColor()
- local text, textCol = filterColors(txt)
- local bgCol = blit_names[term.getBackgroundColor()]:rep(#text)
- local out = blitWrap(text,textCol,bgCol)
- term.setTextColor(tx)
- return out
- end
- printf = function(txt)
- return writef(tostring(txt.."\n"))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement