使用 Perl Ansi 颜色为整个屏幕着色

Using Perl Ansi color to color entire screen

我在 Windows 中使用 Term::ANSIColor 和 Win32::Console::ANSI。 我经常在脚本中使用彩色打印来突出显示一些关键信息或其他内容。

尽管如此,我还是第一次尝试了一些可行的方法,但并不是我想要的那样。

我在 grey/white 背景上打印时的样子:

我想要的是(编辑成我想要的打印方式):

因此我想知道是否有人知道如何处理 ANSI 转义序列。我要用一种颜色填充我正在打印的整个作品,而不仅仅是换行符。

此外,这是我的脚本(我尝试了两种方法):

print color('bold blue on_white'), "\tnr\tisiNdebele\n";
print colored ['bold blue on_white'], "\tnso\tSepedi\n";

这不是主要问题。我只是想知道是否有人知道我该怎么做。

我的解决方案使用 Term::Size::Any to figure out how big the terminal is, and Text::Tabs\t 转换为空格。我们需要这样做,因为制表符只有一个字符宽,但在打印时会转换为 4、8 或任意数量的空格。

use strict;
use warnings;
use Term::Size::Any 'chars';
use Term::ANSIColor;
use Text::Tabs 'expand';

my ($columns, $rows) = chars;

sub full_background {
    my ($text) = @_;

    chomp $text;
    return sprintf(qq{%-${columns}s}, expand($text)) . "\n";
}

print color('bold blue on_white'), full_background( "\tnr\tisiNdebele\n" );
print colored ['bold blue on_red'], full_background( "\tnso\tSepedi\n" );

这是它的样子。