Re: How to print colorized text to the terminal
> On Jun 3, 2018, at 7:41 PM, Xin Cheng wrote: > > 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. —snip-- > if $temp ~~ s/ (<$pattern>) /\\x1b\[31m$0\\x1b\[0m/ {say $temp} —snip— Change this:s/ (<$pattern>) /\\x1b\[31m$0\\x1b\[0m/ to this:s/ (<$pattern>) /\x1b[31m$0\x1b[0m/ and your example code will correctly highlight the pattern in the (terminal) output. The doubled backslash in your original code becomes a literal backslash; “\\x1b” is 4 characters long, “\x1b” is 1 character long (the escape character). Also, you would need to back-whack the `[` only on the left-hand side of `s///` (the pattern, which uses Regex syntax), not on the right-hand side (the replacement, which uses double-quoted string syntax). If you do not want to use Terminal::ANSIColor, I recommend that you save yourself some future confusion by isolating your escape sequences, like so: constant $color_red = "\e[31m"; constant $color_off = "\e[0m"; sub MAIN ( Str $pattern, Str $filename ) { for $filename.IO.lines -> $line { my Str $temp = $line; # if no <> surrounding $pattern it becomes literal. if $temp ~~ s/ (<$pattern>) /$color_red$0$color_off/ { say $temp; } } } — Hope this helps, Bruce Gray (Util of PerlMonks)
Re: need second pair of eyes
Hi! On Sun, Jun 03, 2018 at 03:05:33PM -0700, ToddAndMargo wrote: > On 06/03/2018 02:54 PM, Brad Gilbert wrote: > > But in this case it is even better to use -I and -M > > > > p6 -I. -MRunNoShell -e '( my $a, my $b ) = > > RunNoShell::RunNoShell("ls *.pm6"); say $a;' > > $ perl6 -I -MRunNoShell '( my $a, my $b ) = RunNoShell::RunNoShell("ls > \*.pm6"); say $a;' You forgot -e to tell p6 to eval the following string Greetings, domm -- #!/usr/bin/perl http://domm.plix.at for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}
Re: How to print colorized text to the terminal
Thanks Bruce, This is great, and It works as I expected. I appreciate all the helps. Regards Xin > On Jun 4, 2018, at 9:04 AM, Bruce Gray wrote: > > >> On Jun 3, 2018, at 7:41 PM, Xin Cheng wrote: >> >> 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. > —snip-- >>if $temp ~~ s/ (<$pattern>) /\\x1b\[31m$0\\x1b\[0m/ {say $temp} > > —snip— > > Change this: s/ (<$pattern>) /\\x1b\[31m$0\\x1b\[0m/ > to this: s/ (<$pattern>) /\x1b[31m$0\x1b[0m/ > and your example code will correctly highlight the pattern in the (terminal) > output. > > The doubled backslash in your original code becomes a literal backslash; > “\\x1b” is 4 characters long, “\x1b” is 1 character long (the escape > character). Also, you would need to back-whack the `[` only on the left-hand > side of `s///` (the pattern, which uses Regex syntax), not on the right-hand > side (the replacement, which uses double-quoted string syntax). > > If you do not want to use Terminal::ANSIColor, I recommend that you save > yourself some future confusion by isolating your escape sequences, like so: > > constant $color_red = "\e[31m"; > constant $color_off = "\e[0m"; > > sub MAIN ( Str $pattern, Str $filename ) { >for $filename.IO.lines -> $line { >my Str $temp = $line; > ># if no <> surrounding $pattern it becomes literal. >if $temp ~~ s/ (<$pattern>) /$color_red$0$color_off/ { >say $temp; >} >} > } > > — > Hope this helps, > Bruce Gray (Util of PerlMonks) > >
Re: need second pair of eyes
On 06/03/2018 03:15 PM, Thomas Klausner wrote: Hi! On Sun, Jun 03, 2018 at 03:05:33PM -0700, ToddAndMargo wrote: On 06/03/2018 02:54 PM, Brad Gilbert wrote: But in this case it is even better to use -I and -M p6 -I. -MRunNoShell -e '( my $a, my $b ) = RunNoShell::RunNoShell("ls *.pm6"); say $a;' $ perl6 -I -MRunNoShell '( my $a, my $b ) = RunNoShell::RunNoShell("ls \*.pm6"); say $a;' You forgot -e to tell p6 to eval the following string Greetings, domm Thank you