1
votes

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?

Image:

Calender

1
doesn't that depend on the terminal you're using? unix.stackexchange.com/questions/84078/… - Ubica

1 Answers

3
votes

ANSI color codes

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.)

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 

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

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

System.out.println(characterCode + character);

where String characterCode = "\033[31;44;1m"; and char character = 'A';

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


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();
    }

}

code result