简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM