Skip to content

Commit

Permalink
jenkinsci#185 Identify first line while rendering log to allow shortl…
Browse files Browse the repository at this point in the history
…og coloring to kick-in
  • Loading branch information
tszmytka committed Jul 27, 2020
1 parent a220892 commit 1f25f07
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
26 changes: 22 additions & 4 deletions src/main/java/hudson/plugins/ansicolor/ColorConsoleAnnotator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import hudson.model.Queue;
import hudson.model.Run;
import hudson.plugins.ansicolor.action.ColorizedAction;
import hudson.plugins.ansicolor.action.LineIdentifier;
import jenkins.model.Jenkins;
import org.apache.commons.io.output.CountingOutputStream;
import org.apache.commons.io.output.NullOutputStream;
Expand Down Expand Up @@ -59,25 +60,38 @@ final class ColorConsoleAnnotator extends ConsoleAnnotator<Object> {

private final String defaultColorMapName;

private final LineIdentifier lineIdentifier;

@CheckForNull
private String colorMapName;

@Nonnull
private List<AnsiAttributeElement> openTags = Collections.emptyList();

private ColorConsoleAnnotator(String defaultColorMapName) {
private long lineNo;

private ColorConsoleAnnotator(String defaultColorMapName, LineIdentifier lineIdentifier, long startLineNo) {
this.defaultColorMapName = defaultColorMapName;
this.lineIdentifier = lineIdentifier;
this.lineNo = startLineNo;
}

@Override
public ConsoleAnnotator<Object> annotate(@Nonnull Object context, @Nonnull MarkupText text) {
final ColorizedAction colorizedAction = ColorizedAction.parseAction(text, runOf(context));
lineNo++;
Run<?, ?> run = runOf(context);
if (run == null) {
return this;
}
final ColorizedAction colorizedAction = lineNo == 1
? ColorizedAction.parseAction(text.getText(), lineNo, run, lineIdentifier)
: ColorizedAction.parseAction(text, run);
switch (colorizedAction.getCommand()) {
case START:
colorMapName = colorizedAction.getColorMapName();
break;
case STOP:
return FACTORY.newInstance(context);
return FACTORY.newInstance(context, lineNo);
case IGNORE:
return this;
default:
Expand Down Expand Up @@ -212,7 +226,11 @@ public static final class Factory extends ConsoleAnnotatorFactory<Object> {

@Override
public ConsoleAnnotator<Object> newInstance(Object context) {
return new ColorConsoleAnnotator(Jenkins.get().getDescriptorByType(AnsiColorBuildWrapper.DescriptorImpl.class).getGlobalColorMapName());
return newInstance(context, 0);
}

private ConsoleAnnotator<Object> newInstance(Object context, long startLineNo) {
return new ColorConsoleAnnotator(Jenkins.get().getDescriptorByType(AnsiColorBuildWrapper.DescriptorImpl.class).getGlobalColorMapName(), new LineIdentifier(), startLineNo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ActionNote extends ConsoleNote<Run<?, ?>> {
private final String actionId;

public ActionNote(ColorizedAction action) {
actionId = action.getId().toString();
actionId = action.getId();
}

@Override
Expand Down

0 comments on commit 1f25f07

Please sign in to comment.