12 changed files with 512 additions and 14 deletions
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
/* |
||||
* Copyright 2012-2015 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.ansi; |
||||
|
||||
/** |
||||
* {@link AnsiElement Ansi} background colors. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Geoffrey Chandler |
||||
* @since 1.3.0 |
||||
*/ |
||||
public enum AnsiBackground implements AnsiElement { |
||||
|
||||
DEFAULT("49"), |
||||
|
||||
BLACK("40"), |
||||
|
||||
RED("41"), |
||||
|
||||
GREEN("42"), |
||||
|
||||
YELLOW("43"), |
||||
|
||||
BLUE("44"), |
||||
|
||||
MAGENTA("45"), |
||||
|
||||
CYAN("46"), |
||||
|
||||
WHITE("47"), |
||||
|
||||
BRIGHT_BLACK("100"), |
||||
|
||||
BRIGHT_RED("101"), |
||||
|
||||
BRIGHT_GREEN("102"), |
||||
|
||||
BRIGHT_YELLOW("103"), |
||||
|
||||
BRIGHT_BLUE("104"), |
||||
|
||||
BRIGHT_MAGENTA("105"), |
||||
|
||||
BRIGHT_CYAN("106"), |
||||
|
||||
BRIGHT_WHITE("107"); |
||||
|
||||
private String code; |
||||
|
||||
private AnsiBackground(String code) { |
||||
this.code = code; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return this.code; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
/* |
||||
* Copyright 2012-2015 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.ansi; |
||||
|
||||
/** |
||||
* {@link AnsiElement Ansi} colors. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Geoffrey Chandler |
||||
* @since 1.3.0 |
||||
*/ |
||||
public enum AnsiColor implements AnsiElement { |
||||
|
||||
DEFAULT("39"), |
||||
|
||||
BLACK("30"), |
||||
|
||||
RED("31"), |
||||
|
||||
GREEN("32"), |
||||
|
||||
YELLOW("33"), |
||||
|
||||
BLUE("34"), |
||||
|
||||
MAGENTA("35"), |
||||
|
||||
CYAN("36"), |
||||
|
||||
WHITE("37"), |
||||
|
||||
BRIGHT_BLACK("90"), |
||||
|
||||
BRIGHT_RED("91"), |
||||
|
||||
BRIGHT_GREEN("92"), |
||||
|
||||
BRIGHT_YELLOW("93"), |
||||
|
||||
BRIGHT_BLUE("94"), |
||||
|
||||
BRIGHT_MAGENTA("95"), |
||||
|
||||
BRIGHT_CYAN("96"), |
||||
|
||||
BRIGHT_WHITE("97"); |
||||
|
||||
private final String code; |
||||
|
||||
private AnsiColor(String code) { |
||||
this.code = code; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return this.code; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,110 @@
@@ -0,0 +1,110 @@
|
||||
/* |
||||
* Copyright 2012-2015 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.ansi; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.EnumSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.springframework.core.env.PropertyResolver; |
||||
import org.springframework.core.env.PropertySource; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
/** |
||||
* {@link PropertyResolver} for {@link AnsiStyle}, {@link AnsiColor} and |
||||
* {@link AnsiBackground} elements. Supports properties of the form {@code AnsiStyle.BOLD} |
||||
* , {@code AnsiColor.RED} or {@code AnsiBackground.GREEN}. Also supports a prefix of |
||||
* {@code Ansi.} which is an aggregation of everything (with background colors prefixed |
||||
* {@code BG_}). |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 1.3.0 |
||||
*/ |
||||
public class AnsiPropertySource extends PropertySource<AnsiElement> { |
||||
|
||||
private static final Iterable<MappedEnum<?>> MAPPED_ENUMS; |
||||
static { |
||||
List<MappedEnum<?>> enums = new ArrayList<MappedEnum<?>>(); |
||||
enums.add(new MappedEnum<AnsiStyle>("AnsiStyle.", AnsiStyle.class)); |
||||
enums.add(new MappedEnum<AnsiColor>("AnsiColor.", AnsiColor.class)); |
||||
enums.add(new MappedEnum<AnsiBackground>("AnsiBackground.", AnsiBackground.class)); |
||||
enums.add(new MappedEnum<AnsiStyle>("Ansi.", AnsiStyle.class)); |
||||
enums.add(new MappedEnum<AnsiColor>("Ansi.", AnsiColor.class)); |
||||
enums.add(new MappedEnum<AnsiBackground>("Ansi.BG_", AnsiBackground.class)); |
||||
MAPPED_ENUMS = Collections.unmodifiableList(enums); |
||||
} |
||||
|
||||
private final boolean encode; |
||||
|
||||
/** |
||||
* Create a new {@link AnsiPropertySource} instance. |
||||
* @param name the name of the property source |
||||
* @param encode if the output should be encoded |
||||
*/ |
||||
public AnsiPropertySource(String name, boolean encode) { |
||||
super(name); |
||||
this.encode = encode; |
||||
} |
||||
|
||||
@Override |
||||
public Object getProperty(String name) { |
||||
if (StringUtils.hasLength(name)) { |
||||
for (MappedEnum<?> mappedEnum : MAPPED_ENUMS) { |
||||
if (name.startsWith(mappedEnum.getPrefix())) { |
||||
String enumName = name.substring(mappedEnum.getPrefix().length()); |
||||
for (Enum<?> ansiEnum : mappedEnum.getEnums()) { |
||||
if (ansiEnum.name().equals(enumName)) { |
||||
if (this.encode) { |
||||
return AnsiOutput.encode((AnsiElement) ansiEnum); |
||||
} |
||||
return ansiEnum; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* Mapping between an enum and the pseudo property source. |
||||
*/ |
||||
private static class MappedEnum<E extends Enum<E>> { |
||||
|
||||
private final String prefix; |
||||
|
||||
private final Set<E> enums; |
||||
|
||||
public MappedEnum(String prefix, Class<E> enumType) { |
||||
this.prefix = prefix; |
||||
this.enums = EnumSet.allOf(enumType); |
||||
|
||||
} |
||||
|
||||
public String getPrefix() { |
||||
return this.prefix; |
||||
} |
||||
|
||||
public Set<E> getEnums() { |
||||
return this.enums; |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
/* |
||||
* Copyright 2012-2015 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.ansi; |
||||
|
||||
/** |
||||
* {@link AnsiElement Ansi} styles. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 1.3.0 |
||||
*/ |
||||
public enum AnsiStyle implements AnsiElement { |
||||
|
||||
NORMAL("0"), |
||||
|
||||
BOLD("1"), |
||||
|
||||
FAINT("2"), |
||||
|
||||
ITALIC("3"), |
||||
|
||||
UNDERLINE("4"); |
||||
|
||||
private final String code; |
||||
|
||||
private AnsiStyle(String code) { |
||||
this.code = code; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return this.code; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,86 @@
@@ -0,0 +1,86 @@
|
||||
/* |
||||
* Copyright 2012-2015 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.ansi; |
||||
|
||||
import org.junit.After; |
||||
import org.junit.Test; |
||||
import org.springframework.boot.ansi.AnsiOutput.Enabled; |
||||
|
||||
import static org.hamcrest.Matchers.equalTo; |
||||
import static org.hamcrest.Matchers.nullValue; |
||||
import static org.junit.Assert.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link AnsiPropertySource}. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
public class AnsiPropertySourceTests { |
||||
|
||||
private AnsiPropertySource source = new AnsiPropertySource("ansi", false); |
||||
|
||||
@After |
||||
public void reset() { |
||||
AnsiOutput.setEnabled(Enabled.DETECT); |
||||
} |
||||
|
||||
@Test |
||||
public void getAnsiStyle() throws Exception { |
||||
assertThat(this.source.getProperty("AnsiStyle.BOLD"), |
||||
equalTo((Object) AnsiStyle.BOLD)); |
||||
} |
||||
|
||||
@Test |
||||
public void getAnsiColor() throws Exception { |
||||
assertThat(this.source.getProperty("AnsiColor.RED"), |
||||
equalTo((Object) AnsiColor.RED)); |
||||
} |
||||
|
||||
@Test |
||||
public void getAnsiBackground() throws Exception { |
||||
assertThat(this.source.getProperty("AnsiBackground.GREEN"), |
||||
equalTo((Object) AnsiBackground.GREEN)); |
||||
} |
||||
|
||||
@Test |
||||
public void getAnsi() throws Exception { |
||||
assertThat(this.source.getProperty("Ansi.BOLD"), equalTo((Object) AnsiStyle.BOLD)); |
||||
assertThat(this.source.getProperty("Ansi.RED"), equalTo((Object) AnsiColor.RED)); |
||||
assertThat(this.source.getProperty("Ansi.BG_RED"), |
||||
equalTo((Object) AnsiBackground.RED)); |
||||
} |
||||
|
||||
@Test |
||||
public void getMissing() throws Exception { |
||||
assertThat(this.source.getProperty("AnsiStyle.NOPE"), nullValue()); |
||||
} |
||||
|
||||
@Test |
||||
public void encodeEnabled() throws Exception { |
||||
AnsiOutput.setEnabled(Enabled.ALWAYS); |
||||
AnsiPropertySource source = new AnsiPropertySource("ansi", true); |
||||
assertThat(source.getProperty("Ansi.RED"), equalTo((Object) "\033[31m")); |
||||
} |
||||
|
||||
@Test |
||||
public void encodeDisabled() throws Exception { |
||||
AnsiOutput.setEnabled(Enabled.NEVER); |
||||
AnsiPropertySource source = new AnsiPropertySource("ansi", true); |
||||
assertThat(source.getProperty("Ansi.RED"), equalTo((Object) "")); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue