【问题标题】:How do I color output text from Perl script on Windows?如何在 Windows 上为 Perl 脚本的输出文本着色?
【发布时间】:2010-11-08 09:42:53
【问题描述】:

我想使用 Perl 打印命令为打印到控制台的文本设置颜色格式。

在我的情况下,脚本只能在 WinXP-DOS 命令行下运行,但如果它是独立于操作系统的,那就太好了,尽管我宁愿将它绑定到 WinXP 而不是下载单独的包。

【问题讨论】:

    标签: perl winapi console


    【解决方案1】:

    基于 mphuie 的示例,使其更具跨平台性:

        use Term::ANSIColor;
        use Win32::Console;
    
        if ($^O =~ /win/) {
            our $FG_BLUE;
            our $FG_YELLOW;
            our $FG_RED;
            our $BG_GREEN;
            my $CONSOLE = Win32::Console->new(STD_OUTPUT_HANDLE);
            my $attr = $CONSOLE->Attr(); # Get current console colors
            $blue   = sub {$CONSOLE->Attr($FG_BLUE);return};
            $reset  = sub {$CONSOLE->Attr($attr);return};
            $yellow = sub {$CONSOLE->Attr($FG_YELLOW);return};
            $red    = sub {$CONSOLE->Attr($FG_RED);return};
        } else {
            $blue   = sub {return color('bold blue')};
            $reset  = sub {return color('reset')};
            $yellow = sub {return color('yellow')};
            $red    = sub {return color('red')};
        }
    

    快速说明:从字符串内部调用函数时,终端颜色不会立即改变,因此:

        print "${\$blue->()} this is blue\n";
        print "${\$blue->()}This is... not blue${\$reset->()}\n";
        print "this is Blue ${\$blue->()}\n";
        print "this is reset${\$reset->()}\n";
    

    【讨论】:

      【解决方案2】:

      毕竟这是对我最有效的方法:

      1) 安装了 Win32::Console::ANSI(注意这和 Win32::Console 不一样)

      perl -MCPAN -e shell
      cpan> install Win32::Console::ANSI
      

      2) 如果这个模块是在 Term::ANSIColor 之前加载的,你可以使用标准的 Term::ANSIColor API,它确实可以工作(我在 Windows 7 上试过)。

      use Win32::Console::ANSI;
      use Term::ANSIColor;
      
      print color("blue"), "blue\n", color("reset");
      print "normal\n";
      

      【讨论】:

        【解决方案3】:

        Win32::Console - 这是一个例子

        use Win32::Console;
        my $CONSOLE = Win32::Console->new(STD_OUTPUT_HANDLE);
        my $attr = $CONSOLE->Attr(); # Get current console colors
        $CONSOLE->Attr($FG_YELLOW | $BG_GREEN); # Yellow text on green
        
        print "This is a test\n";
        
        $CONSOLE->Attr($attr); # Set console colors back to original
        

        【讨论】:

          【解决方案4】:
          system("color A"); #DOS command, change text color to lime
          
          system("color 7"); #DOS command, change text color to white
          

          但是,这些命令会更改整个屏幕上的文本颜色。输入“颜色?”在 DOS 窗口中查看颜色选项

          我在 Windows 上使用草莓 perl,但我没有 Win32::Console 包。要在控制台中安装此包类型:

          perl -MCPAN -e shell

          安装 Win32::Console

          退出

          【讨论】:

            【解决方案5】:

            对于任何支持ANSI escape codes 的终端,您都可以使用 CPAN 上提供的Term::ANSIColor package。

            来自维基百科页面:

            Windows 版本中的控制台窗口 基于 NT (Windows NT 4.0, Windows 2000、Windows XP、Windows Server 2003、 Windows Vista 和 Windows Server 2008) 本身不支持 ANSI Escape 序列,虽然一些支持是 可能。

            不知道比这更多的特定于 Windows 的信息,我是 POSIX 人。 :-)

            【讨论】:

            猜你喜欢
            • 2019-11-27
            • 1970-01-01
            • 2023-04-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-16
            • 2020-02-26
            • 1970-01-01
            相关资源
            最近更新 更多