On 06/03/2018 05:41 PM, Xin Cheng wrote:
Hi,

I am trying to make a program to do grep with perl6 regular expression, and I would like to colorize the matched part to the terminal. So the following is what I wrote

sub MAIN(Str $pattern,Str $filename){
     for $filename.IO.lines -> $line  {
         my Str $temp = $line;
        if $temp ~~ s/ (<$pattern>) /\\x1b\[31m$0\\x1b\[0m/ {say $temp}; # if no <> surrounding $pattern it becomes literal.
     }
}

And I named the program as grep6, and I tried it in zsh as

 > grep6 'M.*N' =grep6

And I got,

sub \x1b[31mMAIN\x1b[0m(Str $pattern,Str $filename){

How do I turn the string into color?

Thanks!

Xin


Hi Xin,

I wrote my own module for this.  You can take it apart
to fit your needs.  If the scrolling drives you too crazy,
try this link:

     http://vpaste.net/ulvWc

So far, only tested in Linux.

I have heard there are other operating systems out there,
but have yet to verify their actual existence.

:-)

-T

<code PrintColors>
unit module PrintColors;

#`{

There subs give you the ability to print to the stand output and standard error
     with color.

     To use these, place the following at the top(ish) of your program
        use lib "/home/linuxutil";
use PrintColors; # qx[ PrintRed PrintGreen PrintBlue PrintErr PrintRedErr PrintGreenErr PrintBlueErr ]

}

use Terminal::ANSIColor;  # qx[ color ];

sub PrintRed ( $Str ) is export { print color('bold'), color('red'), "$Str", color('reset'); } sub PrintGreen ( $Str ) is export { print color('bold'), color('green'), "$Str", color('reset'); } sub PrintBlue ( $Str ) is export { print color('bold'), color('blue'), "$Str", color('reset'); }

sub PrintErr   ( $Str ) is export { $*ERR.print: "$Str"; }

sub PrintRedErr ( $Str ) is export { $*ERR.print: color('bold'), color('red'), "$Str", color('reset'); } sub PrintGreenErr ( $Str ) is export { $*ERR.print: color('bold'), color('green'), "$Str", color('reset'); } sub PrintBlueErr ( $Str ) is export { $*ERR.print: color('bold'), color('blue'), "$Str", color('reset'); }
</code>

Reply via email to