mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-09-11 13:39:32 +00:00
Fix color output for file / tty #358
This commit is contained in:
@@ -72,6 +72,12 @@ public class DefaultClient implements Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Color
|
||||||
|
String color = Environment.COLOR.removeCommandLineOption(args);
|
||||||
|
if (color == null) {
|
||||||
|
color = Environment.COLOR.getDefault();
|
||||||
|
}
|
||||||
|
|
||||||
// Serial
|
// Serial
|
||||||
if (Environment.SERIAL.removeCommandLineOption(args) != null) {
|
if (Environment.SERIAL.removeCommandLineOption(args) != null) {
|
||||||
System.setProperty(Environment.SERIAL.getProperty(), Boolean.toString(true));
|
System.setProperty(Environment.SERIAL.getProperty(), Boolean.toString(true));
|
||||||
@@ -93,7 +99,7 @@ public class DefaultClient implements Client {
|
|||||||
|
|
||||||
int exitCode = 0;
|
int exitCode = 0;
|
||||||
boolean noBuffering = batchMode || parameters.noBuffering();
|
boolean noBuffering = batchMode || parameters.noBuffering();
|
||||||
try (TerminalOutput output = new TerminalOutput(noBuffering, parameters.rollingWindowSize(), logFile)) {
|
try (TerminalOutput output = new TerminalOutput(noBuffering, parameters.rollingWindowSize(), logFile, color)) {
|
||||||
try {
|
try {
|
||||||
final ExecutionResult result = new DefaultClient(parameters).execute(output, args);
|
final ExecutionResult result = new DefaultClient(parameters).execute(output, args);
|
||||||
exitCode = result.getExitCode();
|
exitCode = result.getExitCode();
|
||||||
|
@@ -58,6 +58,8 @@ public enum Environment {
|
|||||||
STOP(null, null, null, OptionType.VOID, Flags.OPTIONAL, "mvnd:--stop"),
|
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 */
|
/** 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"),
|
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
|
// Log properties
|
||||||
|
@@ -35,10 +35,12 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.stream.Collector;
|
import java.util.stream.Collector;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import org.fusesource.jansi.internal.CLibrary;
|
||||||
import org.jline.terminal.Size;
|
import org.jline.terminal.Size;
|
||||||
import org.jline.terminal.Terminal;
|
import org.jline.terminal.Terminal;
|
||||||
import org.jline.terminal.TerminalBuilder;
|
import org.jline.terminal.TerminalBuilder;
|
||||||
import org.jline.terminal.impl.AbstractPosixTerminal;
|
import org.jline.terminal.impl.AbstractPosixTerminal;
|
||||||
|
import org.jline.utils.AnsiWriter;
|
||||||
import org.jline.utils.AttributedString;
|
import org.jline.utils.AttributedString;
|
||||||
import org.jline.utils.AttributedStringBuilder;
|
import org.jline.utils.AttributedStringBuilder;
|
||||||
import org.jline.utils.AttributedStyle;
|
import org.jline.utils.AttributedStyle;
|
||||||
@@ -86,6 +88,10 @@ public class TerminalOutput implements ClientOutput {
|
|||||||
*/
|
*/
|
||||||
public static final int KEY_CTRL_M = 'M' & 0x1f;
|
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 GREEN_FOREGROUND = new AttributedStyle().foreground(AttributedStyle.GREEN);
|
||||||
private static final AttributedStyle CYAN_FOREGROUND = new AttributedStyle().foreground(AttributedStyle.CYAN);
|
private static final AttributedStyle CYAN_FOREGROUND = new AttributedStyle().foreground(AttributedStyle.CYAN);
|
||||||
|
|
||||||
@@ -143,7 +149,7 @@ public class TerminalOutput implements ClientOutput {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public TerminalOutput(boolean noBuffering, int rollingWindowSize, Path logFile) throws IOException {
|
public TerminalOutput(boolean noBuffering, int rollingWindowSize, Path logFile, String color) throws IOException {
|
||||||
this.start = System.currentTimeMillis();
|
this.start = System.currentTimeMillis();
|
||||||
this.terminal = TerminalBuilder.terminal();
|
this.terminal = TerminalBuilder.terminal();
|
||||||
this.dumb = terminal.getType().startsWith("dumb");
|
this.dumb = terminal.getType().startsWith("dumb");
|
||||||
@@ -159,7 +165,7 @@ public class TerminalOutput implements ClientOutput {
|
|||||||
this.previousIntHandler = terminal.handle(Terminal.Signal.INT,
|
this.previousIntHandler = terminal.handle(Terminal.Signal.INT,
|
||||||
sig -> daemonDispatch.accept(Message.BareMessage.CANCEL_BUILD_SINGLETON));
|
sig -> daemonDispatch.accept(Message.BareMessage.CANCEL_BUILD_SINGLETON));
|
||||||
this.display = new Display(terminal, false);
|
this.display = new Display(terminal, false);
|
||||||
this.log = logFile == null ? new MessageCollector() : new FileLog(logFile);
|
this.log = logFile == null ? new MessageCollector(color) : new FileLog(logFile, color);
|
||||||
if (!dumb) {
|
if (!dumb) {
|
||||||
final Thread r = new Thread(this::readInputLoop);
|
final Thread r = new Thread(this::readInputLoop);
|
||||||
r.start();
|
r.start();
|
||||||
@@ -763,10 +769,15 @@ public class TerminalOutput implements ClientOutput {
|
|||||||
|
|
||||||
private final Writer out;
|
private final Writer out;
|
||||||
private final Path logFile;
|
private final Path logFile;
|
||||||
|
private boolean closed;
|
||||||
|
|
||||||
public FileLog(Path logFile) throws IOException {
|
public FileLog(Path logFile, String color) throws IOException {
|
||||||
super();
|
super();
|
||||||
this.out = Files.newBufferedWriter(logFile, StandardCharsets.UTF_8);
|
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.logFile = logFile;
|
this.logFile = logFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -787,7 +798,10 @@ public class TerminalOutput implements ClientOutput {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
out.close();
|
if (!closed) {
|
||||||
|
out.close();
|
||||||
|
closed = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -799,6 +813,23 @@ public class TerminalOutput implements ClientOutput {
|
|||||||
class MessageCollector implements ClientLog {
|
class MessageCollector implements ClientLog {
|
||||||
|
|
||||||
private final List<String> messages = new ArrayList<>();
|
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
|
@Override
|
||||||
public void accept(String message) {
|
public void accept(String message) {
|
||||||
@@ -808,7 +839,7 @@ public class TerminalOutput implements ClientOutput {
|
|||||||
@Override
|
@Override
|
||||||
public void flush() {
|
public void flush() {
|
||||||
clearDisplay();
|
clearDisplay();
|
||||||
messages.forEach(terminal.writer()::println);
|
messages.forEach(s -> terminal.writer().println(strip ? AttributedString.stripAnsi(s) : s));
|
||||||
messages.clear();
|
messages.clear();
|
||||||
terminal.flush();
|
terminal.flush();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user