Skip to content

Commit e025836

Browse files
committed
Refactoring in and out + remove useless braces in wrapper
1 parent 51c5d51 commit e025836

1 file changed

Lines changed: 26 additions & 27 deletions

File tree

JShellWrapper/src/main/java/JShellWrapper.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,33 @@ public class JShellWrapper {
1313

1414
public void run() {
1515
Config config = Config.load();
16-
Scanner scanner = new Scanner(System.in);
17-
String imports = desanitize(scanner.nextLine());
18-
String startup = desanitize(scanner.nextLine());
19-
StringOutputStream out = new StringOutputStream(1024);
20-
try (JShell shell = JShell.builder().out(new PrintStream(out)).build()) {
16+
Scanner processIn = new Scanner(System.in);
17+
PrintStream processOut = System.out;
18+
String imports = desanitize(processIn.nextLine());
19+
String startup = desanitize(processIn.nextLine());
20+
StringOutputStream jshellOut = new StringOutputStream(1024);
21+
try (JShell shell = JShell.builder().out(new PrintStream(jshellOut)).build()) {
2122
shell.eval(imports);
2223
shell.eval(startup);
2324
while(true) {
24-
String command = scanner.nextLine();
25+
String command = processIn.nextLine();
2526
switch (command) {
26-
case "eval" -> eval(scanner, config, shell, out);
27-
case "snippets" -> snippets(shell);
27+
case "eval" -> eval(processIn, processOut, config, shell, jshellOut);
28+
case "snippets" -> snippets(processOut, shell);
2829
case "exit" -> {
29-
ok();
30+
ok(processOut);
3031
return;
3132
}
32-
default -> {
33-
throw new RuntimeException("No such command \"" + command + "\"");
34-
}
33+
default -> throw new RuntimeException("No such command \"" + command + "\"");
3534
}
36-
System.out.flush();
35+
processOut.flush();
3736
}
3837
}
3938
}
4039

41-
private void ok() {
42-
System.out.println("OK");
43-
System.out.flush();
40+
private void ok(PrintStream processOut) {
41+
processOut.println("OK");
42+
processOut.flush();
4443
}
4544

4645
/**
@@ -66,11 +65,11 @@ private void ok() {
6665
* empty line<br>
6766
* </code>
6867
*/
69-
private void eval(Scanner scanner, Config config, JShell shell, StringOutputStream out) {
68+
private void eval(Scanner processIn, PrintStream processOut, Config config, JShell shell, StringOutputStream jshellOut) {
7069
TimeoutWatcher watcher = new TimeoutWatcher(config.evalTimeoutSeconds(), shell::stop);
71-
int lineCount = Integer.parseInt(scanner.nextLine());
72-
String code = IntStream.range(0, lineCount).mapToObj(i -> scanner.nextLine()).collect(Collectors.joining("\n"));
73-
ok();
70+
int lineCount = Integer.parseInt(processIn.nextLine());
71+
String code = IntStream.range(0, lineCount).mapToObj(i -> processIn.nextLine()).collect(Collectors.joining("\n"));
72+
ok(processOut);
7473
watcher.start();
7574
List<SnippetEvent> events = shell.eval(code);
7675
watcher.stop();
@@ -99,16 +98,16 @@ private void eval(Scanner scanner, Config config, JShell shell, StringOutputStre
9998
} else {
10099
result.add(sanitize(event.exception().getClass().getName() + ":" + event.exception().getMessage()));
101100
}
102-
result.add(String.valueOf(out.isOverflow()));
103-
result.add(sanitize(out.readAll()));
101+
result.add(String.valueOf(jshellOut.isOverflow()));
102+
result.add(sanitize(jshellOut.readAll()));
104103
if(event.status() == Snippet.Status.REJECTED) {
105104
result.addAll(shell.diagnostics(event.snippet()).map(d -> sanitize(d.getMessage(Locale.ENGLISH))).toList());
106105
}
107106
result.add("");
108107
}
109108
}
110109
for(String line : result) {
111-
System.out.println(line);
110+
processOut.println(line);
112111
}
113112
}
114113

@@ -124,10 +123,10 @@ private void eval(Scanner scanner, Config config, JShell shell, StringOutputStre
124123
* empty line<br>
125124
* </code>
126125
*/
127-
private void snippets(JShell shell) {
128-
ok();
129-
shell.snippets().map(Snippet::source).map(JShellWrapper::sanitize).forEach(System.out::println);
130-
System.out.println();
126+
private void snippets(PrintStream processOut, JShell shell) {
127+
ok(processOut);
128+
shell.snippets().map(Snippet::source).map(JShellWrapper::sanitize).forEach(processOut::println);
129+
processOut.println();
131130
}
132131

133132
private static String sanitize(String s) {

0 commit comments

Comments
 (0)