Code below produces the following output (escape sequence appearing as string): 'a ^[[33YELLOW^[[0m word'
use Curses;
use Term::ANSIColor;
initscr();
addstr(stdscr, "a ".color('yellow')."YELLOW".color('reset')." word");
refresh();
getch();
endwin();
Implementing a parser, with the following code, will produce the desired effects but will be expensive. Not to mention I feel like I'm reinventing the wheel.
use Curses;
use Term::ANSIColor;
initscr();
start_color();
init_pair(1, COLOR_YELLOW, COLOR_BLACK);
formatted_text(stdscr, "a ".color('yellow')."YELLOW".color('reset')."
+word");
refresh();
getch();
endwin();
sub formatted_text {
my ( $win, $text ) = @_;
# tokenize text on escape sequences
foreach ( split(/(\e\[[^m]+m)/, $text) ) {
# token starts with <ESC>, set attr and dont add text
if ( /^\e/ ) {
if ( $_ =~ quotemeta(color('yellow')) ) {
attron(COLOR_PAIR(1));
}
elsif ( $_ =~ quotemeta(color('reset')) ) {
attroff(COLOR_PAIR(1));
}
next;
}
addstr($win, $_);
}
}
|