blob: 50397f311a955dc9c9dbabc61095176e937a94e6 (
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
|
package com.it_jaros.networkScanner;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ProgressBar {
private ScheduledExecutorService ui = Executors.newSingleThreadScheduledExecutor();
public void start(List<Progress> hosts) {
final int lines = hosts.size();
System.out.print("\n\n");
ui.scheduleAtFixedRate(() -> {
// move cursor UP to where bars start
System.out.print("\u001b[" + lines + "A");
for (Progress progress : hosts) {
System.out.print("\u001b[2K\r");
System.out.println(bar(
progress.host() + " open=" + progress.open().get(),
progress.done().get(),
progress.total()));
}
}, 1, 200, TimeUnit.MILLISECONDS);
}
public void stop() {
ui.shutdown();
}
public String bar(String label, int done, int total) {
int width = 30;
double pct = total == 0 ? 1.0 : (done / (double) total);
int filled = (int) (pct * width);
String b = "[" + "#".repeat(filled) + "-".repeat(width - filled) + "]";
int percent = (int) (pct * 100);
return String.format("%-25s %s %3d%% (%d/%d)", label, b, percent, done, total);
}
void onPortFinished(Progress p) {
p.done().incrementAndGet();
}
}
|