Skip to content

Commit

Permalink
[GR-49359] Use java.io.Console#isTerminal() on 22+ for isatty() checks
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Oct 11, 2023
1 parent e79fe54 commit 50d5227
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/launcher/java/org/truffleruby/launcher/RubyLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ protected boolean parseCommonOption(String defaultOptionPrefix, Map<String, Stri
@Override
protected boolean runLauncherAction() {
String pager;
if (helpOptionUsed && System.console() != null && !(pager = getPagerFromEnv()).isEmpty()) {
if (helpOptionUsed && isTTY() && !(pager = getPagerFromEnv()).isEmpty()) {
try {
Process process = new ProcessBuilder(pager.split(" "))
.redirectOutput(Redirect.INHERIT) // set the output of the pager to the terminal and not a pipe
Expand Down Expand Up @@ -264,7 +264,7 @@ private int runRubyMain(Context.Builder contextBuilder, CommandLineOptions confi
break;
case IRB:
config.executionAction = ExecutionAction.PATH;
if (System.console() != null) {
if (isTTY()) {
getError().println(
"[ruby] WARNING: truffleruby starts IRB when stdin is a TTY instead of reading from stdin, use '-' to read from stdin");
config.executionAction = ExecutionAction.PATH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ private ConsoleHolder(
this.in = new IoStream(context, language, inFd, inIo);
this.out = new IoStream(context, language, outFd, outIo);

boolean isTTY = System.console() != null;
boolean system = isTTY && inFd == 0 && outFd == 1;
boolean system = IsTTYHelper.isTTY() && inFd == 0 && outFd == 1;

final Terminal terminal;
try {
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/org/truffleruby/stdlib/readline/IsTTYHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 2.0, or
* GNU General Public License version 2, or
* GNU Lesser General Public License version 2.1.
*/
package org.truffleruby.stdlib.readline;

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

public final class IsTTYHelper {

private static final Method IS_TERMINAL_METHOD = getIsTerminalMethod();

private static Method getIsTerminalMethod() {
try {
return Console.class.getMethod("isTerminal");
} catch (NoSuchMethodException e) {
return null;
}
}

public static boolean isTTY() {
Console console = System.console();
if (console == null) {
return false;
}
if (IS_TERMINAL_METHOD != null) {
try {
return (boolean) IS_TERMINAL_METHOD.invoke(console);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new Error(e);
}
} else {
return true;
}
}

}

0 comments on commit 50d5227

Please sign in to comment.