⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch15_06.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 15.5. Changing Text Color (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:43:18Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch15_01.htm"TITLE="15. User Interfaces"><LINKREL="prev"HREF="ch15_05.htm"TITLE="15.4. Determining Terminal or Window Size"><LINKREL="next"HREF="ch15_07.htm"TITLE="15.6. Reading from the Keyboard"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_05.htm"TITLE="15.4. Determining Terminal or Window Size"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.4. Determining Terminal or Window Size"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch15_01.htm"TITLE="15. User Interfaces"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_07.htm"TITLE="15.6. Reading from the Keyboard"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.6. Reading from the Keyboard"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch15-chap15_changing_0">15.5. Changing Text Color</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-453">Problem</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch15-idx-1000005079-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005079-1"></A><ACLASS="indexterm"NAME="ch15-idx-1000005079-2"></A><ACLASS="indexterm"NAME="ch15-idx-1000005079-3"></A><ACLASS="indexterm"NAME="ch15-idx-1000005079-4"></A><ACLASS="indexterm"NAME="ch15-idx-1000005079-5"></A>You want text to appear in different colors on the screen. For instance, you want to emphasize a mode line or highlight an error message.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-459">Solution</A></H3><PCLASS="para">Use the CPAN module <ACLASS="indexterm"NAME="ch15-idx-1000005080-0"></A>Term::ANSIColor to send the ANSI color-change sequences to the user's terminal:</P><PRECLASS="programlisting">use Term::ANSIColor;print color(&quot;red&quot;), &quot;Danger, Will Robinson!\n&quot;, color(&quot;reset&quot;);print &quot;This is just normal text.\n&quot;;print colored(&quot;&lt;BLINK&gt;Do you hurt yet?&lt;/BLINK&gt;&quot;, &quot;blink&quot;);</PRE><PCLASS="para">Or, you can use convenience functions from Term::ANSIColor:</P><PRECLASS="programlisting">use Term::ANSIColor qw(:constants);print RED, &quot;Danger, Will Robinson!\n&quot;, RESET;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-483">Discussion</A></H3><PCLASS="para">Term::ANSIColor prepares escape sequences that some (but far from all) terminals will recognize. For example, if you normally launch a <EMCLASS="emphasis">color-xterm</EM>, this recipe will work. If you normally use the normal <EMCLASS="emphasis">xterm</EM> program, or have a vt100 in your kitchen, it won't.</P><PCLASS="para">There are two ways of using the module: either by calling the exported functions <CODECLASS="literal">color($attribute)</CODE><ACLASS="indexterm"NAME="ch15-idx-1000005081-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005081-1"></A> and <CODECLASS="literal">colored($text,</CODE> <CODECLASS="literal">$attribute)</CODE>, or by using convenience functions like <CODECLASS="literal">BOLD</CODE>, <CODECLASS="literal">BLUE</CODE>, and <CODECLASS="literal">RESET</CODE>.</P><PCLASS="para">Attributes can be a combination of colors and controls. The colors are black, red, green, yellow, blue, magenta, on_block, on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, and on_white. (Apparently orange and purple don't matter.) The controls are clear, reset, bold, underline, underscore, blink, reverse, and concealed. Clear and reset are synonyms, as are underline and underscore. Reset restores the colors to the way they were when the program started, and concealed makes foreground and background colors the same.</P><PCLASS="para">You can combine attributes:</P><PRECLASS="programlisting"># rhyme for the deadly coral snakeprint color(&quot;red on_black&quot;),  &quot;venom lack\n&quot;;print color(&quot;red on_yellow&quot;), &quot;kill that fellow\n&quot;;print color(&quot;green on_cyan blink&quot;), &quot;garish!\n&quot;;print color(&quot;reset&quot;);</PRE><PCLASS="para">We could have written this as:</P><PRECLASS="programlisting">print colored(&quot;venom lack\n&quot;, &quot;red&quot;, on_black&quot;);print colored(&quot;kill that fellow\n&quot;, &quot;red&quot;, &quot;on_yellow&quot;);print colored(&quot;garish!\n&quot;, &quot;green&quot;, &quot;on_cyan&quot;, &quot;blink&quot;);</PRE><PCLASS="para">or as:</P><PRECLASS="programlisting">use Term::ANSIColor qw(:constants);print BLACK, ON_WHITE, &quot;black on white\n&quot;;print WHITE, ON_BLACK, &quot;white on black\n&quot;;print GREEN, ON_CYAN, BLINK, &quot;garish!\n&quot;;print RESET;</PRE><PCLASS="para">Here, <CODECLASS="literal">BLACK</CODE> is a function exported from Term::ANSIColor.</P><PCLASS="para">It's important to <CODECLASS="literal">print</CODE> <CODECLASS="literal">RESET</CODE> or <CODECLASS="literal">color(&quot;reset&quot;)</CODE> at the end of your program if you're not calling <CODECLASS="literal">colored</CODE> for everything. Failure to reset your terminal will leave it displaying odd colors. You may want to use:</P><PRECLASS="programlisting">END { print color(&quot;reset&quot;) }</PRE><PCLASS="para">to ensure the colors will be reset when your program finishes.</P><PCLASS="para">Attributes that span lines of text can confuse some programs or devices. If this becomes a problem, either manually set the attributes at the start of each line, or use <CODECLASS="literal">colored</CODE> after setting the variable <CODECLASS="literal">$Term::ANSIColor::EACHLINE</CODE> to the line terminator:</P><PRECLASS="programlisting">$Term::ANSIColor::EACHLINE = $/;print colored(&lt;&lt;EOF, RED, ON_WHITE, BOLD, BLINK);This wayeach linehas its ownattribute set.EOF<ACLASS="indexterm"NAME="ch15-idx-1000005083-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005083-1"></A><ACLASS="indexterm"NAME="ch15-idx-1000005083-2"></A><ACLASS="indexterm"NAME="ch15-idx-1000005083-3"></A><ACLASS="indexterm"NAME="ch15-idx-1000005083-4"></A><ACLASS="indexterm"NAME="ch15-idx-1000005083-5"></A><ACLASS="indexterm"NAME="ch15-idx-1000005083-6"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-553">See Also</A></H3><PCLASS="para">The documentation for the Term::AnsiColor module from CPAN</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_05.htm"TITLE="15.4. Determining Terminal or Window Size"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.4. Determining Terminal or Window Size"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_07.htm"TITLE="15.6. Reading from the Keyboard"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.6. Reading from the Keyboard"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">15.4. Determining Terminal or Window Size</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">15.6. Reading from the Keyboard</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -