|
8 | 8 | */ |
9 | 9 | package org.jline.demo.graal; |
10 | 10 |
|
11 | | -import java.io.File; |
12 | | -import java.nio.file.Path; |
13 | 11 | import java.nio.file.Paths; |
14 | | -import java.util.*; |
15 | | -import java.util.function.Supplier; |
16 | | -import java.util.logging.LogManager; |
17 | 12 |
|
18 | | -import org.jline.builtins.ConfigurationPath; |
19 | | -import org.jline.console.impl.Builtins; |
20 | | -import org.jline.console.impl.Builtins.Command; |
21 | | -import org.jline.console.impl.SystemRegistryImpl; |
22 | | -import org.jline.keymap.KeyMap; |
23 | | -import org.jline.reader.*; |
24 | | -import org.jline.reader.LineReader.Option; |
25 | | -import org.jline.reader.impl.DefaultParser; |
| 13 | +import org.jline.reader.LineReader; |
| 14 | +import org.jline.shell.Shell; |
26 | 15 | import org.jline.terminal.Terminal; |
27 | | -import org.jline.terminal.Terminal.Signal; |
28 | 16 | import org.jline.terminal.TerminalBuilder; |
29 | 17 | import org.jline.terminal.spi.TerminalExt; |
| 18 | +import org.jline.terminal.spi.TerminalProvider; |
30 | 19 | import org.jline.utils.OSUtils; |
31 | | -import org.jline.widget.TailTipWidgets; |
32 | | -import org.jline.widget.TailTipWidgets.TipType; |
33 | | -import org.jline.widget.Widgets; |
34 | 20 |
|
35 | 21 | public class Graal { |
36 | 22 |
|
37 | 23 | public static void main(String[] args) { |
| 24 | + if (args.length > 0 && "--check".equals(args[0])) { |
| 25 | + check(); |
| 26 | + return; |
| 27 | + } |
38 | 28 | try { |
39 | | - // Init log |
40 | | - String fname = System.getProperty("java.util.logging.config.file"); |
41 | | - if (fname != null) { |
42 | | - LogManager.getLogManager().readConfiguration(); |
43 | | - } |
44 | | - Supplier<Path> workDir = () -> Paths.get(System.getProperty("user.dir")); |
45 | | - // |
46 | | - // Parser & Terminal |
47 | | - // |
48 | | - DefaultParser parser = new DefaultParser(); |
49 | | - parser.setEofOnUnclosedQuote(true); |
50 | | - parser.setEscapeChars(null); |
51 | | - parser.setRegexVariable(null); // we do not have console variables! |
52 | 29 | Terminal terminal = TerminalBuilder.builder().build(); |
53 | | - Thread executeThread = Thread.currentThread(); |
54 | | - terminal.handle(Signal.INT, signal -> executeThread.interrupt()); |
55 | | - // |
56 | | - // Command registries |
57 | | - // |
58 | | - File file = new File(Graal.class |
59 | | - .getProtectionDomain() |
60 | | - .getCodeSource() |
61 | | - .getLocation() |
62 | | - .toURI() |
63 | | - .getPath()); |
64 | | - String root = file.getCanonicalPath(); |
65 | | - root = root.substring(0, root.length() - 6); |
66 | | - ConfigurationPath configPath = new ConfigurationPath(Paths.get(root), Paths.get(root)); |
67 | | - Set<Builtins.Command> commands = new HashSet<>(Arrays.asList(Builtins.Command.values())); |
68 | | - commands.remove(Command.TTOP); // ttop command is not supported in GraalVM |
69 | | - Builtins builtins = new Builtins(commands, workDir, configPath, null); |
70 | | - SystemRegistryImpl systemRegistry = new SystemRegistryImpl(parser, terminal, workDir, configPath); |
71 | | - systemRegistry.setCommandRegistries(builtins); |
72 | | - // |
73 | | - // LineReader |
74 | | - // |
75 | | - LineReader reader = LineReaderBuilder.builder() |
| 30 | + |
| 31 | + System.out.println(terminal.getName() + ": " + terminal.getType() + ", provider=" |
| 32 | + + ((TerminalExt) terminal).getProvider().name()); |
| 33 | + |
| 34 | + Shell shell = Shell.builder() |
76 | 35 | .terminal(terminal) |
77 | | - .completer(systemRegistry.completer()) |
78 | | - .parser(parser) |
| 36 | + .prompt("graal> ") |
| 37 | + .helpCommands(true) |
| 38 | + .historyCommands(true) |
| 39 | + .optionCommands(true) |
| 40 | + .variableCommands(true) |
| 41 | + .commandHighlighter(true) |
| 42 | + .historyFile(Paths.get(System.getProperty("user.home"), ".jline-graal-history")) |
| 43 | + .option(LineReader.Option.INSERT_BRACKET, true) |
| 44 | + .option(LineReader.Option.EMPTY_WORD_OPTIONS, false) |
| 45 | + .option(LineReader.Option.USE_FORWARD_SLASH, true) |
| 46 | + .option(LineReader.Option.DISABLE_EVENT_EXPANSION, true) |
79 | 47 | .variable(LineReader.SECONDARY_PROMPT_PATTERN, "%M%P > ") |
80 | 48 | .variable(LineReader.INDENTATION, 2) |
81 | 49 | .variable(LineReader.LIST_MAX, 100) |
82 | | - .variable(LineReader.HISTORY_FILE, Paths.get(root, "history")) |
83 | | - .option(Option.INSERT_BRACKET, true) |
84 | | - .option(Option.EMPTY_WORD_OPTIONS, false) |
85 | | - .option(Option.USE_FORWARD_SLASH, true) // use forward slash in directory separator |
86 | | - .option(Option.DISABLE_EVENT_EXPANSION, true) |
| 50 | + .onReaderReady(reader -> { |
| 51 | + if (OSUtils.IS_WINDOWS) { |
| 52 | + reader.setVariable(LineReader.BLINK_MATCHING_PAREN, 0); |
| 53 | + } |
| 54 | + }) |
87 | 55 | .build(); |
88 | | - if (OSUtils.IS_WINDOWS) { |
89 | | - reader.setVariable( |
90 | | - LineReader.BLINK_MATCHING_PAREN, 0); // if enabled cursor remains in begin parenthesis (gitbash) |
91 | | - } |
92 | | - // |
93 | | - // complete command registries |
94 | | - // |
95 | | - builtins.setLineReader(reader); |
96 | | - // |
97 | | - // widgets and console initialization |
98 | | - // |
99 | | - new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TipType.COMPLETER); |
100 | | - KeyMap<Binding> keyMap = reader.getKeyMaps().get("main"); |
101 | | - keyMap.bind(new Reference(Widgets.TAILTIP_TOGGLE), KeyMap.alt("s")); |
102 | | - // |
103 | | - // REPL-loop |
104 | | - // |
105 | | - System.out.println(terminal.getName() + ": " + terminal.getType() + ", provider=" |
106 | | - + ((TerminalExt) terminal).getProvider().name()); |
107 | | - while (true) { |
| 56 | + |
| 57 | + shell.run(); |
| 58 | + } catch (Throwable t) { |
| 59 | + t.printStackTrace(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Verify that a terminal provider can be loaded and used. |
| 65 | + * This is used as a smoke test for native image builds. |
| 66 | + */ |
| 67 | + private static void check() { |
| 68 | + try { |
| 69 | + TerminalProvider provider = null; |
| 70 | + for (String name : new String[] {"ffm", "jni"}) { |
108 | 71 | try { |
109 | | - systemRegistry.cleanUp(); // reset output streams |
110 | | - String line = reader.readLine("graal> "); |
111 | | - Object result = systemRegistry.execute(line); |
112 | | - if (result != null) { |
113 | | - System.out.println(result); |
114 | | - } |
115 | | - } catch (UserInterruptException e) { |
116 | | - // Ignore |
117 | | - } catch (EndOfFileException e) { |
| 72 | + provider = TerminalProvider.load(name); |
118 | 73 | break; |
119 | | - } catch (Exception e) { |
120 | | - systemRegistry.trace(true, e); // print exception |
| 74 | + } catch (Throwable ignored) { |
121 | 75 | } |
122 | 76 | } |
123 | | - systemRegistry.close(); |
| 77 | + if (provider == null) { |
| 78 | + throw new IllegalStateException("No terminal provider found"); |
| 79 | + } |
| 80 | + System.out.println("Provider loaded: " + provider.name()); |
| 81 | + System.out.println("CHECK PASSED"); |
124 | 82 | } catch (Throwable t) { |
125 | | - t.printStackTrace(); |
| 83 | + System.err.println("CHECK FAILED: " + t.getMessage()); |
| 84 | + t.printStackTrace(System.err); |
| 85 | + System.exit(1); |
126 | 86 | } |
127 | 87 | } |
128 | 88 | } |
0 commit comments