cmd中颜色输出问题

发布于 2024-10-08 17:02:49 字数 731 浏览 3 评论 0原文

这是代码:

use Cwd;
use Win32::Console::ANSI;# installed module with ppm
use Term::ANSIColor;

$parent =  cwd();

@files = <*>;
print "\n";
foreach (@files)
{
    $child = "$parent/$_";
    if (-f $_){print "$child\n";}; #file test
    if (-d $_)#directory test
    {
        print color("green")."$child/\n".color("reset");
        my($command) = "cd $child/";#submerge one directory down ( recursive here?)
        $command=~s/\//\\/g;
        system( $command );
    };
}

我遇到的问题是输出着色的方式。 我期望得到的是黑色背景上绿色的“某个目录”。 相反,我得到的背景颜色为绿色,文本为黑色,有时随机为白色。 我在使用 color() 的所有其他代码上都遇到这个问题。 我注意到重新启动后问题就消失了。 另外,我怀疑当我运行其他一些 Perl 代码时,问题会再次影响 DOS 及其所有窗口。基本上,一旦出错,它就会一直存在,直到每个 color() 实例重新启动为止。看起来像是一个小故障。请帮忙。

Here is the code:

use Cwd;
use Win32::Console::ANSI;# installed module with ppm
use Term::ANSIColor;

$parent =  cwd();

@files = <*>;
print "\n";
foreach (@files)
{
    $child = "$parent/$_";
    if (-f $_){print "$child\n";}; #file test
    if (-d $_)#directory test
    {
        print color("green")."$child/\n".color("reset");
        my($command) = "cd $child/";#submerge one directory down ( recursive here?)
        $command=~s/\//\\/g;
        system( $command );
    };
}

The problem I have is with the way output gets colored.
What I am expecting to get is "some directory" in green on black background.
Instead I get background color in green and text in black, sometimes white randomly.
I have this problem on every other code where I use color().
I noticed the problem goes away after I restart.
Also, my suspicion is that the problem returns affecting DOS and all its windows when I run some other perl code. Basically, once it goes wrong it is there until restart for every instance of color(). It looks like a glitch. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毅然前行 2024-10-15 17:02:49

我没有观察到这个问题。然而,您正在使用 system (它启动不同的 shell 来执行命令,因此实际上不会更改您的目录)这一事实可能会造成干扰。或者,如果您使用 CTRL-C 中断脚本,控制台可能会处于不确定状态。

这是实现您想要的更好的方法:

#!/usr/bin/perl

use Cwd;
use File::Spec::Functions qw( catfile canonpath );
use Win32::Console::ANSI;
use Term::ANSIColor;

local $SIG{INT} = sub { print color('reset') };

my $top = cwd;

print_contents($top);

sub print_contents {
    my ($dir) = @_;

    opendir my $dir_h, $dir
        or die "Cannot open directory: '$dir': $!";

    while ( defined (my $entry = readdir $dir_h) ) {
        next if $entry =~ /^[.][.]?\z/;
        my $path = canonpath catfile $dir => $entry;
        if ( -f $path ) {
            print "$path\n";
        }
        elsif ( -d $path ) {
            print color('green'), $path, color('reset'), "\n";
            print_contents($path);
        }
    }

    closedir $dir_h
        or die "Cannot close directory: '$dir': $!";
}

I do not observe the problem. However, the fact that you are using system (which starts a different shell to carry out the command and therefore really does not change your directory) may be what's interfering. Alternatively, the console might be left in an indeterminate state if you are using CTRL-C to break out of the script.

Here is a better way of accomplishing what you want:

#!/usr/bin/perl

use Cwd;
use File::Spec::Functions qw( catfile canonpath );
use Win32::Console::ANSI;
use Term::ANSIColor;

local $SIG{INT} = sub { print color('reset') };

my $top = cwd;

print_contents($top);

sub print_contents {
    my ($dir) = @_;

    opendir my $dir_h, $dir
        or die "Cannot open directory: '$dir': $!";

    while ( defined (my $entry = readdir $dir_h) ) {
        next if $entry =~ /^[.][.]?\z/;
        my $path = canonpath catfile $dir => $entry;
        if ( -f $path ) {
            print "$path\n";
        }
        elsif ( -d $path ) {
            print color('green'), $path, color('reset'), "\n";
            print_contents($path);
        }
    }

    closedir $dir_h
        or die "Cannot close directory: '$dir': $!";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文