From f93f4c2c565ea31645ac10bd68ec822a7436a547 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Mon, 9 Oct 2023 16:38:35 -0500 Subject: [PATCH] Only add ANSI bolding if we're on a TTY Fixes #7864 --- core/src/main/java/org/jruby/Main.java | 10 +++++--- .../org/jruby/util/cli/OutputStrings.java | 24 ++++++++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/org/jruby/Main.java b/core/src/main/java/org/jruby/Main.java index a7b9e2935f0..d5021363ad8 100644 --- a/core/src/main/java/org/jruby/Main.java +++ b/core/src/main/java/org/jruby/Main.java @@ -463,9 +463,13 @@ private void doPrintUsage(boolean force) { if (config.getShouldPrintUsage() || force) { String rubyPager = getRubyPagerEnv(); + // Do not want to boot native subsystem here, so we do best guess based on System.console. It will be + // non-null only if both STDIN and STDOUT are tty. + boolean tty = System.console() != null; + if (rubyPager == null) { - config.getOutput().print(OutputStrings.getBasicUsageHelp()); - config.getOutput().print(OutputStrings.getFeaturesHelp()); + config.getOutput().print(OutputStrings.getBasicUsageHelp(tty)); + config.getOutput().print(OutputStrings.getFeaturesHelp(tty)); } else { try { ProcessBuilder builder = new ProcessBuilder(rubyPager); @@ -477,7 +481,7 @@ private void doPrintUsage(boolean force) { Process process = builder.start(); OutputStream in = process.getOutputStream(); - String fullHelp = OutputStrings.getBasicUsageHelp() + OutputStrings.getFeaturesHelp(); + String fullHelp = OutputStrings.getBasicUsageHelp(tty) + OutputStrings.getFeaturesHelp(tty); in.write(fullHelp.getBytes(StandardCharsets.UTF_8)); in.flush(); diff --git a/core/src/main/java/org/jruby/util/cli/OutputStrings.java b/core/src/main/java/org/jruby/util/cli/OutputStrings.java index b1dc0a5cf1a..0abc6efbf37 100644 --- a/core/src/main/java/org/jruby/util/cli/OutputStrings.java +++ b/core/src/main/java/org/jruby/util/cli/OutputStrings.java @@ -14,6 +14,10 @@ */ public class OutputStrings { public static String getBasicUsageHelp() { + return getBasicUsageHelp(false); + } + + public static String getBasicUsageHelp(boolean tty) { String[][] basicUsageOptions = { {"-0[octal]", "specify record separator (\\0, if no argument)"}, {"-a", "autosplit mode with -n or -p (splits $_ into $F)"}, @@ -65,13 +69,13 @@ public static String getBasicUsageHelp() { {"--enable=feature[,...], --disable=feature[,...]", "enable or disable features"} }; - String header = strBold("Usage:") + " jruby [switches] [--] [programfile] [arguments]"; - return buildOutputOptions(basicUsageOptions, header); + String header = strBold("Usage:", tty) + " jruby [switches] [--] [programfile] [arguments]"; + return buildOutputOptions(basicUsageOptions, header, tty); } - private static String buildOutputOptions(String[][] options, String header) { + private static String buildOutputOptions(String[][] options, String header, boolean tty) { StringBuilder sb = new StringBuilder(); - sb.append(strBold(header)).append("\n"); + sb.append(strBold(header, tty)).append("\n"); int max = Integer.MIN_VALUE; for (String[] strings : options) { @@ -86,12 +90,16 @@ private static String buildOutputOptions(String[][] options, String header) { String value = option[1]; String text = breakLine(value, 60, max + 8); - sb.append(" ").append(strBold(key)).append(generateSpaces(max + 5 - key.length())).append(text).append("\n"); + sb.append(" ").append(strBold(key, tty)).append(generateSpaces(max + 5 - key.length())).append(text).append("\n"); } return sb.toString(); } public static String getFeaturesHelp() { + return getFeaturesHelp(false); + } + + public static String getFeaturesHelp(boolean tty) { String header = "Features:"; String[][] options = { @@ -100,7 +108,7 @@ public static String getFeaturesHelp() { {"rubyopt", "RUBYOPT environment variable (default: " + (Options.CLI_RUBYOPT_ENABLE.defaultValue() ? "enabled" : "disabled") + ")"}, {"frozen-string-literal", "freeze all string literals (default: disabled)"}}; - return buildOutputOptions(options, header); + return buildOutputOptions(options, header, tty); } public static String getExtendedHelp() { return @@ -152,8 +160,8 @@ public static String getCopyrightString() { return String.format("JRuby - Copyright (C) 2001-%s The JRuby Community (and contribs)", LocalDate.now().getYear()); } - private static String strBold(String str) { - if(Platform.IS_WINDOWS) + private static String strBold(String str, boolean tty) { + if (!tty || Platform.IS_WINDOWS) return str; return "\033[1m" + str + "\033[0m";