Skip to content

Instantly share code, notes, and snippets.

@nodegin
Last active December 20, 2015 09:09
Show Gist options
  • Select an option

  • Save nodegin/0f0b519c4777a34fe8e8 to your computer and use it in GitHub Desktop.

Select an option

Save nodegin/0f0b519c4777a34fe8e8 to your computer and use it in GitHub Desktop.
progress bar
// cursor moving
// http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
static void updateProgress(int currentIndex, int total) {
final int width = 25; // progress bar width in chars
float percentage = (float) currentIndex / (float) total;
int currProgress = (int) Math.floor(percentage * width);
int remainProgress = width - currProgress;
String numCurr = new String(new char[currProgress]).replace("\0", "=");
if (numCurr.length() > 0)
numCurr = numCurr.substring(0, numCurr.length() - 1) + ">";
String numRemain = new String(new char[remainProgress]).replace("\0", " ");
System.out.print("\33[2A\r[" + numCurr + numRemain + "] " + currentIndex + " of " + total + " (" + ((int) Math.floor(percentage * 100)) + "%)\n\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment