Skip to content

Commit

Permalink
Only add ANSI bolding if we're on a TTY
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Oct 9, 2023
1 parent 852abd4 commit f93f4c2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
10 changes: 7 additions & 3 deletions core/src/main/java/org/jruby/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down
24 changes: 16 additions & 8 deletions core/src/main/java/org/jruby/util/cli/OutputStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)"},
Expand Down Expand Up @@ -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) {
Expand All @@ -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 = {
Expand All @@ -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
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit f93f4c2

Please sign in to comment.