Skip to content

Commit

Permalink
fix: Do not output ANSI escape if outputFile is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
vlaw committed May 11, 2024
1 parent 69e8e0e commit 216c054
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public abstract class AbstractLifecycleMojo extends AbstractMojo {

/** Allow to specify an output file to bypass console output */
@Parameter(property = "buildplan.outputFile")
private File outputFile;
protected File outputFile;

/** Allow to specify appending to the output file */
@Parameter(property = "buildplan.appendOutput", defaultValue = "false")
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/codehaus/mojo/buildplan/ListMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.codehaus.mojo.buildplan;

import static java.lang.System.lineSeparator;
import static java.util.Objects.isNull;
import static org.codehaus.mojo.buildplan.display.TableColumn.ARTIFACT_ID;
import static org.codehaus.mojo.buildplan.display.TableColumn.EXECUTION_ID;
import static org.codehaus.mojo.buildplan.display.TableColumn.GOAL;
Expand All @@ -30,6 +31,7 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.mojo.buildplan.display.ListTableDescriptor;
import org.codehaus.mojo.buildplan.display.MojoExecutionDisplay;
import org.codehaus.mojo.buildplan.display.PlainTextMojoExecutionDisplay;
import org.codehaus.mojo.buildplan.display.TableDescriptor;
import org.codehaus.plexus.util.StringUtils;

Expand All @@ -49,6 +51,9 @@ public void executeInternal() throws MojoFailureException {
if (!showLifecycles) {
descriptor.hideLifecycle();
}

descriptor.setFileOutput(!isNull(outputFile));

String row = descriptor.rowFormat();
String head = descriptor.titleFormat();

Expand All @@ -69,7 +74,8 @@ public void executeInternal() throws MojoFailureException {

private String tableRow(String row, MojoExecution execution) {

MojoExecutionDisplay display = new MojoExecutionDisplay(execution);
MojoExecutionDisplay display =
isNull(outputFile) ? new MojoExecutionDisplay(execution) : new PlainTextMojoExecutionDisplay(execution);

if (showLifecycles) {
return String.format(
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/codehaus/mojo/buildplan/ListPhaseMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static java.lang.System.lineSeparator;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Objects.isNull;

import java.util.Collection;
import java.util.Map;
Expand All @@ -28,6 +29,7 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.mojo.buildplan.display.ListPhaseTableDescriptor;
import org.codehaus.mojo.buildplan.display.MojoExecutionDisplay;
import org.codehaus.mojo.buildplan.display.PlainTextMojoExecutionDisplay;
import org.codehaus.mojo.buildplan.display.TableDescriptor;
import org.codehaus.mojo.buildplan.util.Multimap;
import org.codehaus.plexus.util.StringUtils;
Expand Down Expand Up @@ -94,7 +96,8 @@ public void executeInternal() throws MojoFailureException {
}

private String line(String rowFormat, MojoExecution execution) {
MojoExecutionDisplay display = new MojoExecutionDisplay(execution);
MojoExecutionDisplay display =
isNull(outputFile) ? new MojoExecutionDisplay(execution) : new PlainTextMojoExecutionDisplay(execution);

return String.format(rowFormat, display.getArtifactId(), display.getGoal(), display.getExecutionId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class ListTableDescriptor extends AbstractTableDescriptor {
private int executionIdSize;
private int goalSize;

private boolean isFileOutput;

public void setFileOutput(boolean fileOutput) {
isFileOutput = fileOutput;
}

public static ListTableDescriptor of(Collection<MojoExecution> executions, DefaultLifecycles defaultLifecycles) {

Map<TableColumn, Integer> maxSize = findMaxSize(executions, defaultLifecycles, TableColumn.values());
Expand All @@ -48,6 +54,10 @@ public static ListTableDescriptor of(Collection<MojoExecution> executions, Defau
.setExecutionIdSize(maxSize.get(TableColumn.EXECUTION_ID));
}

private boolean isEscCodeEnabled() {
return !isFileOutput && MessageUtils.isColorEnabled();
}

public String rowFormat() {
return columns(getPluginSize());
}
Expand Down Expand Up @@ -84,7 +94,7 @@ private String columns(int pluginSize) {
}

public String titleFormat() {
if (MessageUtils.isColorEnabled()) {
if (isEscCodeEnabled()) {
return columns(getPluginSize() - ANSI_COLOR_CODES_LENGTH);
}
return columns(getPluginSize());
Expand All @@ -102,7 +112,7 @@ public int width() {

private int withSeparator(int... ints) {
int width = Arrays.stream(ints).sum() + (ints.length - 1) * SEPARATOR.length();
return MessageUtils.isColorEnabled() ? width - ANSI_COLOR_CODES_LENGTH : width;
return isEscCodeEnabled() ? width - ANSI_COLOR_CODES_LENGTH : width;
}

@Override
Expand Down

0 comments on commit 216c054

Please sign in to comment.