Skip to content

Commit ef7ff66

Browse files
authored
Update ANSI escape code regex (#530)
1 parent c5cf791 commit ef7ff66

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/main/java/io/jenkins/plugins/pipelinegraphview/utils/PipelineStepApi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ private List<PipelineStep> parseSteps(List<FlowNodeWrapper> stepNodes, String st
6767
return steps;
6868
}
6969

70-
private static String cleanTextContent(String text) {
70+
static String cleanTextContent(String text) {
7171
// strips off all ANSI color codes
72-
text = text.replaceAll("\\[\\d+m", "");
72+
text = text.replaceAll("\\e\\[(\\d+[;:]?)+m", "");
7373
return text.trim();
7474
}
7575

src/test/java/io/jenkins/plugins/pipelinegraphview/utils/PipelineStepApiTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.jvnet.hudson.test.Issue;
3434
import org.jvnet.hudson.test.JenkinsRule;
3535
import org.jvnet.hudson.test.LoggerRule;
36+
import org.jvnet.hudson.test.WithoutJenkins;
3637

3738
public class PipelineStepApiTest {
3839

@@ -553,4 +554,20 @@ public void gh362_stepsBeforeStepBlockGetValidStatus() throws Exception {
553554
TestUtils.assertTimesInRange(n, checks.get(n.getName()));
554555
}
555556
}
557+
558+
@Test
559+
@WithoutJenkins
560+
public void clearTextContents() {
561+
assertThat(PipelineStepApi.cleanTextContent("Hello World"), equalTo("Hello World"));
562+
assertThat(PipelineStepApi.cleanTextContent("abc[10m]def"), equalTo("abc[10m]def"));
563+
// 3-4 bit
564+
assertThat(PipelineStepApi.cleanTextContent("\033[32mHello World\033[0m"), equalTo("Hello World"));
565+
assertThat(PipelineStepApi.cleanTextContent("\033[1;32mHello World\033[0m"), equalTo("Hello World"));
566+
// 8-bit
567+
assertThat(PipelineStepApi.cleanTextContent("\033[38;5;6mHello World\033[0m"), equalTo("Hello World"));
568+
// with colon as separator character
569+
assertThat(PipelineStepApi.cleanTextContent("\033[38:5:6mHello World\033[0m"), equalTo("Hello World"));
570+
// 24-bit rgb
571+
assertThat(PipelineStepApi.cleanTextContent("\033[38;2;0;255;128mHello World\033[0m"), equalTo("Hello World"));
572+
}
556573
}

src/test/resources/io/jenkins/plugins/pipelinegraphview/utils/unstableSmokes.jenkinsfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ stage('unstable-one') {
33
timeout(1) {
44
unstable('oops-one')
55
}
6-
echo('bar')
6+
echo('\033[1;32mbar\033[0m')
77
}
88
stage('success') {
99
echo('baz')

0 commit comments

Comments
 (0)