-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShellUtils.java
148 lines (134 loc) · 4.4 KB
/
ShellUtils.java
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
/* ==================================================================
* ShellUtils.java - 3/08/2020 11:41:27 AM
*
* Copyright 2020 SolarNetwork Foundation
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
* ==================================================================
*/
package org.snf.accounting.cli;
import java.util.Locale;
import java.util.Map;
import org.apache.sshd.server.Environment;
import org.davidmoten.text.utils.WordWrap;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import com.github.fonimus.ssh.shell.PromptColor;
import com.github.fonimus.ssh.shell.SshContext;
/**
* Utilities for the CLI shell.
*
* @author matt
* @version 1.0
*/
public class ShellUtils {
/** A default maximum width of shell output, i.e. for wrapping. */
public static final int SHELL_MAX_COLS = 80;
/**
* Color a bold message.
*
* @param message
* message to return
* @return colored message
*/
public static String getBold(String message) {
return new AttributedStringBuilder().append(message, AttributedStyle.BOLD).toAnsi();
}
/**
* Color a bold message with given color.
*
* @param message
* message to return
* @param color
* color to print
* @return colored message
*/
public static String getBoldColored(String message, PromptColor color) {
return new AttributedStringBuilder()
.append(message, AttributedStyle.BOLD.foreground(color.toJlineAttributedStyle())).toAnsi();
}
/**
* Color a faint message.
*
* @param message
* message to return
* @return colored message
*/
public static String getFaint(String message) {
return new AttributedStringBuilder().append(message, AttributedStyle.DEFAULT.faint()).toAnsi();
}
/**
* Broadcast a message to all registered SSH shells.
*
* @param message
* the message to broadcast
*/
public static void wall(String message) {
for (SshContext ctx : TrackingSshShellCommandFactory.sshContexts()) {
ctx.getTerminal().writer().println(message);
ctx.getTerminal().flush();
}
}
/**
* Wrap a string to a maximum character column width.
*
* @param message
* the message to wrap
* @param maxColumns
* the maximum number of characters wide to wrap the text at
* @return the message with newline characters inserted where needed to wrap the text to at most
* {@code maxColumns} characters wide
*/
public static String wrap(CharSequence message, int maxColumns) {
return WordWrap.from(message).maxWidth(maxColumns).wrap();
}
/**
* Wrap a string to the default character column width.
*
* @param message
* the message to wrap
* @return the message with newline characters inserted where needed to wrap the text to at most
* {@link #SHELL_MAX_COLS} characters wide
*/
public static String wrap(CharSequence message) {
return wrap(message, SHELL_MAX_COLS);
}
/**
* Get the locale of the client user.
*
* @return the locale, or {@literal null} if not known
*/
public static Locale clientLocale() {
Environment sshEnv = TrackingSshShellCommandFactory.sshEnvironment();
if (sshEnv != null) {
Map<String, String> env = sshEnv.getEnv();
if (env != null) {
String lang = env.get("LANG");
if (lang != null) {
// spit the likes of en_NZ.UTF-8
String[] components = lang.split("\\.");
String[] countryLang = components[0].split("_");
if (countryLang.length > 1) {
return new Locale(countryLang[0], countryLang[1]);
} else {
return new Locale(countryLang[0]);
}
}
}
}
return null;
}
}