src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/Console.java
author jlahoda
Fri, 01 Jun 2018 13:04:30 +0200
changeset 50338 1d5694c1aa03
parent 47492 560fab171dc7
child 52938 5ff7480c9e28
permissions -rw-r--r--
8203827: Upgrade JLine to 2.14.6 Reviewed-by: alanb, hannesw, rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     1
/*
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     4
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    10
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    15
 * accompanied this code).
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    16
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    20
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    23
 * questions.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    24
 */
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    25
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    26
package jdk.nashorn.tools.jjs;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    27
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
    28
import java.io.File;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    29
import java.io.IOException;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    30
import java.io.InputStream;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    31
import java.io.PrintStream;
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    32
import java.io.Writer;
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    33
import java.nio.file.Files;
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
    34
import java.util.function.Function;
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    35
import java.util.stream.Collectors;
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
    36
import jdk.internal.jline.NoInterruptUnixTerminal;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
    37
import jdk.internal.jline.Terminal;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
    38
import jdk.internal.jline.TerminalFactory;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
    39
import jdk.internal.jline.TerminalFactory.Flavor;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
    40
import jdk.internal.jline.WindowsTerminal;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    41
import jdk.internal.jline.console.ConsoleReader;
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
    42
import jdk.internal.jline.console.KeyMap;
50338
1d5694c1aa03 8203827: Upgrade JLine to 2.14.6
jlahoda
parents: 47492
diff changeset
    43
