Adds a check for docker environment.

This commit is contained in:
Aria 2023-06-22 15:21:36 -07:00
parent 8a4d4109fa
commit fca7091590
2 changed files with 66 additions and 73 deletions

View file

@ -1,89 +1,81 @@
package dev.zontreck.ariaslib.terminal;
import dev.zontreck.ariaslib.util.DelayedExecutorService;
import dev.zontreck.ariaslib.util.EnvironmentUtils;
import dev.zontreck.ariaslib.util.Progress;
import java.io.Console;
import java.util.TimerTask;
public abstract class Task extends TimerTask implements Runnable
{
public abstract class Task extends TimerTask implements Runnable {
public final String TASK_NAME;
private TaskCompletionToken token = new TaskCompletionToken();
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
private boolean isSilent=false;
private boolean isSilent = false;
public Task(String name)
{
TASK_NAME=name;
public Task ( String name ) {
TASK_NAME = name;
}
/**
* This constructor is meant to be used to create silent tasks that do not output to the console. (Example usage: DelayedExecutionService)
* @param name Task name
*
* @param name Task name
* @param silent Whether to print to the terminal
*/
public Task(String name, boolean silent)
{
this(name);
isSilent=silent;
public Task ( String name , boolean silent ) {
this ( name );
isSilent = silent;
}
public boolean isComplete(){
return token.get();
public boolean isComplete ( ) {
return token.get ( );
}
public void startTask()
{
DelayedExecutorService.scheduleTask(this, 1);
if(!isSilent)
DelayedExecutorService.instantExec(new SpinnerTask(token,this));
public void startTask ( ) {
DelayedExecutorService.scheduleTask ( this , 1 );
if ( ! isSilent && ! EnvironmentUtils.isRunningInsideDocker ( ) )
DelayedExecutorService.instantExec ( new SpinnerTask ( token , this ) );
}
public void stopTask()
{
if(token.get() && !isSilent)
{
System.out.printf("\r"+TASK_NAME+"\t\t["+token.status+"]\n");
public void stopTask ( ) {
if ( token.get ( ) && ! isSilent ) {
System.out.printf ( "\r" + TASK_NAME + "\t\t[" + token.status + "]\n" );
}
}
public void setSuccess()
{
token.completed(CHECK);
public void setSuccess ( ) {
token.completed ( CHECK );
}
public void setFail()
{
token.completed(FAIL);
public void setFail ( ) {
token.completed ( FAIL );
}
public class SpinnerTask extends Task
{
public class SpinnerTask extends Task {
public final Task task;
public final TaskCompletionToken token;
private final Progress spinner = new Progress(100);
public SpinnerTask (TaskCompletionToken token, Task parent)
{
super("spinner",true);
this.token=token;
this.task=parent;
private final Progress spinner = new Progress ( 100 );
public SpinnerTask ( TaskCompletionToken token , Task parent ) {
super ( "spinner" , true );
this.token = token;
this.task = parent;
}
@Override
public void run()
{
while(!task.isComplete())
{
public void run ( ) {
while ( ! task.isComplete ( ) ) {
try {
Thread.sleep(50L);
Thread.sleep ( 50L );
if(!task.isSilent && !task.isComplete())
System.out.printf("\r"+task.TASK_NAME+"\t\t"+spinner.getSpinnerTick()+"\r");
}catch(Exception e)
{
e.printStackTrace();
if ( ! task.isSilent && ! task.isComplete ( ) && ! EnvironmentUtils.isRunningInsideDocker ( ) )
System.out.printf ( "\r" + task.TASK_NAME + "\t\t" + spinner.getSpinnerTick ( ) + "\r" );
} catch ( Exception e ) {
e.printStackTrace ( );
}
}
}

View file

@ -1,35 +1,36 @@
package dev.zontreck.ariaslib.terminal;
import java.io.Console;
import dev.zontreck.ariaslib.util.DelayedExecutorService;
import dev.zontreck.ariaslib.util.EnvironmentUtils;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import dev.zontreck.ariaslib.util.DelayedExecutorService;
public class Terminal {
private static final AtomicInteger ID = new AtomicInteger(0);
private static final AtomicBoolean running= new AtomicBoolean(true);
public static String PREFIX = "";
/**
* This starts a terminal instance
* @return The terminal ID
*/
public static int startTerminal()
{
running.set(true);
DelayedExecutorService.getInstance().schedule(new ConsolePrompt(), 1);
private static final AtomicInteger ID = new AtomicInteger ( 0 );
private static final AtomicBoolean running = new AtomicBoolean ( true );
public static String PREFIX = "";
/**
* This starts a terminal instance
*
* @return The terminal ID
*/
public static int startTerminal ( ) {
if ( EnvironmentUtils.isRunningInsideDocker ( ) )
return 0;
running.set ( true );
DelayedExecutorService.getInstance ( ).schedule ( new ConsolePrompt ( ) , 1 );
return ID.getAndIncrement();
}
return ID.getAndIncrement ( );
}
public static boolean isRunning()
{
return running.get();
}
public static boolean isRunning ( ) {
return running.get ( );
}
public static void setRunning(boolean running)
{
Terminal.running.set(running);
}
public static void setRunning ( boolean running ) {
Terminal.running.set ( running );
}
}