在命令行java中突出显示文本 [英] Highlighting text in commandline java

查看:238
本文介绍了在命令行java中突出显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个重新创建unix cal程序的任务,除了一个部分,很简单。在当天,它突出显示数字。我不知道如何做到这一点。

I have an assignment to recreate the unix cal program, fairly straightforward except for one part. On the current day, it highlights the number. I have no idea how to do this. Any idea on how to do it in Java?

图片:

推荐答案


ANSI颜色代码

ANSI color codes

通过展开转义序列设置提示的颜色
\e [sm,其中s是以分号分隔的列表ANSI颜色代码:
\e [31; 44; 1m将前景颜色设置为红色,背景设置为
蓝色,字体设置为粗体字; (\e是ASCII Escape
字符,不要忘记使用m
字符终止序列。)

The color of the prompt is set by expanding the escape sequence "\e[sm", where s is a semicolon-delimited list of ANSI color codes: "\e[31;44;1m" would set the foreground color to red, the background to blue, and the font in bold face; (The "\e" is the ASCII Escape character. Don’t forget to terminate the sequence with the "m" character.)

环境变量中的二进制序列需要通过
指示符指定它们具有零宽度,否则shell不会
正确计算提示宽度。 Bash用斜线括号[..]包含这样的东西
,而Tcsh使用百分号括号%{..
%}。

Binary sequences in the environment variables need to be set off by indicators that they have zero width, or else the shell won’t calculate correctly the width of the prompt. Bash encases such things with slash-brackets "[ .. ]", whereas Tcsh uses percent-brace "%{ .. %}".



The codes:
0   restore default color
1   brighter
2   dimmer
4   underlined text
5   flashing text
7   reverse video

            black   red     green   yellow  blue    purple  cyan    white
foreground  30      31      32      33      34      35      36      37
background  40      41      42      43      44      45      46      47 

http://zipcon.net/~swhite/docs/computers/linux/shell_prompts.html

因此,为了通过Java做到这一点,你需要设置

So in order to do this through Java, you need to set

System.out.println(characterCode + character );

其中 String characterCode =\033 [31; 44; 1m; char character ='A';

A ,前景颜色设置为红色,背景为蓝色,字体以粗体显示。

and you would get an A with foreground color set to red, the background to blue, and the font in bold...

EDIT:Xubuntu中测试的结果

EDIT : Results of a test in Xubuntu

public static void main(String[] args) {
    char character = 'A';
    String characterCode;
    for (int foreground = 30; foreground < 38; foreground++) {
        for (int background = 40; background < 48; background++) {
            characterCode = "\033[" + foreground + ";" + background + ";1m";
            System.out.print(characterCode + character);
        }
        System.out.println();
    }

}

这篇关于在命令行java中突出显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