1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.jline;
20
21 import org.apache.maven.api.services.MessageBuilder;
22 import org.apache.maven.api.services.MessageBuilderFactory;
23 import org.jline.jansi.AnsiConsole;
24 import org.jline.reader.LineReader;
25 import org.jline.reader.LineReaderBuilder;
26 import org.jline.terminal.Terminal;
27 import org.jline.terminal.TerminalBuilder;
28
29 public class MessageUtils {
30
31 static Terminal terminal;
32 static LineReader reader;
33 static MessageBuilderFactory messageBuilderFactory = new JLineMessageBuilderFactory();
34 static boolean colorEnabled = true;
35 static Thread shutdownHook;
36 static final Object STARTUP_SHUTDOWN_MONITOR = new Object();
37
38 public static void systemInstall(Terminal terminal) {
39 MessageUtils.terminal = terminal;
40 MessageUtils.reader = createReader(terminal);
41 }
42
43 public static void systemInstall() {
44 MessageUtils.terminal = new FastTerminal(
45 () -> TerminalBuilder.builder().name("Maven").dumb(true).build(), terminal -> {
46 MessageUtils.reader = createReader(terminal);
47 AnsiConsole.setTerminal(terminal);
48 AnsiConsole.systemInstall();
49 });
50 }
51
52 private static LineReader createReader(Terminal terminal) {
53 return LineReaderBuilder.builder().terminal(terminal).build();
54 }
55
56 public static void registerShutdownHook() {
57 if (shutdownHook == null) {
58 shutdownHook = new Thread(() -> {
59 synchronized (MessageUtils.STARTUP_SHUTDOWN_MONITOR) {
60 MessageUtils.doSystemUninstall();
61 }
62 });
63 Runtime.getRuntime().addShutdownHook(shutdownHook);
64 }
65 }
66
67 public static void systemUninstall() {
68 doSystemUninstall();
69 if (shutdownHook != null) {
70 try {
71 Runtime.getRuntime().removeShutdownHook(shutdownHook);
72 } catch (IllegalStateException var3) {
73
74 }
75 }
76 }
77
78 private static void doSystemUninstall() {
79 try {
80 AnsiConsole.systemUninstall();
81 } finally {
82 terminal = null;
83 }
84 }
85
86 public static void setColorEnabled(boolean enabled) {
87 colorEnabled = enabled;
88 }
89
90 public static boolean isColorEnabled() {
91 return colorEnabled && terminal != null;
92 }
93
94 public static int getTerminalWidth() {
95 return terminal != null ? terminal.getWidth() : -1;
96 }
97
98 public static MessageBuilder builder() {
99 return messageBuilderFactory.builder();
100 }
101 }