Lrexlib adds regex support within lua.
Code: Select all
rex = require"rex_pcre" or echo"\nFailed to load rex_pcre.dll."
--[[-------------------------------------------------------------------------------
-- Echo with color function.
-- Arg1: String to echo
-- Arg2: String containing value for foreground color in hexadecimal RGB format
-- Arg3: String containing value for background color in hexadecimal RGB format
--
-- Color changes can be made within the string using the format |cFRFGFB,BRBGBB
-- where FR is the foreground red value, FG is the foreground green value, FB
-- is the foreground blue value, BR is the background red value, etc. ,BRBGBB
-- is optional. |r can be used within the string to reset the colors to default.
--
-- The colors in arg2 and arg3 replace the normal defaults for your console.
-- So if you use cecho("|cff0000Testing |rTesting", "00ff00", "0000ff"),
-- the first Testing would be red on black and the second would be green on blue.
--]]-------------------------------------------------------------------------------
cecho = function(str, fgColor, bgColor, insert)
local t = {}
local reset, out
if insert then
out = function(str)
insertText(str)
end
else
out = function(str)
echo(str)
end
end
if fgColor then
if bgColor then
reset = function()
local fr, fg, fb = rex.new("([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})"):match(fgColor)
fr, fg, fb = tonumber(fr, 16), tonumber(fg, 16), tonumber(fb, 16)
setFgColor(fr, fg, fb)
local br, bg, bb = rex.new("([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})"):match(bgColor)
br, bg, bb = tonumber(br, 16), tonumber(bg, 16), tonumber(bb, 16)
setBgColor(br, bg, bb)
end
else
reset = function()
local fr, fg, fb = rex.new("([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})"):match(fgColor)
fr, fg, fb = tonumber(fr, 16), tonumber(fg, 16), tonumber(fb, 16)
setFgColor(fr, fg, fb)
end
end
elseif bgColor then
reset = function()
local br, bg, bb = rex.new("([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})"):match(bgColor)
br, bg, bb = tonumber(br, 16), tonumber(bg, 16), tonumber(bb, 16)
setBgColor(br, bg, bb)
end
else
reset = function()
resetFormat()
end
end
for s, fr, fg, fb, br, bg, bb, r in rex.split(str, [[(?:\|c([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2}),?([0-9a-fA-F]{2})?([0-9a-fA-F]{2})?([0-9a-fA-F]{2})?|(\|r))]]) do
local fore, back, color
if fr and fg and fb then fore = { fr, fg, fb } end
if br and bg and bb then back = { br, bg, bb } end
if fore and back then
color = { ["fg"] = fore, ["bg"] = back }
elseif fore then
color = { ["fg"] = fore }
end
if s then table.insert(t, s) end
if color then table.insert(t, color) end
if r then table.insert(t, "r") end
end
for _, v in ipairs(t) do
if type(v) == 'table' then
local fr, fg, fb = unpack(v.fg)
fr, fg, fb = tonumber(fr, 16), tonumber(fg, 16), tonumber(fb, 16)
setFgColor(fr, fg, fb)
if v.bg then
local br, bg, bb = unpack(v.bg)
br, bg, bb = tonumber(br, 16), tonumber(bg, 16), tonumber(bb, 16)
setBgColor(br, bg, bb)
end
elseif v == "r" then
reset()
else
out(v)
end
end
resetFormat()
end