Leverage Maven's -Dstyle.color to avoid coloring instead of stripping the ASCII codes in the client

This commit is contained in:
Peter Palaga
2021-03-07 20:50:36 +01:00
parent 2f4c70f6ef
commit 90cae47f1a
4 changed files with 51 additions and 54 deletions

View File

@@ -58,8 +58,6 @@ public enum Environment {
STOP(null, null, null, OptionType.VOID, Flags.OPTIONAL, "mvnd:--stop"),
/** Use one thread, no log buffering and the default project builder to behave like a standard maven */
SERIAL("mvnd.serial", null, Boolean.FALSE, OptionType.VOID, Flags.OPTIONAL, "mvnd:-1", "mvnd:--serial"),
/** Manage color output, can be either auto (the default), always or never */
COLOR(null, null, "auto", OptionType.STRING, Flags.OPTIONAL, "mvnd:--color"),
//
// Log properties
@@ -108,6 +106,8 @@ public enum Environment {
MAVEN_SHOW_VERSION(null, null, null, OptionType.BOOLEAN, Flags.INTERNAL, "mvn:-V", "mvn:--show-version"),
/** Define */
MAVEN_DEFINE(null, null, null, OptionType.STRING, Flags.INTERNAL, "mvn:-D", "mvn:--define"),
/** Whether the output should be styled using ANSI color codes; possible values: auto, always, never */
MAVEN_COLOR("style.color", null, "auto", OptionType.STRING, Flags.OPTIONAL),
//
// mvnd properties
@@ -379,13 +379,23 @@ public enum Environment {
return args.stream().anyMatch(arg -> Stream.of(prefixes).anyMatch(arg::startsWith));
}
public String getCommandLineOption(Collection<String> args) {
return getCommandLineOption(args, false);
}
public String removeCommandLineOption(Collection<String> args) {
return getCommandLineOption(args, true);
}
String getCommandLineOption(Collection<String> args, boolean remove) {
final String[] prefixes = getPrefixes();
String value = null;
for (Iterator<String> it = args.iterator(); it.hasNext();) {
String arg = it.next();
if (Stream.of(prefixes).anyMatch(arg::startsWith)) {
it.remove();
if (remove) {
it.remove();
}
if (type == OptionType.VOID) {
value = "";
} else {
@@ -395,7 +405,9 @@ public enum Environment {
if (value.isEmpty()) {
if (it.hasNext()) {
value = it.next();
it.remove();
if (remove) {
it.remove();
}
}
} else {
if (value.charAt(0) == '=') {

View File

@@ -35,12 +35,10 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import org.fusesource.jansi.internal.CLibrary;
import org.jline.terminal.Size;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.jline.terminal.impl.AbstractPosixTerminal;
import org.jline.utils.AnsiWriter;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
@@ -88,10 +86,6 @@ public class TerminalOutput implements ClientOutput {
*/
public static final int KEY_CTRL_M = 'M' & 0x1f;
public static final String COLOR_AUTO = "auto";
public static final String COLOR_ALWAYS = "always";
public static final String COLOR_NEVER = "never";
private static final AttributedStyle GREEN_FOREGROUND = new AttributedStyle().foreground(AttributedStyle.GREEN);
private static final AttributedStyle CYAN_FOREGROUND = new AttributedStyle().foreground(AttributedStyle.CYAN);
@@ -149,7 +143,7 @@ public class TerminalOutput implements ClientOutput {
}
}
public TerminalOutput(boolean noBuffering, int rollingWindowSize, Path logFile, String color) throws IOException {
public TerminalOutput(boolean noBuffering, int rollingWindowSize, Path logFile) throws IOException {
this.start = System.currentTimeMillis();
this.terminal = TerminalBuilder.terminal();
this.dumb = terminal.getType().startsWith("dumb");
@@ -165,7 +159,7 @@ public class TerminalOutput implements ClientOutput {
this.previousIntHandler = terminal.handle(Terminal.Signal.INT,
sig -> daemonDispatch.accept(Message.BareMessage.CANCEL_BUILD_SINGLETON));
this.display = new Display(terminal, false);
this.log = logFile == null ? new MessageCollector(color) : new FileLog(logFile, color);
this.log = logFile == null ? new MessageCollector() : new FileLog(logFile);
if (!dumb) {
final Thread r = new Thread(this::readInputLoop);
r.start();
@@ -771,15 +765,10 @@ public class TerminalOutput implements ClientOutput {
private final Writer out;
private final Path logFile;
private boolean closed;
public FileLog(Path logFile, String color) throws IOException {
public FileLog(Path logFile) throws IOException {
super();
if (COLOR_ALWAYS.equalsIgnoreCase(color)) {
this.out = Files.newBufferedWriter(logFile, StandardCharsets.UTF_8);
} else {
this.out = new AnsiWriter(Files.newBufferedWriter(logFile, StandardCharsets.UTF_8));
}
this.out = Files.newBufferedWriter(logFile, StandardCharsets.UTF_8);
this.logFile = logFile;
}
@@ -800,10 +789,7 @@ public class TerminalOutput implements ClientOutput {
@Override
public void close() throws IOException {
if (!closed) {
out.close();
closed = true;
}
out.close();
}
}
@@ -815,23 +801,6 @@ public class TerminalOutput implements ClientOutput {
class MessageCollector implements ClientLog {
private final List<String> messages = new ArrayList<>();
private final boolean strip;
public MessageCollector(String color) {
if (COLOR_NEVER.equalsIgnoreCase(color)) {
this.strip = true;
} else if (COLOR_ALWAYS.equalsIgnoreCase(color)) {
this.strip = false;
} else {
boolean strip;
try {
strip = CLibrary.isatty(1) == 0;
} catch (Throwable t) {
strip = true;
}
this.strip = strip;
}
}
@Override
public void accept(String message) {
@@ -841,7 +810,7 @@ public class TerminalOutput implements ClientOutput {
@Override
public void flush() {
clearDisplay();
messages.forEach(s -> terminal.writer().println(strip ? AttributedString.stripAnsi(s) : s));
messages.forEach(terminal.writer()::println);
messages.clear();
terminal.flush();
}