Skip to content

Commit 3061b72

Browse files
committed
Tidy up console colouring.
1 parent 3da1c90 commit 3061b72

File tree

8 files changed

+72
-77
lines changed

8 files changed

+72
-77
lines changed

src/main/java/org/byteskript/skript/app/ByteSkriptApp.java

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import java.util.ArrayList;
1616
import java.util.List;
1717

18-
import static org.byteskript.skript.app.ByteSkriptApp.Colour.*;
18+
import static org.byteskript.skript.runtime.internal.ConsoleColour.*;
1919

2020
public class ByteSkriptApp extends SkriptApp {
2121
protected static final Skript SKRIPT = new Skript();
@@ -74,32 +74,4 @@ static void makeFiles() {
7474
if (!RESOURCES.exists()) RESOURCES.mkdirs();
7575
}
7676

77-
enum Colour {
78-
RESET("\033[0m"),
79-
BLACK("\033[0;30m"),
80-
RED("\033[0;31m"),
81-
GREEN("\033[0;32m"),
82-
YELLOW("\033[0;33m"),
83-
BLUE("\033[0;34m"),
84-
PURPLE("\033[0;35m"),
85-
CYAN("\033[0;36m"),
86-
CYAN_UNDERLINED("\033[4;36m"),
87-
BRIGHT_PURPLE("\033[0;95m");
88-
89-
private final String code;
90-
91-
Colour(String code) {
92-
this.code = code;
93-
}
94-
95-
@Override
96-
public String toString() {
97-
if (System.console() == null || System.getenv().get("TERM") != null) {
98-
return code;
99-
} else {
100-
return "";
101-
}
102-
}
103-
}
104-
10577
}

src/main/java/org/byteskript/skript/app/SimpleThrottleController.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@
1111
import org.byteskript.skript.runtime.threading.AirlockQueue;
1212
import org.byteskript.skript.runtime.threading.OperationController;
1313

14-
public final class SimpleThrottleController extends Thread implements Runnable {
15-
private final Skript skript;
16-
17-
public SimpleThrottleController(Skript skript) {
18-
this.skript = skript;
19-
}
14+
public record SimpleThrottleController(Skript skript) implements Runnable {
2015

2116
@Override
2217
public void run() {
@@ -39,16 +34,12 @@ public void run() {
3934
}
4035
}
4136
try {
42-
Thread.sleep(50);
37+
Thread.sleep(10);
4338
} catch (InterruptedException e) {
4439
e.printStackTrace();
4540
break;
4641
}
4742
}
4843
}
4944

50-
public Skript skript() {
51-
return skript;
52-
}
53-
5445
}

src/main/java/org/byteskript/skript/app/SkriptApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected static void callLibrary(final File file, final String main, final Skri
6868
target.getMethod("load", Skript.class).invoke(null, skript);
6969
} catch (Throwable ex) {
7070
throw new ScriptLibraryError("Library '" + file.getName() + "' main class is missing load method:\n" +
71-
"static void load(Skript skript)");
71+
"public static void load(Skript skript)\n");
7272
}
7373
}
7474

src/main/java/org/byteskript/skript/error/ScriptError.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
1111

