Skip to content

Commit

Permalink
Refactor terminal check into separate GUI class
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 27, 2024
1 parent 5771b75 commit c914a3c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
26 changes: 26 additions & 0 deletions convex-gui/src/main/java/convex/gui/utils/Terminal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package convex.gui.utils;

import java.io.Console;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Terminal {

public static boolean checkIfTerminal() {
Console c=System.console();
// If null, we have no terminal (Java up to 21)
if (c==null) return false;

// We have a console, but in Java 22+ we need to check if it is actually a terminal
try {
Method m=c.getClass().getMethod("isTerminal");
return (Boolean)m.invoke(c);
} catch (NoSuchMethodException e) {
return true;
} catch (SecurityException | IllegalAccessException | InvocationTargetException e) {
// Shouldn't happen?
return false;
}
}

}
20 changes: 0 additions & 20 deletions convex-gui/src/main/java/convex/gui/utils/Toolkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.Console;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -369,21 +366,4 @@ public static JComponent withTitledBorder(String title, JComponent comp) {
public static Icon menuIcon(int codePoint) {
return SymbolIcon.get(codePoint,Toolkit.BUTTON_FONT.getSize());
}

public static boolean checkIfTerminal() {
Console c=System.console();
// If null, we have no terminal (Java up to 21)
if (c==null) return false;

// We have a console, but in Java 22+ we need to check if it is actually a terminal
try {
Method m=c.getClass().getMethod("isTerminal");
return (Boolean)m.invoke(c);
} catch (NoSuchMethodException e) {
return true;
} catch (SecurityException | IllegalAccessException | InvocationTargetException e) {
// Shouldn't happen?
return false;
}
}
}
4 changes: 2 additions & 2 deletions convex-integration/src/main/java/convex/main/Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package convex.main;

import convex.gui.utils.Toolkit;
import convex.gui.utils.Terminal;

public class Main {

public static void main(String... args) {
boolean terminal=Toolkit.checkIfTerminal();
boolean terminal=Terminal.checkIfTerminal();
if (terminal) {
convex.cli.Main.main(args);
} else {
Expand Down

0 comments on commit c914a3c

Please sign in to comment.