Skip to content

Commit 7e3cc89

Browse files
committed
[Optimize] Convert Ansi and Font from enum to class
1 parent 1dd361b commit 7e3cc89

File tree

10 files changed

+1124
-512
lines changed

10 files changed

+1124
-512
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,3 @@ nbdist/
2525
### custom ###
2626
logs
2727
*.log
28-
test

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
A FIGlet utility for Java.
44

55
## What is FIGlet
6-
7-
[FIGlet](https://en.wikipedia.org/wiki/FIGlet) is a computer program that generates text banners, in a variety of typefaces, composed of letters made up of conglomerations of smaller
8-
ASCII characters (see [ASCII art](https://en.wikipedia.org/wiki/ASCII_art)). The name derives from "Frank, Ian and Glenn's letters".
96

10-
Being free software, FIGlet is commonly included as part of many Unix-like operating systems (Linux, BSD, etc.) distributions, but it has been ported to other platforms as well. The official FIGlet FTP site includes precompiled ports for the Acorn, Amiga, Apple II, Atari ST, BeOS, Macintosh, MS-DOS, NeXTSTEP, OS/2, and Windows platforms, as well as a reimplementation in Perl (Text::FIGlet). There are third-party reimplementations of FIGlet in Java (including one embedded in the JavE ASCII art editor), JavaScript, PHP and Python. FIGlet was featured as a Debian Package of the Day in 2007.
7+
[FIGlet](https://en.wikipedia.org/wiki/FIGlet) is a computer program that generates text banners, in a variety of typefaces, composed of letters made up of conglomerations of smaller ASCII characters (see [ASCII art](https://en.wikipedia.org/wiki/ASCII_art)).
8+
The name derives from "Frank, Ian and Glenn's letters".
9+
10+
Being free software, FIGlet is commonly included as part of many Unix-like operating systems (Linux, BSD, etc.) distributions, but it has been ported to other platforms as well.
11+
The official FIGlet FTP site includes precompiled ports for the Acorn, Amiga, Apple II, Atari ST, BeOS, Macintosh, MS-DOS, NeXTSTEP, OS/2, and Windows platforms, as well as a reimplementation in Perl (Text::FIGlet).
12+
There are third-party reimplementations of FIGlet in Java (including one embedded in the JavE ASCII art editor), JavaScript, PHP and Python. FIGlet was featured as a Debian Package of the Day in 2007.
1113

1214
## Dependency
1315

@@ -29,7 +31,7 @@ implementation 'io.leego:banana:2.0.1'
2931

3032
## Usage
3133

32-
### Example
34+
### Example
3335

3436
```java
3537
BananaUtils.bananaify("Hello, Banana!");
@@ -205,4 +207,5 @@ BananaUtils.bananansi("Hello, World!", Ansi.RED, Ansi.BG_YELLOW);
205207
> * Bugs & Issues: [Click Here](https://github.com/yihleego/banana/issues)
206208
207209
## License
210+
208211
Banana is under the Apache 2.0 license. See the [LICENSE](LICENSE.txt) file for details.

docs/FONTS.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,7 @@ BananaUtils.bananaify("Hello, World!", Font.FOUR_TOPS);
18031803
Fraktur
18041804

18051805
```java
1806-
BananaUtils.bananaify("Hello, World!",Font.FRAKTUR);
1806+
BananaUtils.bananaify("Hello, World!", Font.FRAKTUR);
18071807
```
18081808

18091809
```
@@ -3178,7 +3178,7 @@ BananaUtils.bananaify("Hello, World!", Font.PEPPER);
31783178
Poison
31793179

31803180
```java
3181-
BananaUtils.bananaify("Hello, World!",Font.POISON);
3181+
BananaUtils.bananaify("Hello, World!", Font.POISON);
31823182
```
31833183

31843184
```
@@ -3653,7 +3653,7 @@ BananaUtils.bananaify("Hello, World!", Font.SMALL_KEYBOARD);
36533653
Small Poison
36543654

36553655
```java
3656-
BananaUtils.bananaify("Hello, World!",Font.SMALL_POISON);
3656+
BananaUtils.bananaify("Hello, World!", Font.SMALL_POISON);
36573657
```
36583658

36593659
```

pom.xml

+26-10
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,15 @@
5656
<java.version>1.7</java.version>
5757
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5858
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
59-
<maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
59+
<maven-resources-plugin.version>3.2.0</maven-resources-plugin.version>
60+
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
6061
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
6162
<maven-javadoc-plugin.version>3.2.0</maven-javadoc-plugin.version>
6263
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
6364
<nexus-staging-maven-plugin.version>1.6.8</nexus-staging-maven-plugin.version>
65+
<junit.version>4.13.1</junit.version>
6466
</properties>
6567

66-
<dependencies>
67-
<dependency>
68-
<groupId>junit</groupId>
69-
<artifactId>junit</artifactId>
70-
<version>4.13.1</version>
71-
<scope>test</scope>
72-
</dependency>
73-
</dependencies>
74-
7568
<profiles>
7669
<profile>
7770
<id>dev</id>
@@ -98,11 +91,17 @@
9891
<configuration>
9992
<nonFilteredFileExtensions>
10093
<nonFilteredFileExtension>flf</nonFilteredFileExtension>
94+
<nonFilteredFileExtension>tlf</nonFilteredFileExtension>
10195
<nonFilteredFileExtension>aol</nonFilteredFileExtension>
10296
</nonFilteredFileExtensions>
10397
<encoding>${project.build.sourceEncoding}</encoding>
10498
</configuration>
10599
</plugin>
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-surefire-plugin</artifactId>
103+
<version>${maven-surefire-plugin.version}</version>
104+
</plugin>
106105
</plugins>
107106
</build>
108107
</profile>
@@ -173,6 +172,14 @@
173172
</execution>
174173
</executions>
175174
</plugin>
175+
<plugin>
176+
<groupId>org.apache.maven.plugins</groupId>
177+
<artifactId>maven-surefire-plugin</artifactId>
178+
<version>${maven-surefire-plugin.version}</version>
179+
<configuration>
180+
<skip>true</skip>
181+
</configuration>
182+
</plugin>
176183
<plugin>
177184
<groupId>org.sonatype.plugins</groupId>
178185
<artifactId>nexus-staging-maven-plugin</artifactId>
@@ -189,4 +196,13 @@
189196
</profile>
190197
</profiles>
191198

199+
<dependencies>
200+
<dependency>
201+
<groupId>junit</groupId>
202+
<artifactId>junit</artifactId>
203+
<version>${junit.version}</version>
204+
<scope>test</scope>
205+
</dependency>
206+
</dependencies>
207+
192208
</project>
+95-35
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,89 @@
11
package io.leego.banana;
22

3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
39
/**
410
* @author Yihleego
511
*/
6-
public enum Ansi {
7-
/** Font-Colors. */
8-
BLACK("30"),
9-
RED("31"),
10-
GREEN("32"),
11-
YELLOW("33"),
12-
BLUE("34"),
13-
PURPLE("35"),
14-
CYAN("36"),
15-
WHITE("37"),
16-
/** Background-Colors. */
17-
BG_BLACK("40"),
18-
BG_RED("41"),
19-
BG_GREEN("42"),
20-
BG_YELLOW("43"),
21-
BG_BLUE("44"),
22-
BG_PURPLE("45"),
23-
BG_CYAN("46"),
24-
BG_WHITE("47"),
25-
/** Others. */
26-
NORMAL("0"),
27-
BOLD("1"),
28-
FAINT("2"),
29-
ITALIC("3"),
30-
UNDERLINE("4"),
31-
SLOW_BLINK("5"),
32-
RAPID_BLINK("6"),
33-
REVERSE_VIDEO("7"),
34-
CONCEAL("8"),
35-
CROSSED_OUT("9"),
36-
PRIMARY("10");
12+
public class Ansi {
13+
/** Ansi-Colors */
14+
public static final Ansi BLACK;
15+
public static final Ansi RED;
16+
public static final Ansi GREEN;
17+
public static final Ansi YELLOW;
18+
public static final Ansi BLUE;
19+
public static final Ansi PURPLE;
20+
public static final Ansi CYAN;
21+
public static final Ansi WHITE;
22+
/** Background-Colors */
23+
public static final Ansi BG_BLACK;
24+
public static final Ansi BG_RED;
25+
public static final Ansi BG_GREEN;
26+
public static final Ansi BG_YELLOW;
27+
public static final Ansi BG_BLUE;
28+
public static final Ansi BG_PURPLE;
29+
public static final Ansi BG_CYAN;
30+
public static final Ansi BG_WHITE;
31+
/** Others */
32+
public static final Ansi NORMAL;
33+
public static final Ansi BOLD;
34+
public static final Ansi FAINT;
35+
public static final Ansi ITALIC;
36+
public static final Ansi UNDERLINE;
37+
public static final Ansi SLOW_BLINK;
38+
public static final Ansi RAPID_BLINK;
39+
public static final Ansi REVERSE_VIDEO;
40+
public static final Ansi CONCEAL;
41+
public static final Ansi CROSSED_OUT;
42+
public static final Ansi PRIMARY;
43+
44+
private static final List<Ansi> VALUES;
45+
private static final Map<String, Ansi> MAP;
46+
47+
static {
48+
List<Ansi> values = new ArrayList<>();
49+
values.add(BLACK = new Ansi("30"));
50+
values.add(RED = new Ansi("31"));
51+
values.add(GREEN = new Ansi("32"));
52+
values.add(YELLOW = new Ansi("33"));
53+
values.add(BLUE = new Ansi("34"));
54+
values.add(PURPLE = new Ansi("35"));
55+
values.add(CYAN = new Ansi("36"));
56+
values.add(WHITE = new Ansi("37"));
57+
values.add(BG_BLACK = new Ansi("40"));
58+
values.add(BG_RED = new Ansi("41"));
59+
values.add(BG_GREEN = new Ansi("42"));
60+
values.add(BG_YELLOW = new Ansi("43"));
61+
values.add(BG_BLUE = new Ansi("44"));
62+
values.add(BG_PURPLE = new Ansi("45"));
63+
values.add(BG_CYAN = new Ansi("46"));
64+
values.add(BG_WHITE = new Ansi("47"));
65+
values.add(NORMAL = new Ansi("0"));
66+
values.add(BOLD = new Ansi("1"));
67+
values.add(FAINT = new Ansi("2"));
68+
values.add(ITALIC = new Ansi("3"));
69+
values.add(UNDERLINE = new Ansi("4"));
70+
values.add(SLOW_BLINK = new Ansi("5"));
71+
values.add(RAPID_BLINK = new Ansi("6"));
72+
values.add(REVERSE_VIDEO = new Ansi("7"));
73+
values.add(CONCEAL = new Ansi("8"));
74+
values.add(CROSSED_OUT = new Ansi("9"));
75+
values.add(PRIMARY = new Ansi("10"));
76+
Map<String, Ansi> map = new HashMap<>(values.size());
77+
for (Ansi v : values) {
78+
map.put(v.code, v);
79+
}
80+
VALUES = Collections.unmodifiableList(values);
81+
MAP = Collections.unmodifiableMap(map);
82+
}
3783

38-
private final String code;
84+
protected final String code;
3985

40-
Ansi(String code) {
86+
protected Ansi(String code) {
4187
this.code = code;
4288
}
4389

@@ -50,14 +96,14 @@ public String getAnsi() {
5096
}
5197

5298
public static String ansify(String text, Ansi style) {
53-
if (text == null || "".equals(text) || style == null) {
99+
if (text == null || text.isEmpty() || style == null) {
54100
return text;
55101
}
56102
return style.getAnsi() + text + NORMAL.getAnsi();
57103
}
58104

59105
public static String ansify(String text, Ansi... styles) {
60-
if (text == null || "".equals(text) || styles == null || styles.length == 0) {
106+
if (text == null || text.isEmpty() || styles == null || styles.length == 0) {
61107
return text;
62108
}
63109
int count = 0;
@@ -75,4 +121,18 @@ public static String ansify(String text, Ansi... styles) {
75121
sb.deleteCharAt(sb.length() - 1);
76122
return sb.append("m").append(text).append(NORMAL.getAnsi()).toString();
77123
}
124+
125+
public static List<Ansi> values() {
126+
return VALUES;
127+
}
128+
129+
public static Ansi get(String code) {
130+
return MAP.get(code);
131+
}
132+
133+
public static Ansi getOrDefault(String code, Ansi defaultValue) {
134+
Ansi ansi;
135+
return (ansi = MAP.get(code)) != null ? ansi : defaultValue;
136+
}
137+
78138
}

0 commit comments

Comments
 (0)