src/jdk.internal.le/share/classes/jdk/internal/org/jline/reader/LineReaderBuilder.java
author pliden
Fri, 13 Sep 2019 08:40:09 +0200
changeset 58125 9b4717ca9bd1
parent 52938 5ff7480c9e28
child 58903 eeb1c0da2126
permissions -rw-r--r--
8230878: ZGC: Use THREAD_LOCAL instead of __thread Reviewed-by: kbarrett
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
     1
/*
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2002-2018, the original author or authors.
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
     3
 *
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
     4
 * This software is distributable under the BSD license. See the terms of the
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
     5
 * BSD license in the documentation provided with this software.
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
     6
 *
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
     7
 * http://www.opensource.org/licenses/bsd-license.php
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
     8
 */
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
     9
package jdk.internal.org.jline.reader;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    10
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    11
import java.io.IOError;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    12
import java.io.IOException;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    13
import java.util.HashMap;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    14
import java.util.Map;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    15
import java.util.Objects;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    16
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    17
import jdk.internal.org.jline.reader.impl.LineReaderImpl;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    18
import jdk.internal.org.jline.reader.impl.history.DefaultHistory;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    19
import jdk.internal.org.jline.terminal.Terminal;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    20
import jdk.internal.org.jline.terminal.TerminalBuilder;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    21
import jdk.internal.org.jline.utils.Log;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    22
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    23
public final class LineReaderBuilder {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    24
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    25
    public static LineReaderBuilder builder() {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    26
        return new LineReaderBuilder();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    27
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    28
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    29
    Terminal terminal;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    30
    String appName;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    31
    Map<String, Object> variables = new HashMap<>();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    32
    Map<LineReader.Option, Boolean> options = new HashMap<>();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    33
    History history;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    34
    Completer completer;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    35
    History memoryHistory;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    36
    Highlighter highlighter;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    37
    Parser parser;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    38
    Expander expander;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    39
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    40
    private LineReaderBuilder() {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    41
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    42
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    43
    public LineReaderBuilder terminal(Terminal terminal) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    44
        this.terminal = terminal;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    45
        return this;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    46
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    47
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    48
    public LineReaderBuilder appName(String appName) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    49
        this.appName = appName;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    50
        return this;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    51
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    52
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    53
    public LineReaderBuilder variables(Map<String, Object> variables) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    54
        Map<String, Object> old = this.variables;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    55
        this.variables = Objects.requireNonNull(variables);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    56
        this.variables.putAll(old);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    57
        return this;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    58
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    59
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    60
    public LineReaderBuilder variable(String name, Object value) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    61
        this.variables.put(name, value);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    62
        return this;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    63
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    64
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    65
    public LineReaderBuilder option(LineReader.Option option, boolean value) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    66
        this.options.put(option, value);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    67
        return this;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    68
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    69
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    70
    public LineReaderBuilder history(History history) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    71
        this.history = history;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    72
        return this;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    73
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    74
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    75
    public LineReaderBuilder completer(Completer completer) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    76
        this.completer = completer;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    77
        return this;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    78
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    79
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    80
    public LineReaderBuilder highlighter(Highlighter highlighter) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    81
        this.highlighter = highlighter;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    82
        return this;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    83
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    84
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    85
    public LineReaderBuilder parser(Parser parser) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    86
        if (parser != null) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    87
            try {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    88
                if (!Boolean.parseBoolean(LineReader.PROP_SUPPORT_PARSEDLINE)
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    89
                        && !(parser.parse("", 0) instanceof CompletingParsedLine)) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    90
                    Log.warn("The Parser of class " + parser.getClass().getName() + " does not support the CompletingParsedLine interface. " +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    91
                            "Completion with escaped or quoted words won't work correctly.");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    92
                }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    93
            } catch (Throwable t) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    94
                // Ignore
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    95
            }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    96
        }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    97
        this.parser = parser;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    98
        return this;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
    99
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   100
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   101
    public LineReaderBuilder expander(Expander expander) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   102
        this.expander = expander;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   103
        return this;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   104
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   105
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   106
    public LineReader build() {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   107
        Terminal terminal = this.terminal;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   108
        if (terminal == null) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   109
            try {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   110
                terminal = TerminalBuilder.terminal();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   111
            } catch (IOException e) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   112
                throw new IOError(e);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   113
            }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   114
        }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   115
        LineReaderImpl reader = new LineReaderImpl(terminal, appName, variables);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   116
        if (history != null) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   117
            reader.setHistory(history);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   118
        } else {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   119
            if (memoryHistory == null) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   120
                memoryHistory = new DefaultHistory();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   121
            }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   122
            reader.setHistory(memoryHistory);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   123
        }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   124
        if (completer != null) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   125
            reader.setCompleter(completer);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   126
        }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   127
        if (highlighter != null) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   128
            reader.setHighlighter(highlighter);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   129
        }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   130
        if (parser != null) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   131
            reader.setParser(parser);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   132
        }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   133
        if (expander != null) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   134
            reader.setExpander(expander);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   135
        }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   136
        for (Map.Entry<LineReader.Option, Boolean> e : options.entrySet()) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   137
            reader.option(e.getKey(), e.getValue());
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   138
        }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   139
        return reader;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   140
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   141
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents:
diff changeset
   142
}