2015-01-11 47 views
0

我有一個任務來重新創建unix cal程序,除了一部分外,它非常簡單。在當天,它突出了數字。我不知道如何做到這一點。任何想法如何在Java中做到這一點?突出顯示命令行中的文本java

圖片:

Calender

+0

不依賴在你使用的終端上? http://unix.stackexchange.com/questions/84078/highlighting-text-in-shell – Ubica

回答

2

ANSI顏色碼

提示的顏色是通過擴大轉義序列 「\ E [SM」,其中s是分號設置ANSI顏色代碼的分隔列表: 「\ e [31; 44; 1m」會將前景色設置爲紅色,背景色爲 藍色,字體爲粗體; (在「\ E」是ASCII轉義字符 不要忘記用「米」字 終止序列。)

變量需要由 指標來襯托環境的二進制序列,他們具有零寬度,否則shell不會 正確計算提示的寬度。 Bash使用斜槓括號「[..]」包含 ,而Tcsh使用百分比大括號「%{.. %}」。

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來做到這一點,你需要設置

System.out.println(characterCode + character);

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

,你會得到一個與f。A oreground顏色設置爲紅色,背景爲藍色,並以粗體字體...


編輯:測試的結果的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

+0

是的,我無法得到這個工作。雖然好源。我的編譯器告訴我它不能逃避\ e。 – Danman

+0

我沒有真正測試它,所以我確定它需要一些修補程序...我其實很好奇:)我將切換到Linux並檢查它是如何工作的... – Ubica

+0

好吧,那麼我試了一下,它使用'String characterCode =「\ 033 [31; 44; 1m」;'。我在Ubuntu終端中試過:) – Ubica