Re: How can I do color text output?
Re: How can I do color text output?
- Subject: Re: How can I do color text output?
- From: Philip Aker <email@hidden>
- Date: Mon, 16 Feb 2009 04:30:03 -0800
On 2009-02-15, at 09:36:53, Ellie Baker wrote:
I am new to Xcode and was wondering if someone could advise me about
the best approach to writing a simple program that outputs text in
color. It could be output to a file or to the console — I just need
to be able to print the color output. For example, how can I write
“hello world” with the characters in different colors? Can I do
this using C++ and the console for output? Or must I use cocoa (if
so, how?)? Or is there some simpler option? I only need to use a
few basic colors (nothing fancy).
One easy thing to do is to write an html document as a string in your
code. You can then process it with the shell tools 'textutil' and 'lp'
depending on your needs.
The following is the basic idea (remember to encode any apostrophes in
your text as ' ).
/****************/
#include <iostream>
#include <string>
/*
g++ -o prcolors prcolors.cpp
*/
std::string do_head( std::string title ) {
std::string head( "<html><head><title>" );
head.append( title );
head.append( "</title><meta http-equiv=\"Content-Type\" content=
\"text/html;charset=UTF-8\"/><meta http-equiv=\"Content-Style-Type\"
content=\"text/css\"/>" );
head.append( "</head><body>" );
return head;
}
std::string do_tail() {
std::string tail( "</body></html>" );
return tail;
}
std::string do_element( std::string element, std::string style,
std::string text ) {
std::string elmt( "<" );
elmt.append( element );
elmt.append( " style=\"" );
elmt.append( style );
elmt.append( "\">" );
elmt.append( text );
elmt.append( "</" );
elmt.append( element );
elmt.append( ">" );
return elmt;
}
//echo '' | textutil -convert rtf -stdin -stdout | lp -
int main( int argc, char *argv[] ) {
int err = 0;
std::string title( "Some Title" );
std::string html = do_head( title );
html.append( do_element( "h1", "color:#400;", title ) );
html.append( do_element( "p", "color:#408;", "The document text" ) );
html.append( do_tail() );
/* output html */
//std::cout << html << std::endl;
std::string command( "echo '" );
command.append( html );
/* output and open rtf */
command.append( "' | textutil -stdin -stdout -convert rtf | open -
f;" );
/* print rtf */
//command.append( "' | textutil -stdin -stdout -convert rtf | lp -;" );
system( command.c_str() );
return err;
}
Philip Aker
echo email@hidden@nl | tr a-z@. p-za-o.@
Democracy: Two wolves and a sheep voting on lunch.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden