Add a simple task bus with console status messages.

This commit is contained in:
Aria 2023-05-06 03:37:01 -07:00
parent c6d83e67d5
commit ec58268a25
5 changed files with 142 additions and 2 deletions

View file

@ -12,7 +12,7 @@ plugins {
id 'maven-publish'
}
version = "1.1.4"
version = "1.1.5"
group = "dev.zontreck"
archivesBaseName = "LibAC"
java.toolchain.languageVersion = JavaLanguageVersion.of(17)

View file

@ -0,0 +1,64 @@
package dev.zontreck.ariaslib.terminal;
import dev.zontreck.ariaslib.util.DelayedExecutorService;
import dev.zontreck.ariaslib.util.Progress;
import java.io.Console;
public abstract class Task implements Runnable
{
public final String TASK_NAME;
private TaskCompletionToken token = new TaskCompletionToken();
public static final String CHECK = "";
public static final String FAIL = "X";
// Else use the progress spinner from the Progress class
public Task(String name)
{
TASK_NAME=name;
}
public boolean isComplete(){
return token.get();
}
public void startTask()
{
DelayedExecutorService.getInstance().schedule(this, 1);
DelayedExecutorService.getInstance().schedule(new SpinnerTask(token), 1);
}
public void stopTask()
{
if(token.get())
{
ConsolePrompt.console.printf("\r"+TASK_NAME+"\t\t["+token.status+"]\n");
}
}
public class SpinnerTask implements Runnable
{
public final TaskCompletionToken token;
private final Progress spinner = new Progress(100);
public SpinnerTask (TaskCompletionToken token)
{
this.token=token;
}
@Override
public void run()
{
while(!token.get())
{
try {
Thread.sleep(1000L);
ConsolePrompt.console.printf("\r"+TASK_NAME+"\t\t"+spinner.getSpinnerTick());
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}

View file

@ -0,0 +1,38 @@
package dev.zontreck.ariaslib.terminal;
import dev.zontreck.ariaslib.util.DelayedExecutorService;
import java.util.List;
import java.util.Stack;
public class TaskBus implements Runnable
{
public static Stack<Task> tasks = new Stack<>();
public static Task current = null;
@Override
public void run()
{
try{
if(TaskBus.current == null)
{
current = tasks.pop();
current.startTask();
}else {
if(current.isComplete())
{
current.stopTask();
current=null;
}
}
// Don't care about a empty stack exception. We'll just queue this task check back up
}catch(Exception e){}
}
public static void register()
{
DelayedExecutorService.getInstance().scheduleRepeating(new TaskBus(), 1);
}
}

View file

@ -0,0 +1,20 @@
package dev.zontreck.ariaslib.terminal;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Should not be re-used for multiple tasks!!!
*/
public class TaskCompletionToken
{
private AtomicBoolean complete = new AtomicBoolean(false);
public String status = "";
public void completed(String reason){
status=reason;
complete.set(true);
}
public boolean get(){
return complete.get();
}
}

View file

@ -13,7 +13,7 @@ public class Progress
{
if(tickNum.get()>=TICKS.length()) tickNum.set(0);
return "[ " + TICKS.substring(tickNum.getAndIncrement(), tickNum.get()) + " ]";
return "[" + TICKS.substring(tickNum.getAndIncrement(), tickNum.get()) + "]";
}
public Progress(int maximum)
@ -35,4 +35,22 @@ public class Progress
{
return (current*100/max);
}
public void increment(){
current++;
sanity();
}
private void sanity(){
if(current > maximum) current = maximum;
if(current < 0)current = 0;
}
public void decrement(){
current--;
sanity();
}
public void setCurrent(int cur)
{
current=cur;
}
}