Skip to content

Commit

Permalink
Major improvements to the terminal experience
Browse files Browse the repository at this point in the history
Terminal users rejoice! By updating to Jline 3 we've improved the user experience and gotten rid of the conflicts that were causing bugs and inconsistencies with terminal logging.
  • Loading branch information
ME1312 committed Sep 19, 2019
1 parent d60015b commit 0ac8953
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 100 deletions.
10 changes: 10 additions & 0 deletions GalaxiAPI/global/src/net/ME1312/Galaxi/Library/Map/ObjectMap.java
Expand Up @@ -55,6 +55,16 @@ public ObjectMap(Map<? extends K, ?> map) {
return map;
}

/**
* Change the Key type of this map
*
* @param <T> Key Type
* @return Object Map
*/
public <T> ObjectMap<T> key() {
return (ObjectMap<T>) this;
}

/**
* Get the Keys
*
Expand Down
7 changes: 7 additions & 0 deletions GalaxiAPI/pom.xml
Expand Up @@ -10,6 +10,13 @@
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>1.18</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
Expand Down
51 changes: 45 additions & 6 deletions GalaxiAPI/src/net/ME1312/Galaxi/Library/Log/LogLevel.java
@@ -1,13 +1,52 @@
package net.ME1312.Galaxi.Library.Log;

import org.fusesource.jansi.Ansi;

/**
* Log Level Enum
*/
public enum LogLevel {
DEBUG,
MESSAGE,
INFO,
WARN,
ERROR,
SEVERE,
DEBUG(Ansi.ansi().fgBrightCyan()),
MESSAGE(Ansi.ansi().fgBrightBlack()),
INFO(),
SUCCESS(Ansi.ansi().fgBrightGreen()),
WARN(Ansi.ansi().fgBrightYellow()),
ERROR(Ansi.ansi().fgBrightRed()),
SEVERE(Ansi.ansi().fgBrightRed()),

;

private String name;
private Ansi color;
LogLevel() {
this(null, null);
}
LogLevel(String name) {
this(name, null);
}
LogLevel(Ansi color) {
this(null, color);
}
LogLevel(String name, Ansi color) {
this.name = (name != null)?name:toString();
this.color = color;
}

/**
* Get the name of this Log Level
*
* @return Level Name
*/
public String getName() {
return name;
}

/**
* Get the color of this Log Level
*
* @return Level Color
*/
public Ansi getColor() {
return color;
}
}
Expand Up @@ -12,11 +12,11 @@
/**
* Primitive Log Handler Class
*/
public final class PrimitiveLogger extends Handler {
public final class LogTranslator extends Handler {
private final Logger log;
private boolean open = true;

PrimitiveLogger(Logger log) {
public LogTranslator(Logger log) {
this.log = log;
}

Expand Down
46 changes: 38 additions & 8 deletions GalaxiAPI/src/net/ME1312/Galaxi/Library/Log/Logger.java
Expand Up @@ -4,9 +4,9 @@
import net.ME1312.Galaxi.Library.Container;
import net.ME1312.Galaxi.Library.NamedContainer;
import net.ME1312.Galaxi.Library.Util;
import org.fusesource.jansi.Ansi;

import java.io.File;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
Expand All @@ -21,7 +21,6 @@
*/
public final class Logger {
private static final Container<PrintStream> pso = new Container<PrintStream>(null);
private static final Container<PrintStream> pse = new Container<PrintStream>(null);
private static boolean running = true;
static final LinkedList<NamedContainer<LogStream, String>> messages = new LinkedList<NamedContainer<LogStream, String>>();
private static final LinkedList<LogFilter> gFilters = new LinkedList<LogFilter>();
Expand All @@ -41,24 +40,53 @@ public Logger(String prefix) {
debug = new LogStream(this, DEBUG, pso);
message = new LogStream(this, MESSAGE, pso);
info = new LogStream(this, INFO, pso);
success = new LogStream(this, SUCCESS, pso);
warn = new LogStream(this, WARN, pso);
error = new LogStream(this, ERROR, pse);
severe = new LogStream(this, SEVERE, pse);
error = new LogStream(this, ERROR, pso);
severe = new LogStream(this, SEVERE, pso);

primitive = java.util.logging.Logger.getAnonymousLogger();
primitive.setUseParentHandlers(false);
primitive.addHandler(new PrimitiveLogger(this));
primitive.addHandler(new LogTranslator(this));

this.prefix = prefix;
}

public final LogStream debug;
public final LogStream message;
public final LogStream info;
public final LogStream success;
public final LogStream warn;
public final LogStream error;
public final LogStream severe;

/**
* Get the stream by Log Level
*
* @param level Log Level
* @return Log Stream
*/
public LogStream get(LogLevel level) {
switch (level) {
case DEBUG:
return debug;
case MESSAGE:
return message;
case INFO:
return info;
case SUCCESS:
return success;
case WARN:
return warn;
case ERROR:
return error;
case SEVERE:
return severe;
default:
return null;
}
}

/**
* Get this logger as a standard Java Logger
*
Expand Down Expand Up @@ -116,7 +144,7 @@ public static void removeStaticFilter(LogFilter filter) {
gFilters.remove(filter);
}

private static void log() {
private static void log(boolean COLOR_LEVELS) {
(thread = new Thread(() -> {
LogStream last = null;
boolean terminated = true;
Expand All @@ -125,7 +153,7 @@ private static void log() {
NamedContainer<LogStream, String> container = Util.getDespiteException(() -> messages.get(0), null);
if (container != null) {
LogStream stream = container.name();
String prefix = '[' + new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()) + "] [" + stream.getLogger().getPrefix() + File.separator + stream.getLevel() + "] > ";
String prefix = '[' + new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()) + "] [" + stream.getLogger().getPrefix() + File.separator + stream.getLevel().getName() + "] > ";
LinkedList<String> messages = new LinkedList<String>();
boolean terminate = false;
if (container.get().length() > 0) {
Expand Down Expand Up @@ -173,7 +201,9 @@ private static void log() {
if (response == null || response == Boolean.TRUE) {
if (terminated || last != stream) {
if (!terminated) stream.stream.get().print('\n');
if (COLOR_LEVELS && stream.getLevel().getColor() != null) stream.stream.get().write(stream.getLevel().getColor().toString().getBytes(StandardCharsets.UTF_8));
stream.stream.get().write(prefix.getBytes(StandardCharsets.UTF_8));
if (COLOR_LEVELS && stream.getLevel().getColor() != null) stream.stream.get().write(Ansi.ansi().reset().toString().getBytes(StandardCharsets.UTF_8));
}
last = stream;
logged = true;
Expand All @@ -195,7 +225,7 @@ private static void log() {
Util.isException(() -> Logger.messages.remove(0));
} catch (Throwable e) {
Util.isException(() -> Logger.messages.remove(0));
if (pse.get() != null) e.printStackTrace(pse.get());
if (pso.get() != null) e.printStackTrace(pso.get());
}
Util.isException(() -> Thread.sleep(32));
}
Expand Down
2 changes: 1 addition & 1 deletion GalaxiEngine/UI/pom.xml
Expand Up @@ -13,7 +13,7 @@
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>1.17.1</version>
<version>1.18</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
Expand Down
Expand Up @@ -174,7 +174,7 @@ public boolean dispatchKeyEvent(KeyEvent event) {
List<CharSequence> candidates = new LinkedList<CharSequence>();
if (icache == null || iauto == Boolean.FALSE) {
candidates.add((input.getText().startsWith(">"))?input.getText().substring(1):input.getText());
READER.getMethod("complete", String.class, int.class, List.class).invoke(reader, (input.getText().startsWith(">"))?input.getText().substring(1):input.getText(), position, candidates);
candidates.addAll((List) READER.getMethod("completeLine", String.class).invoke(reader, (input.getText().startsWith(">"))?input.getText().substring(1):input.getText()));
icache = new NamedContainer<>(position, candidates);
iautopos = (kpressed[KeyEvent.VK_SHIFT] != Boolean.TRUE)?candidates.size() - 1:1;
} else {
Expand Down
6 changes: 3 additions & 3 deletions GalaxiEngine/pom.xml
Expand Up @@ -13,14 +13,14 @@
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>1.17.1</version>
<version>1.18</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>jline</groupId>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
<version>2.12</version>
<version>3.12.1</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
Expand Down
17 changes: 4 additions & 13 deletions GalaxiEngine/src/net/ME1312/Galaxi/Engine/GalaxiEngine.java
@@ -1,6 +1,5 @@
package net.ME1312.Galaxi.Engine;

import jline.TerminalFactory;
import net.ME1312.Galaxi.Engine.Library.ConsoleReader;
import net.ME1312.Galaxi.Engine.Library.DefaultCommands;
import net.ME1312.Galaxi.Engine.Library.Log.SystemLogger;
Expand All @@ -18,13 +17,12 @@
import net.ME1312.Galaxi.Plugin.App;
import net.ME1312.Galaxi.Plugin.Plugin;
import net.ME1312.Galaxi.Plugin.PluginInfo;
import org.fusesource.jansi.AnsiConsole;
import org.jline.reader.LineReader;
import org.json.JSONObject;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLStreamHandler;
import java.nio.charset.Charset;
Expand All @@ -33,9 +31,7 @@
import java.util.concurrent.TimeUnit;
import java.util.jar.Manifest;

import static net.ME1312.Galaxi.Engine.GalaxiOption.APPLICATION_DIRECTORY;
import static net.ME1312.Galaxi.Engine.GalaxiOption.SHOW_DEBUG_MESSAGES;
import static net.ME1312.Galaxi.Engine.GalaxiOption.USE_JLINE;
import static net.ME1312.Galaxi.Engine.GalaxiOption.*;

/**
* Galaxi Engine Main Class
Expand Down Expand Up @@ -112,18 +108,13 @@ private GalaxiEngine(PluginInfo app) throws Exception {

if (!(SHOW_DEBUG_MESSAGES.usr().equalsIgnoreCase("true") || (SHOW_DEBUG_MESSAGES.usr().length() <= 0 && SHOW_DEBUG_MESSAGES.get())))
Logger.addStaticFilter((stream, message) -> (stream.getLevel() != LogLevel.DEBUG)?null:false);
if (!USE_JLINE.def()) TerminalFactory.configure(TerminalFactory.NONE);
jline.console.ConsoleReader jline = new jline.console.ConsoleReader(System.in, AnsiConsole.out);
jline.setPrompt("");
Util.reflect(SystemLogger.class.getDeclaredMethod("start", PrintStream.class, PrintStream.class, jline.console.ConsoleReader.class), null, AnsiConsole.out(), AnsiConsole.err(), jline);
this.console = new ConsoleReader(this, running);

this.app.getLogger().info.println("Loading " + engine.getName() + " v" + engine.getVersion().toString() + " Libraries");
if (app == null) this.app.getLogger().warn.println("GalaxiEngine is running in standalone mode");
else if (engine.getName().equalsIgnoreCase(this.app.getName())) throw new IllegalStateException("App name cannot be the same as the Engine's name");

console = new ConsoleReader(this, jline, running);
DefaultCommands.load(this);

URL.setURLStreamHandlerFactory(protocol -> {
HashMap<String, URLStreamHandler> protocols;
try {
Expand Down Expand Up @@ -233,7 +224,7 @@ public void terminate(int code) {
private boolean stopping = false;
private void exit(int code) {
running.set(false);
Util.isException(() -> Util.<jline.console.ConsoleReader>reflect(ConsoleReader.class.getDeclaredField("jline"), console).setPrompt(""));
//Util.isException(() -> Util.<LineReader>reflect(ConsoleReader.class.getDeclaredField("jline"), console).(""));

if (onStop != null) try {
onStop.run();
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
public final class GalaxiOption<T> extends Container<T> {
public static final GalaxiOption<File> APPLICATION_DIRECTORY = new GalaxiOption<>("user.dir", File::new);
public static final GalaxiOption<Boolean> COLOR_LOG_LEVELS = new GalaxiOption<>("galaxi.log.color_levels", usr -> usr.length() == 0 || usr.equalsIgnoreCase("true"));
public static final GalaxiOption<Double> CONSOLE_WINDOW_SIZE = new GalaxiOption<>("galaxi.console.scaling", usr -> (Util.getDespiteException(() -> Double.parseDouble(usr), 0D) > 0)?Double.parseDouble(usr):1);
public static final GalaxiOption<Boolean> ENABLE_RELOAD = new GalaxiOption<>(() -> false);
public static final GalaxiOption<File> LOG_DIRECTORY = new GalaxiOption<>(() -> new File(APPLICATION_DIRECTORY.get(), "Logs"));
Expand Down

0 comments on commit 0ac8953

Please sign in to comment.