View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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                  // ignore
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 }