G*********a 发帖数: 1080 | 1 i guess the output text is always black, or the default color the terminal.
The only way I can think out to print the text in color without making any
GUI is:
to write output in HTML or any Makeup language which extra brackets, then it
can be viewed in a browser and the colors will show. Does this sound
plausible?
Any other method? thanks. |
m******t 发帖数: 2416 | 2 You can probably try printing escaped ANSI color code, but it's up to the
terminal at run-time to honor it. |
g*****g 发帖数: 34805 | 3 Or you can print out xml, and use xsl to highlight it.
.
it
【在 G*********a 的大作中提到】 : i guess the output text is always black, or the default color the terminal. : The only way I can think out to print the text in color without making any : GUI is: : to write output in HTML or any Makeup language which extra brackets, then it : can be viewed in a browser and the colors will show. Does this sound : plausible? : Any other method? thanks.
|
G*********a 发帖数: 1080 | 4 this sounds cool, but i couldn't figure out how to do it, I just googled
this out: http://www.termsys.demon.co.uk/vtansi.htm
it seems you are able to change the setting of ANSI color at terminal... but
how, do u mind to give me an example command?
so, after all, i still need to print the tags in the output text then it
will be translated into color on the terminal, right?
【在 m******t 的大作中提到】 : You can probably try printing escaped ANSI color code, but it's up to the : terminal at run-time to honor it.
|
c*****t 发帖数: 1879 | 5 For red hello world
System.out.println ("\x1B[31mHello World");
It could be \1x1B[31;m as well, not too sure :)
but
【在 G*********a 的大作中提到】 : this sounds cool, but i couldn't figure out how to do it, I just googled : this out: http://www.termsys.demon.co.uk/vtansi.htm : it seems you are able to change the setting of ANSI color at terminal... but : how, do u mind to give me an example command? : so, after all, i still need to print the tags in the output text then it : will be translated into color on the terminal, right?
|
G*********a 发帖数: 1080 | 6 Thanks, but it doesn't work:
javac Hello.java
Hello.java:4: illegal escape character
{ System.out.println("\x1B[31;mHello");}
^
1 error
After changing it into : System.out.println("\\x1B[31;mHello");
it just print out either: \x1B[31;mHello or \x1B[31mHello
【在 c*****t 的大作中提到】 : For red hello world : System.out.println ("\x1B[31mHello World"); : It could be \1x1B[31;m as well, not too sure :) : : but
|
c*****t 发帖数: 1879 | 7 try
"\033[..." instead.
【在 G*********a 的大作中提到】 : Thanks, but it doesn't work: : javac Hello.java : Hello.java:4: illegal escape character : { System.out.println("\x1B[31;mHello");} : ^ : 1 error : After changing it into : System.out.println("\\x1B[31;mHello"); : it just print out either: \x1B[31;mHello or \x1B[31mHello
|
G*********a 发帖数: 1080 | 8 still can't. have u got it successfully?
【在 c*****t 的大作中提到】 : try : "\033[..." instead.
|
m******t 发帖数: 2416 | 9
I believe Java prints strings in unicode, whereas you need to output the
escaping sequences in raw bytes. This code here prints "Hello World" in red
on white background (works in my cygwin+rxvt, at least):
public class ANSI {
public static void main(String[] args) throws Exception {
System.out.write("\u001b[31;47m".getBytes("ISO-8859-1"));
System.out.print ("Hello World");
System.out.write("\u001b[0m".getBytes("ISO-8859-1"));
System.out.println();
}
}
HTH
【在 G*********a 的大作中提到】 : still can't. have u got it successfully?
|
m******t 发帖数: 2416 | 10 Argh, this BBS has _got_ to stop trimming spaces on every line! 8-( |
|
|
G*********a 发帖数: 1080 | 11 great, this one works for me too. thank u very much! :)
red
【在 m******t 的大作中提到】 : Argh, this BBS has _got_ to stop trimming spaces on every line! 8-(
|
m******t 发帖数: 2416 | 12
Heh, just be aware that it's not going to look pretty in a non-ANSI terminal
- I just tried it in DOS Prompt. 8-)
【在 G*********a 的大作中提到】 : great, this one works for me too. thank u very much! :) : : red
|
c*****t 发帖数: 1879 | 13 public class Test
{
public static void main (String[] args)
{
System.out.println ("\033[31mHello World!");
}
}
turns text red on PuTTY.
【在 G*********a 的大作中提到】 : still can't. have u got it successfully?
|
G*********a 发帖数: 1080 | 14 Thank you very much, coconut. I tried it and it does the same on my terminal
at Mac. It turns everything later at the terminal to red too. It is great
to know it although what I wanted was to highlight the partial text of the
output only, as magicfat's way. Thanks!
【在 c*****t 的大作中提到】 : public class Test : { : public static void main (String[] args) : { : System.out.println ("\033[31mHello World!"); : } : } : turns text red on PuTTY.
|
m******t 发帖数: 2416 | 15 Coconut's code is better, actually. For some reason I thought the control
sequences had to be in ascii, which turns out to be unnecessary. You can
highlight any part by outputting control sequences at any point, for example:
System.out.println("\u001b[31;47mRED ON WHITE\u001b[0m Normal");
terminal
【在 G*********a 的大作中提到】 : Thank you very much, coconut. I tried it and it does the same on my terminal : at Mac. It turns everything later at the terminal to red too. It is great : to know it although what I wanted was to highlight the partial text of the : output only, as magicfat's way. Thanks!
|