You can use regexps to break up the string into ANSI sequences, along these lines:
test = "\027[0;1;33mYou tell foo, \"// Test string.\"\027[0;37mbar"
for sequence, text in string.gmatch (test, "\027%[([0-9;]+)m([^\027]+)") do
for ansi in string.gmatch (sequence, "%d+") do
print ("ANSI code: ", ansi)
end -- for
print ("Text: ", text)
end -- for
Output:
ANSI code: 0
ANSI code: 1
ANSI code: 33
Text: You tell foo, "// Test string."
ANSI code: 0
ANSI code: 37
Text: bar
I seem to recall doing this a while back but don't remember where.
You can now take those sequences and do something meaningful with them, to apply to those parts of text.
From the MUSHclient source, these are what the codes do:
// ANSI Colour Codes
#define ANSI_RESET 0
#define ANSI_BOLD 1
#define ANSI_BLINK 3
#define ANSI_UNDERLINE 4
#define ANSI_SLOW_BLINK 5
#define ANSI_FAST_BLINK 6
#define ANSI_INVERSE 7
#define ANSI_CANCEL_BOLD 22
#define ANSI_CANCEL_BLINK 23
#define ANSI_CANCEL_UNDERLINE 24
#define ANSI_CANCEL_SLOW_BLINK 25
#define ANSI_CANCEL_INVERSE 27
#define ANSI_TEXT_BLACK 30
#define ANSI_TEXT_RED 31
#define ANSI_TEXT_GREEN 32
#define ANSI_TEXT_YELLOW 33
#define ANSI_TEXT_BLUE 34
#define ANSI_TEXT_MAGENTA 35
#define ANSI_TEXT_CYAN 36
#define ANSI_TEXT_WHITE 37
#define ANSI_TEXT_256_COLOUR 38
#define ANSI_SET_FOREGROUND_DEFAULT 39
#define ANSI_BACK_BLACK 40
#define ANSI_BACK_RED 41
#define ANSI_BACK_GREEN 42
#define ANSI_BACK_YELLOW 43
#define ANSI_BACK_BLUE 44
#define ANSI_BACK_MAGENTA 45
#define ANSI_BACK_CYAN 46
#define ANSI_BACK_WHITE 47
#define ANSI_BACK_256_COLOUR 48
#define ANSI_SET_BACKGROUND_DEFAULT 49
So in other words, my first test sequence does:
- 0 - reset to standard (ie. not bold, not underline, etc.)
- 1 - set bold text
- 33 - text to yellow (foreground colour)
The second sequence does:
- 0 - reset to standard (ie. not bold, not underline, etc.)
- 37 - text to white (foreground colour)
My first regexp looks for
ESC [ <some numbers or ";"> m. Thus it would fail to find the very first words on the line (if any) if they are not preceded by an escape sequence. You could inject a dummy one (eg. ESC[0m ) or handle that as a special case of a style run that does not change colours from the previous one.
This should give you enough information to get started.
[EDIT] Bear in mind the second regexp in my example returns strings. You need to use
tonumber on them if you want to compare them directly to numbers.