Add script command.
Also: prepare for execute-then-quit. Add javadocs
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.jline;
|
||||
|
||||
import org.jline.reader.ParsedLine;
|
||||
import org.jline.reader.Parser;
|
||||
import org.springframework.shell.Input;
|
||||
import org.springframework.shell.InputProvider;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
|
||||
/**
|
||||
* An {@link InputProvider} that reads input from file-like sources.
|
||||
* <p>
|
||||
* <p>Still uses a {@link org.jline.reader.Parser} to interpret word boundaries. Supports backslashes at end
|
||||
* of line to signal line continuation.</p>
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class FileInputProvider implements InputProvider, Closeable {
|
||||
|
||||
private static final String BACKSLASH_AT_EOL_REGEX = "(.*)\\\\\\s*$";
|
||||
private final BufferedReader reader;
|
||||
|
||||
private final Parser parser;
|
||||
|
||||
public FileInputProvider(Reader reader, Parser parser) {
|
||||
this.reader = new BufferedReader(reader);
|
||||
this.parser = parser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Input readInput() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean continued = false;
|
||||
String line;
|
||||
try {
|
||||
do {
|
||||
line = reader.readLine();
|
||||
if (line == null) {
|
||||
break;
|
||||
}
|
||||
continued = line.matches(BACKSLASH_AT_EOL_REGEX);
|
||||
sb.append(line.replaceFirst(BACKSLASH_AT_EOL_REGEX, "$1 "));
|
||||
} while (continued);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (line == null) {
|
||||
return null;
|
||||
} else {
|
||||
ParsedLine parsedLine = parser.parse(sb.toString(), sb.toString().length());
|
||||
return new ParsedLineInput(parsedLine);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
@@ -22,13 +22,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.jline.reader.Candidate;
|
||||
import org.jline.reader.Completer;
|
||||
import org.jline.reader.Highlighter;
|
||||
import org.jline.reader.LineReader;
|
||||
import org.jline.reader.LineReaderBuilder;
|
||||
import org.jline.reader.ParsedLine;
|
||||
import org.jline.reader.UserInterruptException;
|
||||
import org.jline.reader.*;
|
||||
import org.jline.terminal.Terminal;
|
||||
import org.jline.terminal.TerminalBuilder;
|
||||
import org.jline.utils.AttributedString;
|
||||
@@ -101,11 +95,15 @@ class JLineShellAutoConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LineReader lineReader() {
|
||||
public Parser parser() {
|
||||
ExtendedDefaultParser parser = new ExtendedDefaultParser();
|
||||
parser.setEofOnUnclosedQuote(true);
|
||||
parser.setEofOnEscapedNewLine(true);
|
||||
return parser;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LineReader lineReader() {
|
||||
LineReaderBuilder lineReaderBuilder = LineReaderBuilder.builder()
|
||||
.terminal(terminal())
|
||||
.appName("Spring Shell")
|
||||
@@ -130,7 +128,7 @@ class JLineShellAutoConfiguration {
|
||||
}
|
||||
}
|
||||
})
|
||||
.parser(parser);
|
||||
.parser(parser());
|
||||
|
||||
return lineReaderBuilder.build();
|
||||
}
|
||||
@@ -139,7 +137,7 @@ class JLineShellAutoConfiguration {
|
||||
* Sanitize the buffer input given the customizations applied to the JLine parser (<em>e.g.</em> support for
|
||||
* line continuations, <em>etc.</em>)
|
||||
*/
|
||||
static private List<String> sanitizeInput(List<String> words) {
|
||||
static List<String> sanitizeInput(List<String> words) {
|
||||
words = words.stream()
|
||||
.map(s -> s.replaceAll("^\\n+|\\n+$", "")) // CR at beginning/end of line introduced by backslash continuation
|
||||
.map(s -> s.replaceAll("\\n+", " ")) // CR in middle of word introduced by return inside a quoted string
|
||||
@@ -204,30 +202,11 @@ class JLineShellAutoConfiguration {
|
||||
return Input.EMPTY;
|
||||
}
|
||||
}
|
||||
return new JLineInput(lineReader.getParsedLine());
|
||||
return new ParsedLineInput(lineReader.getParsedLine());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static class JLineInput implements Input {
|
||||
|
||||
private final ParsedLine parsedLine;
|
||||
|
||||
JLineInput(ParsedLine parsedLine) {
|
||||
this.parsedLine = parsedLine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String rawText() {
|
||||
return parsedLine.line();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> words() {
|
||||
return sanitizeInput(parsedLine.words());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.jline;
|
||||
|
||||
import org.jline.reader.ParsedLine;
|
||||
import org.springframework.shell.Input;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An implementation of {@link Input} backed by the result of a {@link org.jline.reader.Parser#parse(String, int)}.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
class ParsedLineInput implements Input {
|
||||
|
||||
private final ParsedLine parsedLine;
|
||||
|
||||
ParsedLineInput(ParsedLine parsedLine) {
|
||||
this.parsedLine = parsedLine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String rawText() {
|
||||
return parsedLine.line();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> words() {
|
||||
return JLineShellAutoConfiguration.sanitizeInput(parsedLine.words());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user