import jdk.internal.jline.console.completer.CandidateListCompletionHandler;
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    44
import jdk.internal.jline.extra.EditingHistory;
41616
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    45
import jdk.internal.misc.Signal;
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    46
import jdk.internal.misc.Signal.Handler;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    47
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    48
class Console implements AutoCloseable {
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
    49
    private static final String DOCUMENTATION_SHORTCUT = "\033\133\132"; //Shift-TAB
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    50
    private final ConsoleReader in;
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    51
    private final File historyFile;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    52
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
    53
    Console(final InputStream cmdin, final PrintStream cmdout, final File historyFile,
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    54
            final NashornCompleter completer, final Function<String, String> docHelper) throws IOException {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    55
        this.historyFile = historyFile;
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    56
50338
1d5694c1aa03 8203827: Upgrade JLine to 2.14.6
jlahoda
parents: 47492
diff changeset
    57
        TerminalFactory.registerFlavor(Flavor.WINDOWS, ttyDevice -> isCygwin() ? new JJSUnixTerminal() : new JJSWindowsTerminal());
1d5694c1aa03 8203827: Upgrade JLine to 2.14.6
jlahoda
parents: 47492
diff changeset
    58
        TerminalFactory.registerFlavor(Flavor.UNIX, ttyDevice -> new JJSUnixTerminal());
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    59
        in = new ConsoleReader(cmdin, cmdout);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    60
        in.setExpandEvents(false);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    61
        in.setHandleUserInterrupt(true);
32240
d7c7a5dc92d8 8133652: Implement tab-completion for member select expressions
sundar
parents: 32152
diff changeset
    62
        in.setBellEnabled(true);
38494
52305229ea03 8131017: jshell tool: pasting code with tabs invokes tab completion
jlahoda
parents: 38488
diff changeset
    63
        in.setCopyPasteDetection(true);
50338
1d5694c1aa03 8203827: Upgrade JLine to 2.14.6
jlahoda
parents: 47492
diff changeset
    64
        ((CandidateListCompletionHandler) in.getCompletionHandler()).setPrintSpaceAfterFullCompletion(false);
38898
43383182cae3 8159031: jjs throws NoSuchFileException if ~/.jjs.history does not exist
hannesw
parents: 38494
diff changeset
    65
        final Iterable<String> existingHistory = historyFile.exists() ? Files.readAllLines(historyFile.toPath()) : null;
43383182cae3 8159031: jjs throws NoSuchFileException if ~/.jjs.history does not exist
hannesw
parents: 38494
diff changeset
    66
        in.setHistory(new EditingHistory(in, existingHistory) {
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    67
            @Override protected boolean isComplete(CharSequence input) {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    68
                return completer.isComplete(input.toString());
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    69
            }
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    70
        });
32240
d7c7a5dc92d8 8133652: Implement tab-completion for member select expressions
sundar
parents: 32152
diff changeset
    71
        in.addCompleter(completer);
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
    72
        Runtime.getRuntime().addShutdownHook(new Thread((Runnable)this::saveHistory));
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
    73
        bind(DOCUMENTATION_SHORTCUT, (Runnable) ()->showDocumentation(docHelper));
41616
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    74
        try {
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    75
            Signal.handle(new Signal("CONT"), new Handler() {
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    76
                @Override public void handle(Signal sig) {
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    77
                    try {
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    78
                        in.getTerminal().reset();
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    79
                        in.redrawLine();
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    80
                        in.flush();
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    81
                    } catch (Exception ex) {
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    82
                        ex.printStackTrace();
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    83
                    }
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    84
                }
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    85
            });
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    86
        } catch (IllegalArgumentException ignored) {
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    87
            //the CONT signal does not exist on this platform
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    88
        }
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    89
    }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    90
32240
d7c7a5dc92d8 8133652: Implement tab-completion for member select expressions
sundar
parents: 32152
diff changeset
    91
    String readLine(final String prompt) throws IOException {
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    92
        return in.readLine(prompt);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    93
    }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    94
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    95
    @Override
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    96
    public void close() {
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
    97
        saveHistory();
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    98
    }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    99
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
   100
    private void saveHistory() {
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   101
        try (Writer out = Files.newBufferedWriter(historyFile.toPath())) {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   102
            String lineSeparator = System.getProperty("line.separator");
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   103
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   104
            out.write(getHistory().save()
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   105
                                  .stream()
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   106
                                  .collect(Collectors.joining(lineSeparator)));
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
   107
        } catch (final IOException exp) {}
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
   108
    }
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   109
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   110
    EditingHistory getHistory() {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   111
        return (EditingHistory) in.getHistory();
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   112
    }
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   113
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   114
    boolean terminalEditorRunning() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   115
        Terminal terminal = in.getTerminal();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   116
        if (terminal instanceof JJSUnixTerminal) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   117
            return ((JJSUnixTerminal) terminal).isRaw();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   118
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   119
        return false;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   120
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   121
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   122
    void suspend() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   123
        try {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   124
            in.getTerminal().restore();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   125
        } catch (Exception ex) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   126
            throw new IllegalStateException(ex);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   127
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   128
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   129
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   130
    void resume() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   131
        try {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   132
            in.getTerminal().init();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   133
        } catch (Exception ex) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   134
            throw new IllegalStateException(ex);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   135
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   136
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   137
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   138
    static final class JJSUnixTerminal extends NoInterruptUnixTerminal {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   139
        JJSUnixTerminal() throws Exception {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   140
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   141
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   142
        boolean isRaw() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   143
            try {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   144
                return getSettings().get("-a").contains("-icanon");
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   145
            } catch (IOException | InterruptedException ex) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   146
                return false;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   147
            }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   148
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   149
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   150
        @Override
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   151
        public void disableInterruptCharacter() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   152
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   153
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   154
        @Override
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   155
        public void enableInterruptCharacter() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   156
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   157
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   158
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   159
    static final class JJSWindowsTerminal extends WindowsTerminal {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   160
        public JJSWindowsTerminal() throws Exception {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   161
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   162
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   163
        @Override
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   164
        public void init() throws Exception {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   165
            super.init();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   166
            setAnsiSupported(false);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   167
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   168
    }
32525
3244ec62a5cb 8135151: jjs should work in cygwin environment
sundar
parents: 32313
diff changeset
   169
3244ec62a5cb 8135151: jjs should work in cygwin environment
sundar
parents: 32313
diff changeset
   170
    private static boolean isCygwin() {
3244ec62a5cb 8135151: jjs should work in cygwin environment
sundar
parents: 32313
diff changeset
   171
        return System.getenv("SHELL") != null;
3244ec62a5cb 8135151: jjs should work in cygwin environment
sundar
parents: 32313
diff changeset
   172
    }
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   173
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   174
    private void bind(String shortcut, Object action) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   175
        KeyMap km = in.getKeys();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   176
        for (int i = 0; i < shortcut.length(); i++) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   177
            final Object value = km.getBound(Character.toString(shortcut.charAt(i)));
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   178
            if (value instanceof KeyMap) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   179
                km = (KeyMap) value;
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   180
            } else {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   181
                km.bind(shortcut.substring(i), action);
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   182
            }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   183
        }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   184
    }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   185
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   186
    private void showDocumentation(final Function<String, String> docHelper) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   187
        final String buffer = in.getCursorBuffer().buffer.toString();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   188
        final int cursor = in.getCursorBuffer().cursor;
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   189
        final String doc = docHelper.apply(buffer.substring(0, cursor));
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   190
        try {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   191
            if (doc != null) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   192
                in.println();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   193
                in.println(doc);
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   194
                in.redrawLine();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   195
                in.flush();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   196
            } else {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   197
                in.beep();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   198
            }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   199
        } catch (IOException ex) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   200
            throw new IllegalStateException(ex);
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   201
        }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   202
    }
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   203
}