Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Osiris-Team committed Feb 3, 2021
1 parent 3a90158 commit 0fbf1e0
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 20 deletions.
112 changes: 92 additions & 20 deletions src/main/java/com/osiris/betterthread/BetterThreadDisplayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BetterWarning> allWarnings = new ArrayList<>();
private boolean finished;

private static byte anim;

Expand Down Expand Up @@ -73,7 +75,6 @@ public BetterThreadDisplayer(BetterThreadManager manager, String label, String t

// Check if Jansi console was already started
AnsiConsole.systemInstall();
this.start();
}

@Override
Expand Down Expand Up @@ -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<BetterWarning> betterWarnings = process.getWarnings();
if (!betterWarnings.isEmpty()){
allWarnings.addAll(betterWarnings);
}
}

formatWarnings();
finished = true;
return false;
}
}
Expand All @@ -225,17 +237,6 @@ private long getPercentage(long now, long max){
* This is will be shown when all processes finished.
*/
private void formatWarnings(){
List<BetterWarning> allBetterWarnings = new ArrayList();

// Go through every process and add their warnings to the allBetterWarnings list
for (BetterThread process :
manager.getAll()) {
List<BetterWarning> betterWarnings = process.getWarnings();
if (!betterWarnings.isEmpty()){
allBetterWarnings.addAll(betterWarnings);
}
}


Ansi ansiDate = ansi()
.bg(WHITE)
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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<BetterWarning> getAllWarnings() {
return allWarnings;
}

public void setAllWarnings(List<BetterWarning> allWarnings) {
this.allWarnings = allWarnings;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}


}
}

0 comments on commit 0fbf1e0

Please sign in to comment.