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, $_); } }

In reply to Re^2: Handling ANSI Escape Sequences for Printing to Curses by atancasis
in thread Handling ANSI Escape Sequences for Printing to Curses by atancasis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.