forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBanner.java
More file actions
235 lines (213 loc) · 7.59 KB
/
Banner.java
File metadata and controls
235 lines (213 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package act.util;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* 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.
* #L%
*/
import act.Act;
import act.Zen;
import act.conf.AppConfigKey;
import act.conf.ConfLoader;
import act.internal.util.AppDescriptor;
import act.sys.Env;
import ascii.Image2ascii;
import com.github.lalyos.jfiglet.FigletFont;
import org.fusesource.jansi.Ansi;
import org.osgl.$;
import org.osgl.util.E;
import org.osgl.util.IO;
import org.osgl.util.S;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
* ASCII arts for Act
*/
public class Banner {
private static String cachedBanner;
public static void print(AppDescriptor appDescriptor) {
String banner = banner(appDescriptor);
System.out.println(banner);
cachedBanner = banner;
}
public static String cachedBanner() {
return cachedBanner;
}
public static String banner(AppDescriptor appDescriptor) {
String bannerText = null;
String udfBanner = udfBanner();
if (null != udfBanner) {
bannerText = S.concat(udfBanner, "\n");
}
if (null == bannerText) {
bannerText = asciiArt(appDescriptor.getAppName());
}
int bannerTextWidth = width(bannerText);
String favicon = favicon();
int faviconWidth = width(favicon);
int maxWidth = Math.max(faviconWidth, bannerTextWidth);
S.Buffer sb = S.buffer();
String actVersion = Act.VERSION.getVersion();
if ("ACTFRAMEWORK".equals(appDescriptor.getAppName())) {
sb.append(bannerText);
if (S.notBlank(favicon)) {
sb.append("\n");
addFavicon(sb, favicon, maxWidth, faviconWidth);
}
int n = actVersion.length();
int padLeft = (maxWidth - n + 1) / 2;
sb.append(S.times(" ", padLeft)).append(actVersion).append("\n");
} else {
sb.append(bannerText);
if (S.notBlank(favicon)) {
sb.append("\n");
addFavicon(sb, favicon, maxWidth, faviconWidth);
sb.append("\n");
sb.append(poweredBy(maxWidth, actVersion, true));
} else {
sb.append(poweredBy(maxWidth, actVersion, false));
}
sb.append("\n\n version: ").append(appDescriptor.getVersion().getVersion());
}
File aFile = new File("");
String group = Act.nodeGroup();
sb.append("\nscan pkg: ").append(System.getProperty(AppConfigKey.SCAN_PACKAGE.key()));
sb.append("\nbase dir: ").append(aFile.getAbsolutePath());
sb.append("\n pid: ").append(Env.PID.get());
sb.append("\n profile: ").append(ConfLoader.confSetName());
sb.append("\n mode: ").append(Act.mode());
if (S.notBlank(group)) {
sb.append("\n group: ").append(group);
}
sb.append("\n");
sb.append("\n zen: ").append(Zen.wordsOfTheDay());
sb.append("\n");
return sb.toString();
}
private static final String[] _BANNER_FONTS = {
"banner3", "big", "doom", "marquee",
"lcd", "mini", "slant", "small",
"speed", "standard", "starwars",
};
private static void addFavicon(S.Buffer buffer, String favicon, int maxWidth, int faviconWidth) {
if (S.blank(favicon)) {
return;
}
int delta = maxWidth - faviconWidth;
if (0 == delta) {
buffer.append(favicon).append("\n");
} else {
int padLeft = (delta + 1) / 2;
String[] lines = favicon.split("\n");
for (String line : lines) {
buffer.append(S.times(" ", padLeft)).append(line).append("\n");
}
}
}
private static String favicon() {
boolean isIcon = true;
URL url = Banner.class.getResource("/asset/favicon.png");
if (null == url) {
url = Banner.class.getResource("/asset/img/favicon.png");
if (null == url) {
url = Banner.class.getResource("/asset/image/favicon.png");
}
}
if (null != url) {
isIcon = false;
} else {
url = Banner.class.getResource("/asset/favicon.ico");
if (null == url) {
url = Banner.class.getResource("/asset/img/favicon.ico");
if (null == url) {
url = Banner.class.getResource("/asset/image/favicon.ico");
}
}
}
if (null == url) {
return "";
}
return removeEndingBlankLines(Image2ascii.render(url, true, isIcon));
}
private static String asciiArt(String s) {
String font = System.getProperty("banner.font");
if (null == font) {
int len = s.length();
if (len < 5) {
font = "big";
} else if (len < 7) {
font = "standard";
} else if (len < 10) {
font = "small";
} else {
font = "mini";
}
} else if ("BianLian".equals(font)) {
font = $.random(_BANNER_FONTS);
}
String path = font.endsWith(".flf") ? font : S.concat("/", font, ".flf");
File file = new File(path);
if (file.exists() && file.canRead()) {
try {
return FigletFont.convertOneLine(file, s.toUpperCase());
} catch (IOException e) {
throw E.ioException(e);
}
}
InputStream is = Banner.class.getResourceAsStream(path);
if (null == is) {
is = Banner.class.getResourceAsStream("/standard.flf");
}
try {
return FigletFont.convertOneLine(is, s.toUpperCase());
} catch (IOException e) {
throw E.ioException(e);
}
}
private static String removeEndingBlankLines(String text) {
int lastLineBreak = text.lastIndexOf("\n");
boolean lastLineIsBlank = (S.isBlank(text.substring(lastLineBreak, text.length())));
return lastLineIsBlank ? removeEndingBlankLines(text.substring(0, lastLineBreak)) : text;
}
private static int width(String banner) {
String[] lines = banner.split("\n");
int max = 0;
for (String s : lines) {
max = Math.max(max, s.length());
}
return max;
}
private static String udfBanner() {
URL url = Banner.class.getResource("/act_banner.txt");
return null == url ? null : IO.readContentAsString(url);
}
private static String poweredBy(int width, String actVersion, boolean center) {
String raw = S.concat("powered by @|bold ActFramework|@ ", actVersion);
String poweredBy = Ansi.ansi().render(raw).toString();
int pw = raw.length() - 9;
int gap = width - pw;
gap = Math.max(gap, 0);
if (gap == 0) {
return poweredBy;
}
if (center) {
gap = (gap + 1) / 2;
}
return S.concat(S.times(" ", gap), poweredBy);
}
}