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 java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.io.PrintWriter;
25  import java.nio.charset.Charset;
26  import java.util.concurrent.Callable;
27  import java.util.concurrent.ExecutorService;
28  import java.util.concurrent.Executors;
29  import java.util.concurrent.Future;
30  import java.util.function.Consumer;
31  import java.util.function.IntConsumer;
32  import java.util.function.IntSupplier;
33  
34  import org.apache.maven.api.services.MavenException;
35  import org.jline.terminal.Attributes;
36  import org.jline.terminal.Cursor;
37  import org.jline.terminal.MouseEvent;
38  import org.jline.terminal.Size;
39  import org.jline.terminal.Terminal;
40  import org.jline.terminal.spi.SystemStream;
41  import org.jline.terminal.spi.TerminalExt;
42  import org.jline.terminal.spi.TerminalProvider;
43  import org.jline.utils.ColorPalette;
44  import org.jline.utils.InfoCmp;
45  import org.jline.utils.NonBlockingReader;
46  
47  public class FastTerminal implements TerminalExt {
48  
49      final Future<Terminal> terminal;
50  
51      public FastTerminal(Callable<Terminal> builder, Consumer<Terminal> consumer) {
52          ExecutorService executor = Executors.newSingleThreadExecutor();
53          terminal = executor.submit(() -> {
54              try {
55                  Terminal terminal = builder.call();
56                  consumer.accept(terminal);
57                  return terminal;
58              } catch (Exception e) {
59                  throw new MavenException(e);
60              } finally {
61                  executor.shutdown();
62              }
63          });
64      }
65  
66      public TerminalExt getTerminal() {
67          try {
68              return (TerminalExt) terminal.get();
69          } catch (Exception e) {
70              throw new RuntimeException(e);
71          }
72      }
73  
74      @Override
75      public String getName() {
76          return getTerminal().getName();
77      }
78  
79      @Override
80      public SignalHandler handle(Signal signal, SignalHandler signalHandler) {
81          return getTerminal().handle(signal, signalHandler);
82      }
83  
84      @Override
85      public void raise(Signal signal) {
86          getTerminal().raise(signal);
87      }
88  
89      @Override
90      public NonBlockingReader reader() {
91          return getTerminal().reader();
92      }
93  
94      @Override
95      public PrintWriter writer() {
96          return getTerminal().writer();
97      }
98  
99      @Override
100     public Charset encoding() {
101         return getTerminal().encoding();
102     }
103 
104     @Override
105     public InputStream input() {
106         return getTerminal().input();
107     }
108 
109     @Override
110     public OutputStream output() {
111         return getTerminal().output();
112     }
113 
114     @Override
115     public boolean canPauseResume() {
116         return getTerminal().canPauseResume();
117     }
118 
119     @Override
120     public void pause() {
121         getTerminal().pause();
122     }
123 
124     @Override
125     public void pause(boolean b) throws InterruptedException {
126         getTerminal().pause(b);
127     }
128 
129     @Override
130     public void resume() {
131         getTerminal().resume();
132     }
133 
134     @Override
135     public boolean paused() {
136         return getTerminal().paused();
137     }
138 
139     @Override
140     public Attributes enterRawMode() {
141         return getTerminal().enterRawMode();
142     }
143 
144     @Override
145     public boolean echo() {
146         return getTerminal().echo();
147     }
148 
149     @Override
150     public boolean echo(boolean b) {
151         return getTerminal().echo(b);
152     }
153 
154     @Override
155     public Attributes getAttributes() {
156         return getTerminal().getAttributes();
157     }
158 
159     @Override
160     public void setAttributes(Attributes attributes) {
161         getTerminal().setAttributes(attributes);
162     }
163 
164     @Override
165     public Size getSize() {
166         return getTerminal().getSize();
167     }
168 
169     @Override
170     public void setSize(Size size) {
171         getTerminal().setSize(size);
172     }
173 
174     @Override
175     public int getWidth() {
176         return getTerminal().getWidth();
177     }
178 
179     @Override
180     public int getHeight() {
181         return getTerminal().getHeight();
182     }
183 
184     @Override
185     public Size getBufferSize() {
186         return getTerminal().getBufferSize();
187     }
188 
189     @Override
190     public void flush() {
191         getTerminal().flush();
192     }
193 
194     @Override
195     public String getType() {
196         return getTerminal().getType();
197     }
198 
199     @Override
200     public boolean puts(InfoCmp.Capability capability, Object... objects) {
201         return getTerminal().puts(capability, objects);
202     }
203 
204     @Override
205     public boolean getBooleanCapability(InfoCmp.Capability capability) {
206         return getTerminal().getBooleanCapability(capability);
207     }
208 
209     @Override
210     public Integer getNumericCapability(InfoCmp.Capability capability) {
211         return getTerminal().getNumericCapability(capability);
212     }
213 
214     @Override
215     public String getStringCapability(InfoCmp.Capability capability) {
216         return getTerminal().getStringCapability(capability);
217     }
218 
219     @Override
220     public Cursor getCursorPosition(IntConsumer intConsumer) {
221         return getTerminal().getCursorPosition(intConsumer);
222     }
223 
224     @Override
225     public boolean hasMouseSupport() {
226         return getTerminal().hasMouseSupport();
227     }
228 
229     @Override
230     public boolean trackMouse(MouseTracking mouseTracking) {
231         return getTerminal().trackMouse(mouseTracking);
232     }
233 
234     @Override
235     public MouseEvent readMouseEvent() {
236         return getTerminal().readMouseEvent();
237     }
238 
239     @Override
240     public MouseEvent readMouseEvent(IntSupplier intSupplier) {
241         return getTerminal().readMouseEvent(intSupplier);
242     }
243 
244     @Override
245     public boolean hasFocusSupport() {
246         return getTerminal().hasFocusSupport();
247     }
248 
249     @Override
250     public boolean trackFocus(boolean b) {
251         return getTerminal().trackFocus(b);
252     }
253 
254     @Override
255     public ColorPalette getPalette() {
256         return getTerminal().getPalette();
257     }
258 
259     @Override
260     public void close() throws IOException {
261         getTerminal().close();
262     }
263 
264     @Override
265     public TerminalProvider getProvider() {
266         return getTerminal().getProvider();
267     }
268 
269     @Override
270     public SystemStream getSystemStream() {
271         return getTerminal().getSystemStream();
272     }
273 }