Skip to content

Commit 50d5227

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

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ protected boolean parseCommonOption(String defaultOptionPrefix, Map<String, Stri
230230
@Override
231231
protected boolean runLauncherAction() {
232232
String pager;
233-
if (helpOptionUsed && System.console() != null && !(pager = getPagerFromEnv()).isEmpty()) {
233+
if (helpOptionUsed && isTTY() && !(pager = getPagerFromEnv()).isEmpty()) {
234234
try {
235235
Process process = new ProcessBuilder(pager.split(" "))
236236
.redirectOutput(Redirect.INHERIT) // set the output of the pager to the terminal and not a pipe
@@ -264,7 +264,7 @@ private int runRubyMain(Context.Builder contextBuilder, CommandLineOptions confi
264264
break;
265265
case IRB:
266266
config.executionAction = ExecutionAction.PATH;
267-
if (System.console() != null) {
267+
if (isTTY()) {
268268
getError().println(
269269
"[ruby] WARNING: truffleruby starts IRB when stdin is a TTY instead of reading from stdin, use '-' to read from stdin");
270270
config.executionAction = ExecutionAction.PATH;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ private ConsoleHolder(
116116
this.in = new IoStream(context, language, inFd, inIo);
117117
this.out = new IoStream(context, language, outFd, outIo);
118118

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

122121
final Terminal terminal;
123122
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.stdlib.readline;
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)