1212
public interface ScriptError {
1313

14-
String BLACK_BACKGROUND = "\033[40m";
15-
String ANSI_RESET = "\u001B[0m";
16-
String ANSI_YELLOW = "\u001B[33m";
17-
String ANSI_RED = "\u001B[31m";
18-
String ANSI_PURPLE = "\u001B[35m";
19-
String ANSI_CYAN = "\u001B[36m";
20-
String ANSI_WHITE = "\u001B[37m";
21-
2214

2315
interface Output {
2416
void println(String string);

src/main/java/org/byteskript/skript/error/ScriptParseError.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.io.PrintWriter;
1414
import java.util.Map;
1515

16+
import static org.byteskript.skript.runtime.internal.ConsoleColour.*;
17+
1618
public class ScriptParseError extends Error implements ScriptError {
1719

1820
private final int line;
@@ -77,22 +79,22 @@ public synchronized Throwable fillInStackTrace() {
7779
}
7880

7981
public void printStackTrace(OutputWriter stream) {
80-
stream.println(ANSI_RESET + "A parsing error has occurred in:");
81-
stream.println("\tline " + ANSI_CYAN + line + ANSI_RESET + " of '" + BLACK_BACKGROUND + ANSI_YELLOW + details.file + ANSI_RESET + "'");
82+
stream.println(RESET + "A parsing error has occurred in:");
83+
stream.println("\tline " + CYAN + line + RESET + " of '" + BLACK_BACKGROUND + YELLOW + details.file + RESET + "'");
8284
if (details.expression != null) {
83-
stream.println(ANSI_RESET + "No match was found for expression:\n\t'" + BLACK_BACKGROUND + ANSI_YELLOW + details.expression + ANSI_RESET + "'");
84-
stream.println(ANSI_RESET + "In the line/effect:\n\t'" + BLACK_BACKGROUND + ANSI_YELLOW + details.line + ANSI_RESET + "'");
85-
stream.println(ANSI_RESET + "This effect was matched to:\n\t'" + BLACK_BACKGROUND + ANSI_YELLOW + details.lineMatched.name() + ANSI_RESET + "'");
85+
stream.println(RESET + "No match was found for expression:\n\t'" + BLACK_BACKGROUND + YELLOW + details.expression + RESET + "'");
86+
stream.println(RESET + "In the line/effect:\n\t'" + BLACK_BACKGROUND + YELLOW + details.line + RESET + "'");
87+
stream.println(RESET + "This effect was matched to:\n\t'" + BLACK_BACKGROUND + YELLOW + details.lineMatched.name() + RESET + "'");
8688
} else if (details.line != null) {
87-
stream.println(ANSI_RESET + "No match was found for line:\n\t'" + BLACK_BACKGROUND + ANSI_YELLOW + details.line + ANSI_RESET + "'");
89+
stream.println(RESET + "No match was found for line:\n\t'" + BLACK_BACKGROUND + YELLOW + details.line + RESET + "'");
8890
}
8991
if (details.hints.isEmpty()) return;
90-
stream.println(ANSI_RESET + "Below are some hints from partial matches.");
92+
stream.println(RESET + "Below are some hints from partial matches.");
9193
for (Map.Entry<SyntaxElement, String> entry : details.hints.entrySet()) {
9294
final SyntaxElement element = entry.getKey();
9395
final String hint = entry.getValue();
94-
stream.println(ANSI_RESET + "If you were trying to use '" + BLACK_BACKGROUND + ANSI_CYAN + element.name() + ANSI_RESET + "':");
95-
stream.println(ANSI_RESET + "\t" + hint);
96+
stream.println(RESET + "If you were trying to use '" + BLACK_BACKGROUND + CYAN + element.name() + RESET + "':");
97+
stream.println(RESET + "\t" + hint);
9698
}
9799
}
98100

src/main/java/org/byteskript/skript/lang/syntax/type/property/PropertyMember.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.byteskript.skript.error.ScriptError;
1515
import org.byteskript.skript.lang.element.StandardElements;
1616
import org.byteskript.skript.runtime.data.SourceData;
17+
import org.byteskript.skript.runtime.internal.ConsoleColour;
1718

1819
import java.time.Instant;
1920
import java.util.regex.Matcher;
@@ -56,7 +57,7 @@ public Pattern.Match match(String thing, Context context) {
5657
if (matcher.find() && matcher.group("name") != null)
5758
return new Pattern.Match(matcher);
5859
context.getError()
59-
.addHint(this, "Property names must be " + ScriptError.ANSI_CYAN + "[a-z0-9_]" + ScriptError.ANSI_RESET + " and start with a letter.");
60+
.addHint(this, "Property names must be " + ConsoleColour.CYAN + "[a-z0-9_]" + ConsoleColour.RESET + " and start with a letter.");
6061
return null;
6162
}
6263

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2022 ByteSkript org (Moderocky)
3+
* View the full licence information and permissions:
4+
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE
5+
*/
6+
7+
package org.byteskript.skript.runtime.internal;
8+
9+
public enum ConsoleColour {
10+
RESET("\033[0m"),
11+
BLACK("\033[0;30m"),
12+
RED("\033[0;31m"),
13+
GREEN("\033[0;32m"),
14+
YELLOW("\033[0;33m"),
15+
BLUE("\033[0;34m"),
16+
PURPLE("\033[0;35m"),
17+
CYAN("\033[0;36m"),
18+
CYAN_UNDERLINED("\033[4;36m"),
19+
BRIGHT_PURPLE("\033[0;95m"),
20+
BLACK_BACKGROUND("\033[40m");
21+
22+
private final String code;
23+
24+
ConsoleColour(String code) {
25+
this.code = code;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
if (System.console() == null || System.getenv().get("TERM") != null) {
31+
return code;
32+
} else {
33+
return "";
34+
}
35+
}
36+
}

src/main/java/org/byteskript/skript/runtime/threading/ScriptExceptionHandler.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
package org.byteskript.skript.runtime.threading;
88

9-
import org.byteskript.skript.error.ScriptError;
109
import org.byteskript.skript.error.ScriptParseError;
1110

11+
import static org.byteskript.skript.runtime.internal.ConsoleColour.*;
12+
1213
/**
1314
* This produces nice, detailed error messages.
1415
*/
15-
public class ScriptExceptionHandler implements Thread.UncaughtExceptionHandler, ScriptError {
16+
public class ScriptExceptionHandler implements Thread.UncaughtExceptionHandler {
1617

1718
@Override
1819
public void uncaughtException(Thread source, Throwable throwable) {
@@ -26,17 +27,17 @@ public void uncaughtException(Thread source, Throwable throwable) {
2627
System.err.println("An error occurred while running a script.");
2728
System.err.println("\t" + throwable.getMessage());
2829
if (start != null)
29-
System.err.println("This program started in: " + BLACK_BACKGROUND + ANSI_YELLOW + start.getName()
30-
.replace('.', '/') + ".bsk" + ANSI_RESET);
30+
System.err.println("This program started in: " + BLACK_BACKGROUND + YELLOW + start.getName()
31+
.replace('.', '/') + ".bsk" + RESET);
3132
final StackTraceElement[] elements = throwable.getStackTrace();
3233
if (elements == null || elements.length < 1) return;
3334
System.err.println("The error came from:");
3435
if (elements[0].getClassName().startsWith("skript.")) {
35-
System.err.println("\t'" + ANSI_RED + elements[0].getClassName() + ANSI_RESET + "' line " + ANSI_CYAN + elements[0].getLineNumber() + ANSI_RESET);
36-
System.err.println("\t(This is from Skript code.)" + ANSI_RESET);
36+
System.err.println("\t'" + RED + elements[0].getClassName() + RESET + "' line " + CYAN + elements[0].getLineNumber() + RESET);
37+
System.err.println("\t(This is from Skript code.)" + RESET);
3738
} else {
38-
System.err.println("\t'" + ANSI_RED + elements[0].getClassName() + ANSI_RESET + "' line " + ANSI_CYAN + elements[0].getLineNumber() + ANSI_RESET);
39-
System.err.println("\t(This is from a Java library.)" + ANSI_RESET);
39+
System.err.println("\t'" + RED + elements[0].getClassName() + RESET + "' line " + CYAN + elements[0].getLineNumber() + RESET);
40+
System.err.println("\t(This is from a Java library.)" + RESET);
4041
}
4142
System.err.println("Below is the list of trigger calls that caused this error.");
4243
System.err.println("The top line was the most recent call.");
@@ -49,33 +50,33 @@ public void uncaughtException(Thread source, Throwable throwable) {
4950
if (method.startsWith("verify$")) {
5051
error
5152
.append("\tverifier ")
52-
.append(ANSI_PURPLE)
53+
.append(PURPLE)
5354
.append(method.substring(7))
54-
.append(ANSI_RESET);
55+
.append(RESET);
5556
} else if (method.startsWith("event$")) {
5657
error
5758
.append("\tevent ")
58-
.append(ANSI_PURPLE)
59+
.append(PURPLE)
5960
.append(method.substring(6))
60-
.append(ANSI_RESET);
61+
.append(RESET);
6162
} else {
6263
error
6364
.append("\tfunction ")
64-
.append(ANSI_PURPLE)
65+
.append(PURPLE)
6566
.append(method)
66-
.append(ANSI_RESET)
67+
.append(RESET)
6768
.append("(...)");
6869
}
6970
error
7071
.append(" in '")
7172
.append(BLACK_BACKGROUND)
72-
.append(ANSI_YELLOW)
73+
.append(YELLOW)
7374
.append(this.getScriptName(element))
74-
.append(ANSI_RESET)
75+
.append(RESET)
7576
.append("' at line ")
76-
.append(ANSI_CYAN)
77+
.append(CYAN)
7778
.append(element.getLineNumber())
78-
.append(ANSI_RESET);
79+
.append(RESET);
7980
System.err.println(error);
8081
}
8182
} else {

0 commit comments

Comments
 (0)