author | wetmore |
Thu, 21 Sep 2017 12:13:25 -0700 | |
changeset 47238 | 2557ac47e731 |
parent 47216 | 71c04702a3d5 |
child 50338 | 1d5694c1aa03 |
permissions | -rw-r--r-- |
31530
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
1 |
/* |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
2 |
* Copyright (c) 2002-2012, the original author or authors. |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
3 |
* |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
4 |
* This software is distributable under the BSD license. See the terms of the |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
5 |
* BSD license in the documentation provided with this software. |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
6 |
* |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
7 |
* http://www.opensource.org/licenses/bsd-license.php |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
8 |
*/ |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
9 |
package jdk.internal.jline; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
10 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
11 |
import java.io.IOException; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
12 |
import java.io.InputStream; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
13 |
import java.io.OutputStream; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
14 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
15 |
import jdk.internal.jline.internal.Log; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
16 |
import jdk.internal.jline.internal.ShutdownHooks; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
17 |
import jdk.internal.jline.internal.ShutdownHooks.Task; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
18 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
19 |
/** |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
20 |
* Provides support for {@link Terminal} instances. |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
21 |
* |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
22 |
* @author <a href="mailto:jason@planet57.com">Jason Dillon</a> |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
23 |
* @since 2.0 |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
24 |
*/ |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
25 |
public abstract class TerminalSupport |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
26 |
implements Terminal |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
27 |
{ |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
28 |
public static final int DEFAULT_WIDTH = 80; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
29 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
30 |
public static final int DEFAULT_HEIGHT = 24; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
31 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
32 |
private Task shutdownTask; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
33 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
34 |
private boolean supported; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
35 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
36 |
private boolean echoEnabled; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
37 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
38 |
private boolean ansiSupported; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
39 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
40 |
protected TerminalSupport(final boolean supported) { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
41 |
this.supported = supported; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
42 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
43 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
44 |
public void init() throws Exception { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
45 |
if (shutdownTask != null) { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
46 |
ShutdownHooks.remove(shutdownTask); |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
47 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
48 |
// Register a task to restore the terminal on shutdown |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
49 |
this.shutdownTask = ShutdownHooks.add(new Task() |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
50 |
{ |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
51 |
public void run() throws Exception { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
52 |
restore(); |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
53 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
54 |
}); |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
55 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
56 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
57 |
public void restore() throws Exception { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
58 |
TerminalFactory.resetIf(this); |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
59 |
if (shutdownTask != null) { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
60 |
ShutdownHooks.remove(shutdownTask); |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
61 |
shutdownTask = null; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
62 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
63 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
64 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
65 |
public void reset() throws Exception { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
66 |
restore(); |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
67 |
init(); |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
68 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
69 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
70 |
public final boolean isSupported() { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
71 |
return supported; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
72 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
73 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
74 |
public synchronized boolean isAnsiSupported() { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
75 |
return ansiSupported; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
76 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
77 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
78 |
protected synchronized void setAnsiSupported(final boolean supported) { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
79 |
this.ansiSupported = supported; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
80 |
Log.debug("Ansi supported: ", supported); |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
81 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
82 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
83 |
/** |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
84 |
* Subclass to change behavior if needed. |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
85 |
* @return the passed out |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
86 |
*/ |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
87 |
public OutputStream wrapOutIfNeeded(OutputStream out) { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
88 |
return out; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
89 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
90 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
91 |
/** |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
92 |
* Defaults to true which was the behaviour before this method was added. |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
93 |
*/ |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
94 |
public boolean hasWeirdWrap() { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
95 |
return true; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
96 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
97 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
98 |
public int getWidth() { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
99 |
return DEFAULT_WIDTH; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
100 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
101 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
102 |
public int getHeight() { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
103 |
return DEFAULT_HEIGHT; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
104 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
105 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
106 |
public synchronized boolean isEchoEnabled() { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
107 |
return echoEnabled; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
108 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
109 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
110 |
public synchronized void setEchoEnabled(final boolean enabled) { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
111 |
this.echoEnabled = enabled; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
112 |
Log.debug("Echo enabled: ", enabled); |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
113 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
114 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
115 |
public InputStream wrapInIfNeeded(InputStream in) throws IOException { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
116 |
return in; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
117 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
118 |
|
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
119 |
public String getOutputEncoding() { |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
120 |
// null for unknown |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
121 |
return null; |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
122 |
} |
aa87d96a02b1
8080679: Include jline in JDK for Java and JavaScript REPLs
jlahoda
parents:
diff
changeset
|
123 |
} |