Animating the console!
There are times when I love to use command-line/terminal (whatever you want to call it). And when I do I like to do something fun with it, and not just putting text to read. This post contains two examples of some fun code that you might wanna use to make something nice of your command-line application.
The first example is a rolling piece of text on console (something similar to what you might get from the HTML marquee.
[](/uploads/2012/11/MarqueeOnConsole1.gif)/**
* MarqueeOnConsole.java
* Following piece of code available to use without any fee for any closed/open-source project.
* Creates a HTML-marquee-like text on console.
*
* @author Ankit Gupta
* @version 1.0
*/
public class MarqueeOnConsole{
/** Width of the marquee in terms of character. **/
public static final int CONSOLE_LENGTH=40;
/** Creates a rolling marquee on console. **/
public static void main(String []agrs) throws InterruptedException{
String text = "This text is rolling!";
int prefixLength=-1;
while(true){
String prefix = "\r";
prefixLength++;
if(prefixLength>CONSOLE_LENGTH){
prefixLength=0;
}
for(int i=0; i < prefixLength;i++){
prefix += " ";
}
int endIndex = CONSOLE_LENGTH-prefixLength;
if(endIndex > text.length()){
endIndex = text.length();
}
String printableText = text.substring(0,endIndex);
System.out.print(prefix + printableText);
Thread.sleep(100);
}
}
}
This second example is the one that simulates the process completion that you see on console with increasing percent.
The key in both the example here is the use of “\r” (carriage return character). What it does is that it moves the cursor to the front of the current-line without moving it down (like “\n”).
Although, this is not something awesome but still something that might help you make your command-line tool or application look pretty.
If you have done some nice effect in your text-based console app, I would like to have a look at it. Send me an email or leave a reply here.