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

I need to print some selected words to a text file with a different color? Is it possible? I tried this:
use Term::ANSIColor; open (FILE, ">text.txt") or die "Can't open file\n"; print FILE, color("red"), "Stop!\n", color("reset"); print FILE, color("green"), "Go!\n", color("reset");
Did not work! Any help?

edited: Tue Jul 16 06:53:50 2002 by jeffa - added code tags

Replies are listed 'Best First'.
(jeffa) Re: How can I change the text color
by jeffa (Bishop) on Jul 16, 2002 at 06:54 UTC
    First, remove the comma after FILE on the last two lines:
    print FILE color("red"), "Stop!\n", color("reset"); print FILE color("green"), "Go!\n", color("reset");
    Second, Term::ANSIColor uses ANSI escape sequences to color output. This is only useful in a standard terminal that complies with ECMA-048 and ISO 6429 - don't expect to be able to open the file in an editor and see pretty colors. :) Here is what text.txt should look like when viewed with the vim editor:
    ^[[31mStop! ^[[0m^[[32mGo! ^[[0m
    If you cat the file in a compliant terminal, you will see colors.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: How can I change the text color
by Abigail-II (Bishop) on Jul 16, 2002 at 09:54 UTC
    Files are just streams of bytes. They don't have any concept of colours. Colours can be generated in many ways, but they all have to do with turning on pixels on the screen. Term::ANSIColor is one way of doing it, and it only works when addressing a terminal - and not even any terminal, only those terminals that understand the ANSI way of encoding colours (and assosiated stuff).

    So, if you are going to put the escape sequences in a file, to get the pretty colours back, you need to send the escape sequences directly to the terminal. Using cat is one option.

    Abigail

Re: How can I change the text color
by opolat (Novice) on Jul 27, 2002 at 18:02 UTC
    I haven't used this widget, but sounds like what you are looking for! Tk::TextANSIColor

    NAME

    Tk::TextANSIColor - Tk::Text widget with support for ANSI color escape codes

    SYNOPSIS

    use Tk::TextANSIColor; $wid = $mw->TextANSIColor(?options,...?); $wid->insert($pos, $string, ?taglist, ?string, ?taglist); $string_with_escape_codes = $wid->getansi('0.0','end'); use Term::ANSIColor; $red = color('red'); # Retrieve color codes $bold = color('bold'); $wid->insert('end', "$red red text $bold with bold\n");
    DESCRIPTION

    This widget extends the capabilities of the standard Tk::Text widget by adding support for ANSI color escape codes. When these escape codes are detected they are replaced by equivalent tags. This widget was developed to solve the problem associated with driving a scrolling status display on a GUI as well as a status display going to an Xterm without having to know whether an xterm or Tk window is receiving the status information. Mainly used in conjunction with a tied filehandle:

    $text = $MW->TextANSIColor->pack; tie *TEXT, "Tk::TextANSIColor", $text; $info = colored("Some information\n", 'red'); # Print information to all filehandles print TEXT $info print STDOUT $info
    Currently the Term::ANSIColor module is required in order to decode the escape codes (and probably to generate them in the first place).

    METHODS

    The following methods are available in addition to those described in the documentation for Tk::Text:

    getansi

    $widget->getansi(index1, ?index2?)
    Similar to the standard get method for Tk::Text widgets, except it returns a range of characters from the text with the ANSI escape-codes embedded. This allows one to insert a string containing ANSI escape-codes into the widget, manipulate them, and fetch them back from the widget with the escape codes intact. The return value will be all the characters in the text starting with the one whose index is index1 and ending just before the one whose index is index2 (the character at index2 will not be returned). If index2 is omitted then the single character at index1 is returned. If there are no characters in the specified range (e.g. index1 is past the end of the file or index2 is less than or equal to index1) then an empty string is returned. If the specified range contains embedded windows, no information about them is included in the returned string. Use the standard get method to fetch the string without ANSI escape-codes. TAGS

    This widget uses the following tags internally:

    ANSIbd - bold ANSIul - underline ANSIfgCOL - foreground color ANSIbgCOL - background color
    where COL can be one of black, red, green, yellow, blue, magenta, cyan or white. If required, the tags can be altered after the widget is created by using the tagConfigure() method. e.g.:
    $widget->tagConfigure('ANSIfgred', -foreground => 'blue');
    in order to make 'red' appear 'blue'.

    REQUIREMENTS

    This modules requires the Term::ANSIColor module. The Tk module is also required. SEE ALSO

    Tk::Text, Term::ANSIColor

    AUTHOR

    Tim Jenness (<t.jenness@jach.hawaii.edu>)