Skip to content

Commit

Permalink
Make convex.jar work as CLI or desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 26, 2024
1 parent 5909014 commit 4ff3c6f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
12 changes: 2 additions & 10 deletions convex-gui/src/main/java/convex/gui/components/AbstractGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import javax.swing.JFrame;
import javax.swing.JPanel;
Expand Down Expand Up @@ -67,15 +66,8 @@ public void setupFrame(JFrame frame) {
}

public void waitForClose() {
try {
finished.get();
close();
} catch (ExecutionException e) {
// Probably won't happen?
} catch (InterruptedException e) {
// Set interrupt status, in case caller wants to handle this
Thread.currentThread().interrupt();
}
finished.join();
close();
}

public JFrame getFrame() {
Expand Down
20 changes: 20 additions & 0 deletions convex-gui/src/main/java/convex/gui/utils/Toolkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
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 @@ -366,4 +369,21 @@ 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;
}
}
}
5 changes: 4 additions & 1 deletion convex-integration/src/main/java/convex/main/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package convex.main;

import convex.gui.utils.Toolkit;

public class Main {

public static void main(String... args) {
if (System.console()!=null) {
boolean terminal=Toolkit.checkIfTerminal();
if (terminal) {
convex.cli.Main.main(args);
} else {
convex.gui.MainGUI.main(args);
Expand Down

0 comments on commit 4ff3c6f

Please sign in to comment.