Skip to content

Commit c914a3c

Browse files
committed
Refactor terminal check into separate GUI class
1 parent 5771b75 commit c914a3c

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package convex.gui.utils;
2+
3+
import java.io.Console;
4+
import java.lang.reflect.InvocationTargetException;
5+
import java.lang.reflect.Method;
6+
7+
public class Terminal {
8+
9+
public static boolean checkIfTerminal() {
10+
Console c=System.console();
11+
// If null, we have no terminal (Java up to 21)
12+
if (c==null) return false;
13+
14+
// We have a console, but in Java 22+ we need to check if it is actually a terminal
15+
try {
16+
Method m=c.getClass().getMethod("isTerminal");
17+
return (Boolean)m.invoke(c);
18+
} catch (NoSuchMethodException e) {
19+
return true;
20+
} catch (SecurityException | IllegalAccessException | InvocationTargetException e) {
21+
// Shouldn't happen?
22+
return false;
23+
}
24+
}
25+
26+
}

convex-gui/src/main/java/convex/gui/utils/Toolkit.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
import java.awt.event.MouseAdapter;
1717
import java.awt.event.MouseEvent;
1818
import java.awt.image.BufferedImage;
19-
import java.io.Console;
2019
import java.io.IOException;
2120
import java.io.InputStream;
22-
import java.lang.reflect.InvocationTargetException;
23-
import java.lang.reflect.Method;
2421
import java.net.URI;
2522
import java.net.URISyntaxException;
2623
import java.net.URL;
@@ -369,21 +366,4 @@ public static JComponent withTitledBorder(String title, JComponent comp) {
369366
public static Icon menuIcon(int codePoint) {
370367
return SymbolIcon.get(codePoint,Toolkit.BUTTON_FONT.getSize());
371368
}
372-
373-
public static boolean checkIfTerminal() {
374-
Console c=System.console();
375-
// If null, we have no terminal (Java up to 21)
376-
if (c==null) return false;
377-
378-
// We have a console, but in Java 22+ we need to check if it is actually a terminal
379-
try {
380-
Method m=c.getClass().getMethod("isTerminal");
381-
return (Boolean)m.invoke(c);
382-
} catch (NoSuchMethodException e) {
383-
return true;
384-
} catch (SecurityException | IllegalAccessException | InvocationTargetException e) {
385-
// Shouldn't happen?
386-
return false;
387-
}
388-
}
389369
}

convex-integration/src/main/java/convex/main/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package convex.main;
22

3-
import convex.gui.utils.Toolkit;
3+
import convex.gui.utils.Terminal;
44

55
public class Main {
66

77
public static void main(String... args) {
8-
boolean terminal=Toolkit.checkIfTerminal();
8+
boolean terminal=Terminal.checkIfTerminal();
99
if (terminal) {
1010
convex.cli.Main.main(args);
1111
} else {

0 commit comments

Comments
 (0)