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