I then have my own Colors to Styles function written a long time ago.
It relies on having defined a table called atcode_to_color_value which, as the name suggests, translates Aardwolf's color codes into MUSHclient color numbers.
-- Converts text with Aardwolf normal and xterm 256 color codes into MUSHclient style runs
function ColorsToStyles (Text, default_foreground_code, default_background_code)
if not default_foreground_code then
default_foreground_code = "@w"
end
default_foreground = atcode_to_color_value[default_foreground_code]
if not default_background_code then
default_background_code = "@k"
end
default_background = atcode_to_color_value[default_background_code]
if Text:match ("@") then
astyles = {}
-- make sure we start with a color
if Text:sub(1, 1) ~= "@" then
Text = default_foreground_code .. Text
end -- if
Text = Text:gsub ("@%-", "~") -- fix tildes
Text = Text:gsub ("@@", "\0") -- change @@ to 0x00
Text = Text:gsub ("@x([^%d])","%1") -- strip invalid xterm codes (non-number)
Text = Text:gsub ("@x[3-9]%d%d","") -- strip invalid xterm codes (300+)
Text = Text:gsub ("@x2[6-9]%d","") -- strip invalid xterm codes (260+)
Text = Text:gsub ("@x25[6-9]","") -- strip invalid xterm codes (256+)
Text = Text:gsub ("@[^xrgybmcwDRGYBMCWd]", "") -- strip hidden garbage
for color, text in Text:gmatch ("@(%a)([^@]+)") do
text = text:gsub ("%z", "@") -- put any @ characters back
if color == "x" then -- xterm 256 colors
code,text = text:match("(%d%d?%d?)(.*)")
color = color..code
end
if #text > 0 then
table.insert (astyles, { text = text,
bold = (color == color:upper()),
length = #text,
textcolour = atcode_to_color_value[color] or default_foreground,
backcolour = default_background })
end -- if some text
end -- for each color run.
return astyles
end -- if any color codes at all
-- No color codes, create a single style.
return { { text = Text,
length = #Text,
textcolour = default_foreground,
backcolour = default_background } }
end -- function ColorsToStyles
If you want to go from ANSI to Styles, you could chain these two functions together. |