Skip to content

Commit e3f5895

Browse files
committed
[GR-49359] Use java.io.Console#isTerminal() on 22+ for isatty() checks
1 parent f2b7cad commit e3f5895

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

src/launcher/java/org/truffleruby/launcher/RubyLauncher.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.graalvm.polyglot.PolyglotException;
3030
import org.graalvm.polyglot.Source;
3131
import org.graalvm.polyglot.Value;
32+
import org.truffleruby.shared.IsTTYHelper;
3233
import org.truffleruby.shared.Metrics;
3334
import org.truffleruby.shared.TruffleRuby;
3435
import org.truffleruby.shared.options.OptionsCatalog;
@@ -230,7 +231,7 @@ protected boolean parseCommonOption(String defaultOptionPrefix, Map<String, Stri
230231
@Override
231232
protected boolean runLauncherAction() {
232233
String pager;
233-
if (helpOptionUsed && System.console() != null && !(pager = getPagerFromEnv()).isEmpty()) {
234+
if (helpOptionUsed && IsTTYHelper.isTTY() && !(pager = getPagerFromEnv()).isEmpty()) {
234235
try {
235236
Process process = new ProcessBuilder(pager.split(" "))
236237
.redirectOutput(Redirect.INHERIT) // set the output of the pager to the terminal and not a pipe
@@ -264,7 +265,7 @@ private int runRubyMain(Context.Builder contextBuilder, CommandLineOptions confi
264265
break;
265266
case IRB:
266267
config.executionAction = ExecutionAction.PATH;
267-
if (System.console() != null) {
268+
if (IsTTYHelper.isTTY()) {
268269
getError().println(
269270
"[ruby] WARNING: truffleruby starts IRB when stdin is a TTY instead of reading from stdin, use '-' to read from stdin");
270271
config.executionAction = ExecutionAction.PATH;

src/main/java/org/truffleruby/stdlib/readline/ConsoleHolder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262

6363
import com.oracle.truffle.api.CompilerDirectives;
6464
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
65+
import org.truffleruby.shared.IsTTYHelper;
6566

6667
public final class ConsoleHolder {
6768

@@ -116,8 +117,7 @@ private ConsoleHolder(
116117
this.in = new IoStream(context, language, inFd, inIo);
117118
this.out = new IoStream(context, language, outFd, outIo);
118119

119-
boolean isTTY = System.console() != null;
120-
boolean system = isTTY && inFd == 0 && outFd == 1;
120+
boolean system = IsTTYHelper.isTTY() && inFd == 0 && outFd == 1;
121121

122122
final Terminal terminal;
123123
try {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 2.0, or
7+
* GNU General Public License version 2, or
8+
* GNU Lesser General Public License version 2.1.
9+
*/
10+
package org.truffleruby.shared;
11+
12+
import java.io.Console;
13+
import java.lang.reflect.InvocationTargetException;
14+
import java.lang.reflect.Method;
15+
16+
public final class IsTTYHelper {
17+
18+
private static final Method IS_TERMINAL_METHOD = getIsTerminalMethod();
19+
20+
private static Method getIsTerminalMethod() {
21+
try {
22+
return Console.class.getMethod("isTerminal");
23+
} catch (NoSuchMethodException e) {
24+
return null;
25+
}
26+
}
27+
28+
public static boolean isTTY() {
29+
Console console = System.console();
30+
if (console == null) {
31+
return false;
32+
}
33+
if (IS_TERMINAL_METHOD != null) {
34+
try {
35+
return (boolean) IS_TERMINAL_METHOD.invoke(console);
36+
} catch (IllegalAccessException | InvocationTargetException e) {
37+
throw new Error(e);
38+
}
39+
} else {
40+
return true;
41+
}
42+
}
43+
44+
}

0 commit comments

Comments
 (0)