Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting or underlining output to stdout on Windows

Background

I need to port a Perl script from Linux to Windows. The script outputs to stdout and highlights and underlines specific words as needed. In Linux, this can be accomplished by surrounding the word(s) with system calls to tput:

tput smso and tput rmso for highlighting

tput smul and tput rmul for underlining

Question

Are there any system calls on Windows that can easily accomplish this functionality? If not, does anyone know a workaround that would accomplish similar results?

like image 509
robasia Avatar asked Sep 01 '25 02:09

robasia


1 Answers

If you're using Perl to output stuff, at least Win32::Console can do underline on Windows (10 onwards) as well:

my $win32_console Win32::Console->new();

        # Rendering is flakey under Windows 10
my $attr =  0x8000 # COMMON_LVB_UNDERSCORE, Windows 10 onwards
          | 0x4000 # COMMON_LVB_REVERSE_VIDEO, Windows 10 onwards
          ;
$console->Attr($attr);
$console->Write("Hello World");

But if you're just looking for a Really Quick porting fix, Win32::Console::ANSI will "magically" convert all ANSI sequences in your output to the appropriate console calls.

like image 109
Corion Avatar answered Sep 02 '25 16:09

Corion