Skip to content

Commit 65a27ef

Browse files
makingphilwebb
authored andcommitted
Add ANSI 8-bit color support
Update ANSI property support to include an 8-bit (256 color) option. See gh-18264
1 parent 5ca5ec8 commit 65a27ef

File tree

6 files changed

+288
-0
lines changed

6 files changed

+288
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.commons.logging.Log;
2929
import org.apache.commons.logging.LogFactory;
3030

31+
import org.springframework.boot.ansi.Ansi256PropertySource;
3132
import org.springframework.boot.ansi.AnsiPropertySource;
3233
import org.springframework.core.env.Environment;
3334
import org.springframework.core.env.MapPropertySource;
@@ -43,6 +44,7 @@
4344
*
4445
* @author Phillip Webb
4546
* @author Vedran Pavic
47+
* @author Toshiaki Maki
4648
* @since 1.2.0
4749
*/
4850
public class ResourceBanner implements Banner {
@@ -80,6 +82,7 @@ protected List<PropertyResolver> getPropertyResolvers(Environment environment, C
8082
resolvers.add(environment);
8183
resolvers.add(getVersionResolver(sourceClass));
8284
resolvers.add(getAnsiResolver());
85+
resolvers.add(getAnsi256Resolver());
8386
resolvers.add(getTitleResolver(sourceClass));
8487
return resolvers;
8588
}
@@ -123,6 +126,12 @@ private PropertyResolver getAnsiResolver() {
123126
return new PropertySourcesPropertyResolver(sources);
124127
}
125128

129+
private PropertyResolver getAnsi256Resolver() {
130+
MutablePropertySources sources = new MutablePropertySources();
131+
sources.addFirst(new Ansi256PropertySource("ansi256"));
132+
return new PropertySourcesPropertyResolver(sources);
133+
}
134+
126135
private PropertyResolver getTitleResolver(Class<?> sourceClass) {
127136
MutablePropertySources sources = new MutablePropertySources();
128137
String applicationTitle = getApplicationTitle(sourceClass);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.ansi;
18+
19+
/**
20+
* {@link AnsiElement} implementation for Ansi 256 colors.
21+
* <p>
22+
* use {@link Ansi256Color.Foreground} or {@link Ansi256Color.Background} as a concrete
23+
* class.
24+
*
25+
* @author Toshiaki Maki
26+
* @since 2.2.0
27+
*/
28+
public abstract class Ansi256Color implements AnsiElement {
29+
30+
/**
31+
* color code
32+
*/
33+
final int colorCode;
34+
35+
/**
36+
* @param colorCode color code (must be 0-255)
37+
* @throws IllegalArgumentException if color code is not between 0 and 255.
38+
*/
39+
Ansi256Color(int colorCode) {
40+
if (colorCode < 0 || colorCode > 255) {
41+
throw new IllegalArgumentException("'colorCode' must be between 0 and 255.");
42+
}
43+
this.colorCode = colorCode;
44+
}
45+
46+
/**
47+
* {@link Ansi256Color} foreground colors.
48+
*
49+
* @author Toshiaki Maki
50+
* @since 2.2.0
51+
*/
52+
public static class Foreground extends Ansi256Color {
53+
54+
public Foreground(int colorCode) {
55+
super(colorCode);
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return "38;5;" + super.colorCode;
61+
}
62+
63+
}
64+
65+
/**
66+
* {@link Ansi256Color} background colors.
67+
*
68+
* @author Toshiaki Maki
69+
* @since 2.2.0
70+
*/
71+
public static class Background extends Ansi256Color {
72+
73+
public Background(int colorCode) {
74+
super(colorCode);
75+
}
76+
77+
@Override
78+
public String toString() {
79+
return "48;5;" + super.colorCode;
80+
}
81+
82+
}
83+
84+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.ansi;
18+
19+
import org.springframework.core.env.PropertyResolver;
20+
import org.springframework.core.env.PropertySource;
21+
import org.springframework.util.StringUtils;
22+
23+
/**
24+
* {@link PropertyResolver} for {@link Ansi256Color.Background} and
25+
* {@link Ansi256Color.Foreground} elements. Supports properties of the form
26+
* {@code Ansi256Color.Foreground_N} and {@code Ansi256Color.Background_N} ({@code N} must
27+
* be between 0 and 255).
28+
*
29+
* @author Toshiaki Maki
30+
* @since 2.2.0
31+
*/
32+
public class Ansi256PropertySource extends PropertySource<AnsiElement> {
33+
34+
private static final String PREFIX = "Ansi256Color.";
35+
36+
private static final String FOREGROUND_PREFIX = PREFIX + "Foreground_";
37+
38+
private static final String BACKGROUND_PREFIX = PREFIX + "Background_";
39+
40+
/**
41+
* Create a new {@link Ansi256PropertySource} instance.
42+
* @param name the name of the property source
43+
*/
44+
public Ansi256PropertySource(String name) {
45+
super(name);
46+
}
47+
48+
@Override
49+
public Object getProperty(String name) {
50+
if (StringUtils.hasLength(name)) {
51+
if (name.startsWith(FOREGROUND_PREFIX)) {
52+
final int colorCode = Integer.parseInt(name.substring(FOREGROUND_PREFIX.length()));
53+
return AnsiOutput.encode(new Ansi256Color.Foreground(colorCode));
54+
}
55+
else if (name.startsWith(BACKGROUND_PREFIX)) {
56+
final int colorCode = Integer.parseInt(name.substring(BACKGROUND_PREFIX.length()));
57+
return AnsiOutput.encode(new Ansi256Color.Background(colorCode));
58+
}
59+
}
60+
return null;
61+
}
62+
63+
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ResourceBannerTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*
4040
* @author Phillip Webb
4141
* @author Vedran Pavic
42+
* @author Toshiaki Maki
4243
*/
4344
class ResourceBannerTests {
4445

@@ -95,6 +96,24 @@ void renderWithColorsButDisabled() {
9596
assertThat(banner).startsWith("This is red.");
9697
}
9798

99+
@Test
100+
void renderWith256Colors() {
101+
Resource resource = new ByteArrayResource(
102+
"${Ansi256Color.Foreground_208}This is orange.${Ansi.NORMAL}".getBytes());
103+
AnsiOutput.setEnabled(AnsiOutput.Enabled.ALWAYS);
104+
String banner = printBanner(resource, null, null, null);
105+
assertThat(banner).startsWith("\033[38;5;208mThis is orange.\u001B[0m");
106+
}
107+
108+
@Test
109+
void renderWith256ColorsButDisabled() {
110+
Resource resource = new ByteArrayResource(
111+
"${Ansi256Color.Foreground_208}This is orange.${Ansi.NORMAL}".getBytes());
112+
AnsiOutput.setEnabled(AnsiOutput.Enabled.NEVER);
113+
String banner = printBanner(resource, null, null, null);
114+
assertThat(banner).startsWith("This is orange.");
115+
}
116+
98117
@Test
99118
void renderWithTitle() {
100119
Resource resource = new ByteArrayResource("banner ${application.title} ${a}".getBytes());
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.ansi;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
23+
24+
/**
25+
* Tests for {@link Ansi256Color}.
26+
*
27+
* @author Toshiaki Maki
28+
*/
29+
class Ansi256ColorTest {
30+
31+
@Test
32+
void testForeground() {
33+
final Ansi256Color ansi256Color = new Ansi256Color.Foreground(208);
34+
assertThat(ansi256Color.toString()).isEqualTo("38;5;208");
35+
}
36+
37+
@Test
38+
void testBackground() {
39+
final Ansi256Color ansi256Color = new Ansi256Color.Background(208);
40+
assertThat(ansi256Color.toString()).isEqualTo("48;5;208");
41+
}
42+
43+
@Test
44+
void testIllegalColorCode() {
45+
try {
46+
new Ansi256Color.Foreground(256);
47+
failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
48+
}
49+
catch (IllegalArgumentException ex) {
50+
assertThat(ex.getMessage()).isEqualTo("'colorCode' must be between 0 and 255.");
51+
}
52+
}
53+
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.ansi;
18+
19+
import org.junit.jupiter.api.AfterEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
/**
25+
* Tests for {@link Ansi256PropertySource}.
26+
*
27+
* @author Toshiaki Maki
28+
*/
29+
class Ansi256PropertySourceTest {
30+
31+
private Ansi256PropertySource source = new Ansi256PropertySource("ansi256");
32+
33+
@AfterEach
34+
void reset() {
35+
AnsiOutput.setEnabled(AnsiOutput.Enabled.DETECT);
36+
}
37+
38+
@Test
39+
void getPropertyShouldConvertAnsi256ColorForeground() {
40+
AnsiOutput.setEnabled(AnsiOutput.Enabled.ALWAYS);
41+
final Object property = this.source.getProperty("Ansi256Color.Foreground_100");
42+
assertThat(property).isEqualTo("\033[38;5;100m");
43+
}
44+
45+
@Test
46+
void getPropertyShouldConvertAnsi256ColorBackground() {
47+
AnsiOutput.setEnabled(AnsiOutput.Enabled.ALWAYS);
48+
final Object property = this.source.getProperty("Ansi256Color.Background_100");
49+
assertThat(property).isEqualTo("\033[48;5;100m");
50+
}
51+
52+
@Test
53+
void getMissingPropertyShouldReturnNull() {
54+
AnsiOutput.setEnabled(AnsiOutput.Enabled.ALWAYS);
55+
final Object property = this.source.getProperty("Ansi256Color.ForeGround_100");
56+
assertThat(property).isNull();
57+
}
58+
59+
}

0 commit comments

Comments
 (0)