develooper Front page | perl.perl6.users | Postings from June 2018

How to print colorized text to the terminal

Thread Next
From:
Xin Cheng
Date:
June 4, 2018 00:41
Subject:
How to print colorized text to the terminal
Message ID:
63367AB4-D944-4588-B5E2-B701BBB1C714@gmail.com
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
Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About