package org.uct.cs.simplify.util;

import java.util.Arrays;

public class ProgressBar implements AutoCloseable
{
    private final String title;
    private final char progressChar;
    private final int maxTicks;
    private final long startTime;
    private int progressTicks;
    private int position;


    public ProgressBar(String title, int maxTicks)
    {
        this(title, maxTicks, '|');
    }

    public ProgressBar(String title, int maxTicks, char progressChar)
    {
        this.title = title;
        this.progressChar = progressChar;
        this.maxTicks = maxTicks;
        this.position = 0;
        this.progressTicks = 0;
        this.startTime = System.nanoTime();
        this.printTitle();
    }

    private void printTitle()
    {
        char[] pad = new char[100 - this.title.length() - 7];
        Arrays.fill(pad, '=');
        System.out.printf("===  %s  %s%n", this.title, new String(pad));
    }

    public void tick()
    {
        this.progressTicks += 1;

        int newPosition = ((100 * this.progressTicks) / this.maxTicks);
        int toPrint = newPosition - this.position;
        if (toPrint > 0)
        {
            char[] chars = new char[toPrint];
            Arrays.fill(chars, this.progressChar);

            System.out.print(chars);
        }
        this.position = newPosition;
    }

    @Override
    public void close()
    {
        int toPrint = 100 - this.position;
        if (toPrint > 0)
        {
            char[] chars = new char[toPrint];
            Arrays.fill(chars, this.progressChar);

            System.out.print(chars);
        }
        String timestr = Useful.formatTime(System.nanoTime() - this.startTime);
        timestr = String.format(" ( %s ) ", timestr);
        char[] pad = new char[100 - timestr.length() - 3];
        Arrays.fill(pad, '=');
        System.out.printf("%n%s%s===%n", new String(pad), timestr);
    }
}

@@ -193,19 +193,20 @@
 e final 
-int
+long
  maxTick
@@ -330,35 +330,36 @@
 r(String title, 
-int
+long
  maxTicks)%0A    %7B
@@ -439,19 +439,20 @@
  title, 
-int
+long
  maxTick
@@ -1018,16 +1018,23 @@
 ition = 
+(int) (
 ((100 * 
@@ -1069,16 +1069,17 @@
 axTicks)
+)
 ;%0A      

Allow ProgressBar to take 64bit tickcount