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