aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/uk/ac/ox/cs/pagoda/util/SimpleProgressBar.java
blob: 3c4aad73ccfef74b26e4e640afb4e07494b23e12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package uk.ac.ox.cs.pagoda.util;

import uk.ac.ox.cs.pagoda.util.disposable.Disposable;

public class SimpleProgressBar extends Disposable {

    private final String name;
    private int lastPercent;
    private int maxValue;

    public SimpleProgressBar() {
        this("");
    }

    public SimpleProgressBar(String name) {
        this(name, 100);
    }

    public SimpleProgressBar(String name, int maxValue) {
        this.name = name;
        this.maxValue = maxValue;
    }

    public void update(int value) {
        int percent = value * 100 / maxValue;
        StringBuilder template = new StringBuilder("\r" + name + " [");
        for (int i = 0; i < 50; i++) {
            if (i < percent * .5) {
                template.append("=");
            } else if (i == percent * .5) {
                template.append(">");
            } else {
                template.append(" ");
            }
        }
        template.append("] %s   ");
        System.out.printf(template.toString(), percent + "%");
        System.out.flush();
        lastPercent = percent;
    }

    @Override
    public void dispose() {
        super.dispose();

        System.out.println();
    }
}