leonard.se
2022-03-01 c6e76869f5a02cd52b8f4f9c7d78c0b433b22997
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
package za.co.taulite.tools.smppclient;
 
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.springframework.beans.factory.annotation.Value;
 
public class ShellHelper {
 
    @Value("${shell.out.info}")
    public String infoColor;
 
    @Value("${shell.out.success}")
    public String successColor;
 
    @Value("${shell.out.warning}")
    public String warningColor;
 
    @Value("${shell.out.error}")
    public String errorColor;
 
    private Terminal terminal;
 
    public ShellHelper(Terminal terminal) {
        this.terminal = terminal;
    }
 
    public String getColored(String message, PromptColor color) {
        return (new AttributedStringBuilder()).append(message, AttributedStyle.DEFAULT.foreground(color.toJlineAttributedStyle())).toAnsi();
    }
 
    public String getInfoMessage(String message) {
        return getColored(message, PromptColor.valueOf(infoColor));
    }
 
    public String getSuccessMessage(String message) {
        return getColored(message, PromptColor.valueOf(successColor));
    }
 
    public String getWarningMessage(String message) {
        return getColored(message, PromptColor.valueOf(warningColor));
    }
 
    public String getErrorMessage(String message) {
        return getColored(message, PromptColor.valueOf(errorColor));
    }
 
    /**
     * Print message to the console in the default color.
     *
     * @param message message to print
     */
    public void print(String message) {
        print(message, null);
    }
 
    /**
     * Print message to the console in the success color.
     *
     * @param message message to print
     */
    public void printSuccess(String message) {
        print(message, PromptColor.valueOf(successColor));
    }
 
    /**
     * Print message to the console in the info color.
     *
     * @param message message to print
     */
    public void printInfo(String message) {
        print(message, PromptColor.valueOf(infoColor));
    }
 
    /**
     * Print message to the console in the warning color.
     *
     * @param message message to print
     */
    public void printWarning(String message) {
        print(message, PromptColor.valueOf(warningColor));
    }
 
    /**
     * Print message to the console in the error color.
     *
     * @param message message to print
     */
    public void printError(String message) {
        print(message, PromptColor.valueOf(errorColor));
    }
 
    /**
     * Generic Print to the console method.
     *
     * @param message message to print
     * @param color   (optional) prompt color
     */
    public void print(String message, PromptColor color) {
        String toPrint = message;
        if (color != null) {
            toPrint = getColored(message, color);
        }
        terminal.writer().println(toPrint);
        terminal.flush();
    }
}