8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.tools.jjs;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.PrintStream;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.Iterator;
36 import java.util.List;
37 import jdk.internal.jline.NoInterruptUnixTerminal;
38 import jdk.internal.jline.Terminal;
39 import jdk.internal.jline.TerminalFactory;
40 import jdk.internal.jline.TerminalFactory.Flavor;
41 import jdk.internal.jline.WindowsTerminal;
42 import jdk.internal.jline.console.ConsoleReader;
43 import jdk.internal.jline.console.completer.Completer;
44 import jdk.internal.jline.console.history.FileHistory;
45
46 class Console implements AutoCloseable {
47 private final ConsoleReader in;
48 private final FileHistory history;
49
50 Console(final InputStream cmdin, final PrintStream cmdout, final File historyFile,
51 final Completer completer) throws IOException {
52 TerminalFactory.registerFlavor(Flavor.WINDOWS, isCygwin()? JJSUnixTerminal::new : JJSWindowsTerminal::new);
53 TerminalFactory.registerFlavor(Flavor.UNIX, JJSUnixTerminal::new);
54 in = new ConsoleReader(cmdin, cmdout);
55 in.setExpandEvents(false);
56 in.setHandleUserInterrupt(true);
57 in.setBellEnabled(true);
58 in.setHistory(history = new FileHistory(historyFile));
59 in.addCompleter(completer);
60 Runtime.getRuntime().addShutdownHook(new Thread((Runnable)this::saveHistory));
61 }
62
63 String readLine(final String prompt) throws IOException {
64 return in.readLine(prompt);
65 }
66
67 @Override
68 public void close() {
69 saveHistory();
70 }
71
72 private void saveHistory() {
73 try {
74 getHistory().flush();
75 } catch (final IOException exp) {}
76 }
77
78 FileHistory getHistory() {
79 return (FileHistory) in.getHistory();
80 }
120 }
121
122 @Override
123 public void enableInterruptCharacter() {
124 }
125 }
126
127 static final class JJSWindowsTerminal extends WindowsTerminal {
128 public JJSWindowsTerminal() throws Exception {
129 }
130
131 @Override
132 public void init() throws Exception {
133 super.init();
134 setAnsiSupported(false);
135 }
136 }
137
138 private static boolean isCygwin() {
139 return System.getenv("SHELL") != null;
140 }
141 }
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.tools.jjs;
27
28 import java.awt.event.ActionListener;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.PrintStream;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.function.Function;
39 import jdk.internal.jline.NoInterruptUnixTerminal;
40 import jdk.internal.jline.Terminal;
41 import jdk.internal.jline.TerminalFactory;
42 import jdk.internal.jline.TerminalFactory.Flavor;
43 import jdk.internal.jline.WindowsTerminal;
44 import jdk.internal.jline.console.ConsoleReader;
45 import jdk.internal.jline.console.KeyMap;
46 import jdk.internal.jline.console.completer.Completer;
47 import jdk.internal.jline.console.history.FileHistory;
48
49 class Console implements AutoCloseable {
50 private static final String DOCUMENTATION_SHORTCUT = "\033\133\132"; //Shift-TAB
51 private final ConsoleReader in;
52 private final FileHistory history;
53
54 Console(final InputStream cmdin, final PrintStream cmdout, final File historyFile,
55 final Completer completer, final Function<String, String> docHelper) throws IOException {
56 TerminalFactory.registerFlavor(Flavor.WINDOWS, isCygwin()? JJSUnixTerminal::new : JJSWindowsTerminal::new);
57 TerminalFactory.registerFlavor(Flavor.UNIX, JJSUnixTerminal::new);
58 in = new ConsoleReader(cmdin, cmdout);
59 in.setExpandEvents(false);
60 in.setHandleUserInterrupt(true);
61 in.setBellEnabled(true);
62 in.setHistory(history = new FileHistory(historyFile));
63 in.addCompleter(completer);
64 Runtime.getRuntime().addShutdownHook(new Thread((Runnable)this::saveHistory));
65 bind(DOCUMENTATION_SHORTCUT, (ActionListener)evt -> showDocumentation(docHelper));
66 }
67
68 String readLine(final String prompt) throws IOException {
69 return in.readLine(prompt);
70 }
71
72 @Override
73 public void close() {
74 saveHistory();
75 }
76
77 private void saveHistory() {
78 try {
79 getHistory().flush();
80 } catch (final IOException exp) {}
81 }
82
83 FileHistory getHistory() {
84 return (FileHistory) in.getHistory();
85 }
125 }
126
127 @Override
128 public void enableInterruptCharacter() {
129 }
130 }
131
132 static final class JJSWindowsTerminal extends WindowsTerminal {
133 public JJSWindowsTerminal() throws Exception {
134 }
135
136 @Override
137 public void init() throws Exception {
138 super.init();
139 setAnsiSupported(false);
140 }
141 }
142
143 private static boolean isCygwin() {
144 return System.getenv("SHELL") != null;
145 }
146
147 private void bind(String shortcut, Object action) {
148 KeyMap km = in.getKeys();
149 for (int i = 0; i < shortcut.length(); i++) {
150 final Object value = km.getBound(Character.toString(shortcut.charAt(i)));
151 if (value instanceof KeyMap) {
152 km = (KeyMap) value;
153 } else {
154 km.bind(shortcut.substring(i), action);
155 }
156 }
157 }
158
159 private void showDocumentation(final Function<String, String> docHelper) {
160 final String buffer = in.getCursorBuffer().buffer.toString();
161 final int cursor = in.getCursorBuffer().cursor;
162 final String doc = docHelper.apply(buffer.substring(0, cursor));
163 try {
164 if (doc != null) {
165 in.println();
166 in.println(doc);
167 in.redrawLine();
168 in.flush();
169 } else {
170 in.beep();
171 }
172 } catch (IOException ex) {
173 throw new IllegalStateException(ex);
174 }
175 }
176 }
|