Skip to content

Commit 2aa66b5

Browse files
authored
Allow pasting multiline input (#105)
## What is the goal of this PR? Allow user to paste multiline input to console and edit as one unit. ## What are the changes implemented in this PR? - Set continuation prompt to be empty indentation. - Set history of multiline query as one single unit. - Fix #99
1 parent 8252bba commit 2aa66b5

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

command/ReplCommand.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ public static String getHelpMenu() {
117117
public static ReplCommand getCommand(LineReader reader, Printer printer, String prompt) throws InterruptedException {
118118
ReplCommand command = null;
119119
while (command == null) {
120-
String[] tokens = Utils.getTokens(reader, prompt);
120+
String line = Utils.readNonEmptyLine(reader, prompt);
121+
String[] tokens = Utils.splitLineByWhitespace(line);
121122
if (tokens.length == 1 && tokens[0].equals(Exit.token)) {
122123
command = new Exit();
123124
} else if (tokens.length == 1 && tokens[0].equals(Help.token)) {
@@ -141,6 +142,7 @@ public static ReplCommand getCommand(LineReader reader, Printer printer, String
141142
} else {
142143
printer.error("Unrecognised command, please check help menu");
143144
}
145+
reader.getHistory().add(line.trim());
144146
}
145147
return command;
146148
}

command/TransactionReplCommand.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import java.util.ArrayList;
2626
import java.util.Arrays;
27-
import java.util.Collections;
2827
import java.util.List;
2928

3029
import static grakn.common.collection.Collections.pair;
@@ -108,7 +107,8 @@ public static String getHelpMenu() {
108107

109108
public static TransactionReplCommand getCommand(LineReader reader, String prompt) throws InterruptedException {
110109
TransactionReplCommand command;
111-
String[] tokens = Utils.getTokens(reader, prompt);
110+
String line = Utils.readNonEmptyLine(reader, prompt);
111+
String[] tokens = Utils.splitLineByWhitespace(line);
112112
if (tokens.length == 1 && tokens[0].equals(Exit.token)) {
113113
command = new Exit();
114114
} else if (tokens.length == 1 && tokens[0].equals(Help.token)) {
@@ -125,21 +125,22 @@ public static TransactionReplCommand getCommand(LineReader reader, String prompt
125125
String file = tokens[1];
126126
command = new Source(file);
127127
} else {
128-
String firstQueryLine = String.join(" ", tokens);
129-
String query = getQuery(reader, firstQueryLine, prompt.length());
128+
String query = readQuery(reader, prompt, line);
130129
command = new Query(query);
131130
}
131+
if (command instanceof Query) reader.getHistory().add(command.asQuery().query().trim());
132+
else reader.getHistory().add(line.trim());
132133
return command;
133134
}
134135

135-
private static String getQuery(LineReader reader, String firstQueryLine, int indentation) {
136+
private static String readQuery(LineReader reader, String prompt, String firstQueryLine) {
136137
List<String> queryLines = new ArrayList<>();
137138
queryLines.add(firstQueryLine);
138139
while (true) {
139-
String prompt = String.join("", Collections.nCopies(indentation, " "));
140+
String queryPrompt = Utils.getContinuationPrompt(prompt);
140141
String queryLine;
141142
try {
142-
queryLine = reader.readLine(prompt);
143+
queryLine = Utils.readLineWithoutHistory(reader, queryPrompt);
143144
} catch (UserInterruptException | EndOfFileException e) {
144145
break;
145146
}

common/Utils.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import org.jline.reader.UserInterruptException;
2424

2525
import java.util.Arrays;
26+
import java.util.Collections;
2627
import java.util.List;
2728

2829
public class Utils {
30+
2931
public static String buildHelpMenu(List<Pair<String, String>> menu) {
3032
if (menu.isEmpty()) return "\n";
3133
int maxHelpCommandLength = menu.stream().map(x -> x.first().length()).max(Integer::compare).get();
@@ -42,13 +44,23 @@ public static String buildHelpMenu(List<Pair<String, String>> menu) {
4244
return sb.toString();
4345
}
4446

45-
public static String[] getTokens(LineReader reader, String prompt) throws InterruptedException {
46-
String[] words = null;
47-
while (words == null) {
47+
public static String readLineWithoutHistory(LineReader reader, String prompt) {
48+
String continuationPrompt = getContinuationPrompt(prompt);
49+
reader.variable(LineReader.SECONDARY_PROMPT_PATTERN, continuationPrompt);
50+
try {
51+
reader.variable(LineReader.DISABLE_HISTORY, true);
52+
return reader.readLine(prompt);
53+
} finally {
54+
reader.variable(LineReader.DISABLE_HISTORY, false);
55+
}
56+
}
57+
58+
public static String readNonEmptyLine(LineReader reader, String prompt) throws InterruptedException {
59+
String line = null;
60+
while (line == null) {
4861
try {
49-
String line = reader.readLine(prompt);
50-
words = Utils.splitLineByWhitespace(line);
51-
if (words.length == 0) words = null;
62+
line = readLineWithoutHistory(reader, prompt);
63+
if (line.trim().isEmpty()) line = null;
5264
} catch (UserInterruptException e) {
5365
if (reader.getBuffer().toString().isEmpty()) {
5466
throw new InterruptedException();
@@ -57,10 +69,14 @@ public static String[] getTokens(LineReader reader, String prompt) throws Interr
5769
throw new InterruptedException();
5870
}
5971
}
60-
return words;
72+
return line;
73+
}
74+
75+
public static String getContinuationPrompt(String prompt) {
76+
return String.join("", Collections.nCopies(prompt.length(), " "));
6177
}
6278

63-
private static String[] splitLineByWhitespace(String line) {
79+
public static String[] splitLineByWhitespace(String line) {
6480
return Arrays.stream(line.split("\\s+")).map(String::trim).filter(x -> !x.isEmpty()).toArray(String[]::new);
6581
}
6682
}

0 commit comments

Comments
 (0)