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

ama*_*tin 5 terminal perl ansi-escape

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

不过,这是我第一次尝试一些可行的方法,但并不完全是我想要的。

当我在灰色/白色背景上打印时,它看起来像什么:

它看起来像什么

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

它应该是什么样子

因此我想知道是否有人知道如何处理 ANSI 转义序列。我如何用颜色填充我要打印的整块内容,而不仅仅是换行符。

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

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

这不是一个主要问题。我只是想知道是否有人知道我如何才能实现这一目标。

sim*_*que 5

我的解决方案使用Term::Size::Any来计算终端有多大,并使用Text::Tabs将 s 转换\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" );
Run Code Online (Sandbox Code Playgroud)

它看起来是这样的。

在此输入图像描述