diff options
| author | 2026-02-16 17:56:44 +0100 | |
|---|---|---|
| committer | 2026-07-09 09:59:28 +0200 | |
| commit | e97451bb7d3b16edc5b2b425612603d8a94d9876 (patch) | |
| tree | a01af3aa7c330a6947d0c1a850c7af6eaf8ba6c8 /src/main | |
| parent | 6e3838bd46dd609a7066b786bf1611e6e64a1360 (diff) | |
Fixed missing 100% on progressbar
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/java/com/it_jaros/networkScanner/ProgressBar.java | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/src/main/java/com/it_jaros/networkScanner/ProgressBar.java b/src/main/java/com/it_jaros/networkScanner/ProgressBar.java index 7203a46..0f50267 100644 --- a/src/main/java/com/it_jaros/networkScanner/ProgressBar.java +++ b/src/main/java/com/it_jaros/networkScanner/ProgressBar.java @@ -8,30 +8,37 @@ import java.util.concurrent.TimeUnit; public class ProgressBar { private ScheduledExecutorService ui = Executors.newSingleThreadScheduledExecutor(); + private List<Progress> hosts; public void start(List<Progress> hosts) { - final int lines = hosts.size(); + this.hosts = hosts; + // move cursor down depending on how many hosts have to be scanned hosts.forEach((h) -> System.out.print("\n")); ui.scheduleAtFixedRate(() -> { - // clear bars by moving cursor up - System.out.print("\u001b[" + lines + "A"); - - // paint a bar per host - for (Progress progress : hosts) { - drawBar(progress); - } + drawProgress(); }, 1, 200, TimeUnit.MILLISECONDS); } + private void drawProgress() { + // clear bars by moving cursor up + final int lines = hosts.size(); + System.out.print("\u001b[" + lines + "A"); + + // paint a bar per host + for (Progress progress : hosts) { + drawBar(progress); + } + } + private void drawBar(Progress progress) { System.out.print("\u001b[2K\r"); - System.out.println(bar( + System.out.println(createBar( progress.host() + " open=" + progress.open().get(), progress.done().get(), progress.total())); } - public String bar(String label, int done, int total) { + public String createBar(String label, int done, int total) { int width = 30; double pct = total == 0 ? 1.0 : (done / (double) total); int filled = (int) (pct * width); @@ -44,6 +51,12 @@ public class ProgressBar { public void stop() { ui.shutdown(); + try { + ui.awaitTermination(10, TimeUnit.SECONDS); + // we need to draw one last time for the 100% to appear + drawProgress(); + } catch (InterruptedException ignored) { + } } void onPortFinished(Progress p) { |
