Skip to content

Commit 7f79c26

Browse files
committed
Polish 'Add ANSI 8-bit color support'
See gh-18264
1 parent 65a27ef commit 7f79c26

File tree

12 files changed

+271
-303
lines changed

12 files changed

+271
-303
lines changed

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

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

31-
import org.springframework.boot.ansi.Ansi256PropertySource;
3231
import org.springframework.boot.ansi.AnsiPropertySource;
3332
import org.springframework.core.env.Environment;
3433
import org.springframework.core.env.MapPropertySource;
@@ -82,7 +81,6 @@ protected List<PropertyResolver> getPropertyResolvers(Environment environment, C
8281
resolvers.add(environment);
8382
resolvers.add(getVersionResolver(sourceClass));
8483
resolvers.add(getAnsiResolver());
85-
resolvers.add(getAnsi256Resolver());
8684
resolvers.add(getTitleResolver(sourceClass));
8785
return resolvers;
8886
}
@@ -126,12 +124,6 @@ private PropertyResolver getAnsiResolver() {
126124
return new PropertySourcesPropertyResolver(sources);
127125
}
128126

129-
private PropertyResolver getAnsi256Resolver() {
130-
MutablePropertySources sources = new MutablePropertySources();
131-
sources.addFirst(new Ansi256PropertySource("ansi256"));
132-
return new PropertySourcesPropertyResolver(sources);
133-
}
134-
135127
private PropertyResolver getTitleResolver(Class<?> sourceClass) {
136128
MutablePropertySources sources = new MutablePropertySources();
137129
String applicationTitle = getApplicationTitle(sourceClass);

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ansi/Ansi256Color.java

Lines changed: 0 additions & 84 deletions
This file was deleted.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ansi/Ansi256PropertySource.java

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.util.Assert;
20+
21+
/**
22+
* {@link AnsiElement} implementation for ANSI 8-bit foreground or background color codes.
23+
*
24+
* @author Toshiaki Maki
25+
* @author Phillip Webb
26+
* @since 2.2.0
27+
* @see #foreground(int)
28+
* @see #background(int)
29+
*/
30+
public final class Ansi8BitColor implements AnsiElement {
31+
32+
private final String prefix;
33+
34+
private final int code;
35+
36+
/**
37+
* Create a new {@link Ansi8BitColor} instance.
38+
* @param prefix the prefix escape chars
39+
* @param code color code (must be 0-255)
40+
* @throws IllegalArgumentException if color code is not between 0 and 255.
41+
*/
42+
private Ansi8BitColor(String prefix, int code) {
43+
Assert.isTrue(code >= 0 && code <= 255, "Code must be between 0 and 255");
44+
this.prefix = prefix;
45+
this.code = code;
46+
}
47+
48+
@Override
49+
public boolean equals(Object obj) {
50+
if (this == obj) {
51+
return true;
52+
}
53+
if (obj == null || getClass() != obj.getClass()) {
54+
return false;
55+
}
56+
Ansi8BitColor other = (Ansi8BitColor) obj;
57+
return this.prefix.equals(other.prefix) && this.code == other.code;
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return this.prefix.hashCode() * 31 + this.code;
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return this.prefix + this.code;
68+
}
69+
70+
/**
71+
* Return a foreground ANSI color code instance for the given code.
72+
* @param code the color code
73+
* @return an ANSI color code instance
74+
*/
75+
public static Ansi8BitColor foreground(int code) {
76+
return new Ansi8BitColor("38;5;", code);
77+
}
78+
79+
/**
80+
* Return a background ANSI color code instance for the given code.
81+
* @param code the color code
82+
* @return an ANSI color code instance
83+
*/
84+
public static Ansi8BitColor background(int code) {
85+
return new Ansi8BitColor("48;5;", code);
86+
}
87+
88+
}

0 commit comments

Comments
 (0)