Skip to content

Commit 9353bf9

Browse files
committed
Добавлен обработчик консольного ввода JLine
1 parent cc4bd1f commit 9353bf9

File tree

5 files changed

+100
-7
lines changed

5 files changed

+100
-7
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ dependencies {
9393
}
9494
compile 'org.json:json:20160212'
9595
compile 'org.yaml:snakeyaml:1.17'
96+
compile 'jline:jline:2.14.5'
9697

9798
testCompile 'junit:junit:4.12'
9899
testCompile 'org.openjdk.jmh:jmh-core:1.13'

src/main/java/com/annimon/ownlang/utils/Repl.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
import com.annimon.ownlang.parser.ast.BlockStatement;
1414
import com.annimon.ownlang.parser.ast.Statement;
1515
import com.annimon.ownlang.parser.visitors.PrintVisitor;
16+
import com.annimon.ownlang.utils.repl.JLineConsole;
17+
import com.annimon.ownlang.utils.repl.ReplConsole;
18+
import com.annimon.ownlang.utils.repl.SystemConsole;
19+
import java.io.IOException;
1620
import java.util.ArrayList;
1721
import java.util.List;
1822
import java.util.Locale;
1923
import java.util.Map;
20-
import java.util.Scanner;
2124
import java.util.stream.Collectors;
2225

2326
public final class Repl {
@@ -36,14 +39,13 @@ public static void main(String[] args) {
3639

3740
final BlockStatement history = new BlockStatement();
3841
final StringBuilder buffer = new StringBuilder();
39-
final Scanner scanner = new Scanner(System.in);
42+
final ReplConsole console = initReplConsole();
4043
while (true) {
41-
System.out.print((buffer.length() == 0) ? "\n> " : " ");
44+
console.setPrompt((buffer.length() == 0) ? "\n> " : " ");
4245

43-
if (!scanner.hasNextLine()) break;
46+
final String line = console.readLine();
47+
if (line == null || EXIT.equalsIgnoreCase(line)) break;
4448

45-
final String line = scanner.nextLine();
46-
if (EXIT.equalsIgnoreCase(line)) break;
4749
switch (line.toLowerCase(Locale.ENGLISH)) {
4850
case RESET:
4951
buffer.setLength(0);
@@ -84,7 +86,15 @@ public static void main(String[] args) {
8486
}
8587
buffer.setLength(0);
8688
}
87-
scanner.close();
89+
console.close();
90+
}
91+
92+
private static ReplConsole initReplConsole() {
93+
try {
94+
return new JLineConsole();
95+
} catch (IOException ioe) {
96+
return new SystemConsole();
97+
}
8898
}
8999

90100
private static void printHelp(boolean full) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.annimon.ownlang.utils.repl;
2+
3+
import java.io.IOException;
4+
import jline.TerminalFactory;
5+
import jline.console.ConsoleReader;
6+
7+
public class JLineConsole implements ReplConsole {
8+
9+
private final ConsoleReader console;
10+
11+
public JLineConsole() throws IOException {
12+
console = new ConsoleReader();
13+
}
14+
15+
public ConsoleReader getConsole() {
16+
return console;
17+
}
18+
19+
@Override
20+
public void setPrompt(String prompt) {
21+
console.setPrompt(prompt);
22+
}
23+
24+
@Override
25+
public String readLine() {
26+
try {
27+
return console.readLine();
28+
} catch (IOException ex) {
29+
return null;
30+
}
31+
}
32+
33+
@Override
34+
public void close() {
35+
try {
36+
TerminalFactory.get().restore();
37+
} catch (Exception ignored) {
38+
}
39+
}
40+
41+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.annimon.ownlang.utils.repl;
2+
3+
public interface ReplConsole {
4+
5+
void setPrompt(String prompt);
6+
7+
String readLine();
8+
9+
void close();
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.annimon.ownlang.utils.repl;
2+
3+
import java.util.Scanner;
4+
5+
public class SystemConsole implements ReplConsole {
6+
7+
private final Scanner scanner;
8+
9+
public SystemConsole() {
10+
scanner = new Scanner(System.in);
11+
}
12+
13+
@Override
14+
public void setPrompt(String prompt) {
15+
System.out.print(prompt);
16+
}
17+
18+
@Override
19+
public String readLine() {
20+
if (!scanner.hasNextLine()) {
21+
return null;
22+
}
23+
return scanner.nextLine();
24+
}
25+
26+
@Override
27+
public void close() {
28+
scanner.close();
29+
}
30+
31+
}

0 commit comments

Comments
 (0)