my %esc2action = ( color('yellow') => sub { attron(COLOR_PAIR(1)) }, color('reset') => sub { attroff(COLOR_PAIR(1)) }, ); sub formatted_text { my ($win, $text) = @_; # tokenize text on escape sequences for my $token (split /(\e\[[^m]+?m)/, $text) { my $act = $esc2action{$token}; if ($act) { $act->(); } else { addstr($win, $token); } } }