From 5664137ae9adb681b203279b7969a3aae5270ca7 Mon Sep 17 00:00:00 2001 From: mattirn Date: Sun, 17 Mar 2019 19:38:02 +0100 Subject: [PATCH] added test if terminal support ansi --- .../main/java/org/jline/builtins/Options.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/builtins/src/main/java/org/jline/builtins/Options.java b/builtins/src/main/java/org/jline/builtins/Options.java index 2f13f3951..4761f66b9 100644 --- a/builtins/src/main/java/org/jline/builtins/Options.java +++ b/builtins/src/main/java/org/jline/builtins/Options.java @@ -523,20 +523,24 @@ public static class HelpPrinter { private final Pattern patternOption = Pattern.compile("(\\s|\\[)(-\\?|[-]{1,2}[A-Za-z-]+\\b){1}"); private final String title = "Usage"; private final String ansiReset = "\033[0m"; - private String ansi4title; - private String ansi4command; - private String ansi4argument; - private String ansi4option; + private String ansi4title = ""; + private String ansi4command = ""; + private String ansi4argument = ""; + private String ansi4option= ""; private boolean color = true; private Terminal terminal; public HelpPrinter() { - this(null); + this(null); } public HelpPrinter(Terminal terminal) { this.terminal = terminal; - setColors("ti=1;34:co=1:ar=3:op=33"); + if (ansiSupported()) { + setColors("ti=1;34:co=1:ar=3:op=33"); + } else { + this.color = false; + } } public void setColor(boolean color) { @@ -584,6 +588,11 @@ public void setColors (String str) { } + private boolean ansiSupported () { + return (new AttributedString("HP", AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)) + .toAnsi(terminal).split("HP").length > 0); + } + private String styleToAnsiCode(AttributedStyle style) { String[] as = new AttributedString("HP", style).toAnsi(terminal).split("HP"); return as.length > 0 ? as[0] : ansiReset;