Tuesday, 21 August 2012

Java Linux Console print colours






Here's a Java snippet that prints color output to the console (in a VT100 emulating terminal, i.e., in linux only):



public class ColorTest {

   public static void main (String args[]) {
      for (int i = 30; i < 38; i++) {
System.out.println("\033[" + i + "mHello " + "\033[1;" + i + "mWorld!\033[m");
      }

   }
}




It works by "printing" special terminal control commands that change the color. There are other VT100 terminal control commands that let you move the cursor around, clear the screen, and other things.

Who needs Swing, when you can make old-school ASCII interfaces, right? No fancy libraries required either.

No comments:

Post a Comment