由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - how to print text in color?
相关主题
QT is LGPL noweclipse hot key
Java will store your chinese into Unicode有人用seam吗
显示email中文的问题关于Random怎么用?
怎麼得到字符串中的raw bytes?一个persistence api的问题
Hibernate sequences question最近碰到的笔试题
Be clear about one thing firstGenerate Unique ID for an existing Oracle Table
高手麻烦帮帮忙。。新手学编程database 的线程安全操作
c,java, 数据库内核,数据库应用one multi-threading question
相关话题的讨论汇总
话题: text话题: color话题: print话题: terminal话题: x1b
进入Java版参与讨论
1 (共1页)
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-(
相关主题
Be clear about one thing firsteclipse hot key
高手麻烦帮帮忙。。新手学编程有人用seam吗
c,java, 数据库内核,数据库应用关于Random怎么用?
进入Java版参与讨论
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!

1 (共1页)
进入Java版参与讨论
相关主题
one multi-threading questionHibernate sequences question
Re: Client-Server and actionPerformedBe clear about one thing first
Re: print problem, GUI guru please come in高手麻烦帮帮忙。。新手学编程
今天写了一个游戏c,java, 数据库内核,数据库应用
QT is LGPL noweclipse hot key
Java will store your chinese into Unicode有人用seam吗
显示email中文的问题关于Random怎么用?
怎麼得到字符串中的raw bytes?一个persistence api的问题
相关话题的讨论汇总
话题: text话题: color话题: print话题: terminal话题: x1b