Text bar loading when starting Java at command prompt?

I'm just wondering if there is a way to use System.out.println();other methods to create a cool loading bar when I run my program with a batch file.

The real question here is how can I make this bar as if it only prints on one line.

I do not want it to spread across multiple lines, as shown below:

[aaaaaccccccccccccccc] 25%
[aaaaaaaaaacccccccccc] 50%
[aaaaaaaaaaaaaaaccccc] 75%

He who stays clean in place will make things cleaner and friendlier.

Many thanks,

Justian

EDIT:

Ok I managed to find this link here: How to animate the command line? but the answer is:

  • This is for C ++, not Java
  • Seems not very efficient in Java, do I need to overlap every line in the loop?

Is there a better way to do this in Java?

EDIT:

Here is what I ended up with:

static final int PROGRESSBAR_LENGTH = 20;

public static void drawProgressBar(int numerator, int denominator) {
    int percent = (int) (((double) numerator / (double) denominator) * 100);

    String bar = "[";
    int lines = round((PROGRESSBAR_LENGTH * numerator) / denominator);
    int blanks = PROGRESSBAR_LENGTH - lines;

    for (int i = 0; i < lines; i++)
        bar += "|";

    for (int i = 0; i < blanks; i++)
        bar += " ";

    bar += "] " + percent + "%";

    System.out.print(bar + "\r");
}

private static int round(double dbl) {
    int noDecimal = (int) dbl;
    double decimal = dbl - noDecimal;

    if (decimal >= 0.5)
        return noDecimal + 1;
    else
        return noDecimal;
}

:

[||||||||||||||||....] 80% (. = )

+3
3

: 13 (), 0xD () \r ( ).

:

  • ( 80 , , )
  • , .
  • ( - ), , .

, - :

public static void loadMyProgram() {
    while(programStillLoading) {
        doSomeLoading();
        double loadFraction = howCloseToDone(); // A number between 0 and 1

        System.out.print("[");
        int i = 0;
        for( ; i < (int)(loadFraction * 20); i++)
            System.out.print("=");
        for( ; i < 20; i++)
            System.out.print(" ");
        System.out.print("] " + (int)(loadFraction * 100) + "%");

        System.out.print((char)13);
        //System.out.print((char)0xD); // Same as above, but in hex
        //System.out.print("\r"); // Same as above, but as the symbol
    }

    System.out.println();
}

[<bar>] <percent>%, <bar> - 0..p '=', 0..(20-p) ''. .

, ( , Windows, 20% 110% 1% 3 ) t , , . "%".

+4

backspace - .

System.out.print() System.out.println() - .

, , .

BTW - backspace java, :

System.out.print("\b");
0

This may be the place to start.

Using:

ProgressBar pb = new PorgressBar(10,1);
pb.start();
while (loadingStuff) {
  doStuff();
  pb.update();
}
pb.finish();

Grade:

public class ProgressBar {

      private static String[] s;
      private int pos, inc, size;

      public PorgressBar(int size, int increment) {
        this.size = size;
        this.increment = increment;
        s = new String[size+2];  
        Arrays.fill(s,"a");
        s[0] = "[";
        s[size+1] = "]";
      }

      public void update() {
          System.out.println('\r');
          if (pos+inc<size+2) {
              Arrays.fill(s,pos,pos+inc,"c");
              pos += inc;
          }
          for (String ch : s) {
              System.out.print(ch);
          }  
      }

      public void finish() {
          System.out.println();
      }
}
0
source

Source: https://habr.com/ru/post/1754166/


All Articles