Home > Projects > VT 100 Print Stream
This class is for generating custom output for ANSI/VT100 terminals. Note, this is not portable and should only be used in environments that understand the ANSI/VT100 control codes. For example, it will not work under the Microsoft Windows Command Prompt. Furthermore, not all systems will support all capabilities such as colors. More information about the terminal control codes can be found at:
This is a fairly simple class. If you need to do complex CUI's look at CHARVA or JCurses.
Example of Use
This example draws a red box on the screen and puts some cyan text in the middle indicating the progress for printing a test message.
public class PrintMode
{
public static void main(String[] args)
{
VT100PrintStream out=new VT100PrintStream(System.out);
out.attributeSave();
out.setDefaultFont(VT100PrintStream.GRAPHICS);
out.setForeground(VT100PrintStream.RED);
out.setBackground(VT100PrintStream.BLACK);
// Get dimensions and clear the screen
int[] dim=out.getDimensions(System.in);
out.eraseScreen();
// Top of box
out.cursorTo(1, 1);
out.print("\u006C");
for(int i=2; i<dim[0]; i++)
out.print("\u0071");
out.print("\u006B");
// Sides of box
for(int i=2; i<dim[1]; i++)
{
out.cursorTo(i, 1);
out.print("\u0078");
out.cursorTo(i, dim[0]);
out.print("\u0078");
}
// Bottom of box
out.cursorTo(dim[1], 1);
out.print("\u006D");
for(int i=2; i<dim[0]; i++)
out.print("\u0071");
out.print("\u006A");
// Put text in the middle
out.setDefaultFont(VT100PrintStream.US_ASCII);
out.setForeground(VT100PrintStream.CYAN);
int textWidth=35;
int rowOffset=(dim[1]-2)/2;
int colOffset=(dim[0]-2-textWidth)/2;
out.cursorTo(rowOffset, colOffset);
out.print("Please wait, printing something...");
// Print something
out.startTransparentPrintMode();
out.println("This is a test page printed from a terminal.");
out.stopTransparentPrintMode();
// Tell, user they are done
out.cursorTo(rowOffset+1, colOffset);
out.print("Done printing, press any key to continue...");
// Pause
try
{
System.in.read();
}
catch(Exception e)
{}
out.eraseScreen();
out.attributeRestore();
}
}
For this example to work correctly the terminal needs to be put in raw mode so that input is not buffered until a newline is encountered. On UNIX systems this can be done using the stty command. For example, the following bash script could be used. This step only needs to be done if a function which takes an input stream as a parameter is used.
#!/bin/bash stty raw java -classpath . PrintMode stty sane
On Mac OS X using the Terminal application this example has the following output.
