adv_chat/colorize_message.lua

47 lines
1.4 KiB
Lua
Raw Normal View History

2020-07-29 15:03:55 -07:00
local const_discord = bridges.discord
2019-10-31 13:54:15 -07:00
-- Converts "#XXXXXX" color codes to colors
2019-07-13 11:03:35 -07:00
function colorize_message(message)
local rope={}
2020-07-29 15:03:55 -07:00
local otherrope
if const_discord then
otherrope={}
end
2020-03-30 05:16:11 -07:00
local function append_character(c)
table.insert(rope, c)
2020-07-29 15:03:55 -07:00
if const_discord then
table.insert(otherrope, c)
end
2020-03-30 05:16:11 -07:00
end
2019-10-31 13:54:15 -07:00
local i=1
while i <= message:len() do
local c=message:sub(i,i)
if c == string.char(0x1b) and message:sub(i+1, i+4) == "(c@#" and message:sub(i+11, i+11) == ")" then
table.insert(rope, message:sub(i, i+11))
i=i+11
elseif c == "#" then
2020-03-30 05:16:11 -07:00
local color = true
2019-07-13 11:03:35 -07:00
for j=i+1, i+6 do
2019-10-31 13:54:15 -07:00
local c2=message:sub(j,j):upper()
if c2=="" or not ((c2 >= "0" and c2 <= "9") or (c2 >= "A" and c2 <= "F")) then
2020-03-30 05:16:11 -07:00
color = false
break
2019-07-13 11:03:35 -07:00
end
end
2020-03-30 05:16:11 -07:00
if color then
table.insert(rope, minetest.get_color_escape_sequence(message:sub(i, i+6)))
i=i+6
else
append_character(c)
end
else
append_character(c)
2019-07-13 11:03:35 -07:00
end
2019-10-31 13:54:15 -07:00
i=i+1
2019-07-13 11:03:35 -07:00
end
2020-07-29 15:03:55 -07:00
if const_discord then
return table.concat(rope), table.concat(otherrope)
end
2019-10-31 13:54:15 -07:00
return table.concat(rope)
end
load_schemes()