diff --git a/src/main/java/com/osiris/betterthread/BetterThreadDisplayer.java b/src/main/java/com/osiris/betterthread/BetterThreadDisplayer.java index 99748cf..3862fc9 100644 --- a/src/main/java/com/osiris/betterthread/BetterThreadDisplayer.java +++ b/src/main/java/com/osiris/betterthread/BetterThreadDisplayer.java @@ -30,11 +30,13 @@ public class BetterThreadDisplayer extends Thread { private BetterThreadManager manager; private String label = "[MyAppName]"; private String threadType = "[PROCESS]"; - private DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); private LocalDateTime now; private boolean showWarnings; private boolean showDetailedWarnings; private int refreshInterval; + private DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); + private List allWarnings = new ArrayList<>(); + private boolean finished; private static byte anim; @@ -73,7 +75,6 @@ public BetterThreadDisplayer(BetterThreadManager manager, String label, String t // Check if Jansi console was already started AnsiConsole.systemInstall(); - this.start(); } @Override @@ -208,7 +209,18 @@ else if(process.isSkipped()){ } } if (!isPendingStart){ + + // Go through every process and add their warnings to the allBetterWarnings list + for (BetterThread process : + manager.getAll()) { + List betterWarnings = process.getWarnings(); + if (!betterWarnings.isEmpty()){ + allWarnings.addAll(betterWarnings); + } + } + formatWarnings(); + finished = true; return false; } } @@ -225,17 +237,6 @@ private long getPercentage(long now, long max){ * This is will be shown when all processes finished. */ private void formatWarnings(){ - List allBetterWarnings = new ArrayList(); - - // Go through every process and add their warnings to the allBetterWarnings list - for (BetterThread process : - manager.getAll()) { - List betterWarnings = process.getWarnings(); - if (!betterWarnings.isEmpty()){ - allBetterWarnings.addAll(betterWarnings); - } - } - Ansi ansiDate = ansi() .bg(WHITE) @@ -244,20 +245,20 @@ private void formatWarnings(){ .fg(BLACK).a("[SUMMARY]") .reset(); - if (allBetterWarnings.isEmpty()){ + if (allWarnings.isEmpty()){ System.out.print(ansi() .fg(GREEN).a(" Executed all tasks successfully!") .reset()); } else if (showWarnings) { System.out.print(ansi() - .fg(YELLOW).a(" There are " + allBetterWarnings.size() + " warnings:") + .fg(YELLOW).a(" There are " + allWarnings.size() + " warnings:") .reset()); if (showDetailedWarnings) { BetterWarning betterWarning; - for (int i = 0; i < allBetterWarnings.size(); i++) { - betterWarning = allBetterWarnings.get(i); + for (int i = 0; i < allWarnings.size(); i++) { + betterWarning = allWarnings.get(i); StringBuilder builder = new StringBuilder(); builder.append(ansiDate); builder.append(ansi() @@ -273,8 +274,8 @@ else if (showWarnings) { } else { BetterWarning betterWarning; - for (int i = 0; i < allBetterWarnings.size(); i++) { - betterWarning = allBetterWarnings.get(i); + for (int i = 0; i < allWarnings.size(); i++) { + betterWarning = allWarnings.get(i); StringBuilder builder = new StringBuilder(); builder.append(ansiDate); builder.append(ansi() @@ -288,9 +289,80 @@ else if (showWarnings) { } else{ System.out.print(ansi() - .fg(YELLOW).a(" There are "+ allBetterWarnings.size()+" warnings! Enable 'show-warnings' in the to view them, or check your debug log for further details!") + .fg(YELLOW).a(" There are "+ allWarnings.size()+" warnings! Enable 'show-warnings' in the to view them, or check your debug log for further details!") .reset()); } } + public BetterThreadManager getManager() { + return manager; + } + + public void setManager(BetterThreadManager manager) { + this.manager = manager; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public String getThreadType() { + return threadType; + } + + public void setThreadType(String threadType) { + this.threadType = threadType; + } + + public boolean isShowWarnings() { + return showWarnings; + } + + public void setShowWarnings(boolean showWarnings) { + this.showWarnings = showWarnings; + } + + public boolean isShowDetailedWarnings() { + return showDetailedWarnings; + } + + public void setShowDetailedWarnings(boolean showDetailedWarnings) { + this.showDetailedWarnings = showDetailedWarnings; + } + + public int getRefreshInterval() { + return refreshInterval; + } + + public void setRefreshInterval(int refreshInterval) { + this.refreshInterval = refreshInterval; + } + + public DateTimeFormatter getDateFormatter() { + return dateFormatter; + } + + public void setDateFormatter(DateTimeFormatter dateFormatter) { + this.dateFormatter = dateFormatter; + } + + public boolean isFinished() { + return finished; + } + + public void setFinished(boolean finished) { + this.finished = finished; + } + + public List getAllWarnings() { + return allWarnings; + } + + public void setAllWarnings(List allWarnings) { + this.allWarnings = allWarnings; + } } diff --git a/src/test/java/com/osiris/betterthread/BetterThreadDisplayerTest.java b/src/test/java/com/osiris/betterthread/BetterThreadDisplayerTest.java new file mode 100644 index 0000000..48fdbbb --- /dev/null +++ b/src/test/java/com/osiris/betterthread/BetterThreadDisplayerTest.java @@ -0,0 +1,52 @@ +package com.osiris.betterthread; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class BetterThreadDisplayerTest { + + @Test + void test() { + BetterThreadManager manager = new BetterThreadManager(); + BetterThread test1 = new BetterThread(manager); + test1.start(); + BetterThread test2 = new BetterThread(manager); + test2.start(); + BetterThread test3 = new BetterThread(manager); + test3.start(); + BetterThreadDisplayer displayer = new BetterThreadDisplayer(manager); + displayer.start(); + + // Its normal to get values over 100% because of this loop + Thread thread = new Thread(()->{ + try { + while (!displayer.isFinished()) + for (BetterThread t : + manager.getAll()) { + t.step(); + Thread.sleep(10); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + thread.start(); + + try{ + while (!displayer.isFinished()) + Thread.sleep(1000); + boolean isWarning = false; + for (BetterThread t : + manager.getAll()) { + if (!t.getBetterWarnings().isEmpty()) + isWarning = true; + } + assertTrue(!isWarning); + } catch (Exception e) { + e.printStackTrace(); + } + + + } +} \ No newline at end of file