Ncurses escapes with Perl

275 Views Asked by At

Writing Perl code. I know I can use Term::ANSIColor and do

print color('red') . 'hello world';

However, in this circumstance I'm using a curses library and I'd like to create a label where some words are in red, some in yellow, etc.. All I can manipulate is a string, which I will pass to the library for rendering. If I do

$string = color('red') . 'hello world' . color('yellow') . ' another word';

then when I pass $string to the library for printing, it prints the verbatim ANSI escape sequences. Is there a way to do what I'm trying to do or must I manually manage the drawing of the label with attron() and friends?

1

There are 1 best solutions below

1
On

I'm on Windows, which does not support ANSI color coding natively. You have to use Win32::Console::ANSI. Without it, the following code does what your system is doing. With it, colors are produced. So you are either on Windows, or your terminal does not support ANSI colors. If you are on Windows, just add the Win32 module.

use strict;
use warnings;

use Win32::Console::ANSI;
use Term::ANSIColor;

my $string = color('red') . 'hello world' . color('yellow') . ' another word';
print $string;

exit 0;