Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Capturing ANSI color escape codes with backticks

by Mr_Micawber (Beadle)
on Jan 08, 2010 at 17:28 UTC ( [id://816367]=perlquestion: print w/replies, xml ) Need Help??

Mr_Micawber has asked for the wisdom of the Perl Monks concerning the following question:

I'm playing with a greppish directory listing script, and I want to retain the colors that my normal linux shell uses for ls. I know about Term::ANSIColor, but how do I capture the raw output from the backtick operator, including the escape codes?
  • Comment on Capturing ANSI color escape codes with backticks

Replies are listed 'Best First'.
Re: Capturing ANSI color escape codes with backticks
by moritz (Cardinal) on Jan 08, 2010 at 17:41 UTC
    Backticks do capture the raw output, but on most systems grep is configured to omit the ANSI color codes when it writes to a pipe (and not to a terminal) via the option --color=auto.

    You check by simply writing

    $ echo foo | grep foo # output in read on my terminal foo $ echo foo | grep foo | cat # output in grey on my terminal foo

    Use --color=always to force color codes.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Capturing ANSI color escape codes with backticks
by shawnhcorey (Friar) on Jan 08, 2010 at 18:15 UTC

    From the ls man page:

    By default, color is not used to distinguish types of files. That is equivalent to using --color=none. Using the --color option without the optional WHEN argument is equivalent to using --color=always. With --color=auto, color codes are output only if standard output is connected to a terminal (tty). The environment variable LS_COLORS can influence the colors, and can be set easily by the dircolors command.

    Have you tried the --color=always option?

      The --color=always option worked great. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://816367]
Approved by moritz
Front-paged by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found