Add latest revisions after getting it functional

This commit is contained in:
zontreck 2024-10-31 17:18:14 -07:00
parent 9653e273e4
commit ffcecb8e8e
1443 changed files with 258988 additions and 6046 deletions

View file

@ -0,0 +1,47 @@
package com.zontreck.ariaslib.terminal;
import java.util.ArrayList;
import java.util.List;
public class Banners
{
public static String generateBanner(String text) {
int maxLength = calculateMaxLength(text);
List<String> bannerLines = new ArrayList<>();
StringBuilder border = new StringBuilder();
for (int i = 0; i < maxLength + 4; i++) {
border.append("*");
}
bannerLines.add(border.toString());
bannerLines.add("* " + centerText(text, maxLength) + " *");
bannerLines.add(border.toString());
return String.join("\n", bannerLines);
}
private static String centerText(String text, int maxLength) {
StringBuilder centeredText = new StringBuilder();
int spacesToAdd = (maxLength - text.length()) / 2;
for (int i = 0; i < spacesToAdd; i++) {
centeredText.append(" ");
}
centeredText.append(text);
for (int i = 0; i < spacesToAdd; i++) {
centeredText.append(" ");
}
if (centeredText.length() < maxLength) {
centeredText.append(" ");
}
return centeredText.toString();
}
private static int calculateMaxLength(String text) {
int maxLength = 0;
for (String line : text.split("\n")) {
if (line.length() > maxLength) {
maxLength = line.length();
}
}
return maxLength;
}
}

View file

@ -0,0 +1,86 @@
package com.zontreck.ariaslib.terminal;
import com.zontreck.ariaslib.util.Progress;
import java.util.TimerTask;
public abstract class Task extends TimerTask implements Runnable {
public final String TASK_NAME;
private TaskCompletionToken token = new TaskCompletionToken ( );
public static final String CHECK = "P";
public static final String FAIL = "F";
// Else use the progress spinner from the Progress class
private boolean isSilent = false;
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 silent Whether to print to the terminal
*/
public Task ( String name , boolean silent ) {
this ( name );
isSilent = silent;
}
public boolean isComplete ( ) {
return token.get ( );
}
public void startTask ( ) {
Thread tx = new Thread(this);
tx.start();
if(! isSilent)
{
Thread tx2 = new Thread(new SpinnerTask(token, this));
tx2.start();
}
}
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 setFail ( ) {
token.completed ( FAIL );
}
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;
}
@Override
public void run ( ) {
while ( ! task.isComplete ( ) ) {
try {
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 ( );
}
}
}
}
}

View file

@ -0,0 +1,20 @@
package com.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();
}
}