Skip to content

Commit 6682ff0

Browse files
fix: Fix Ctrl+C to stop it
1 parent f06324d commit 6682ff0

File tree

1 file changed

+13
-5
lines changed
  • src/main/java/net/marcellperger/mathexpr/interactive

1 file changed

+13
-5
lines changed

src/main/java/net/marcellperger/mathexpr/interactive/Shell.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.jetbrains.annotations.Nullable;
99

1010
import java.io.PrintStream;
11+
import java.util.NoSuchElementException;
1112

1213
public class Shell {
1314
Input in;
@@ -22,15 +23,22 @@ public static void main(String[] args) {
2223
new Shell().run();
2324
}
2425

25-
// TODO run() until exit better - parse exit command/Ctrl+C
26+
// TODO run() until exit better - parse exit command, more robust/extensible command handling system
2627

2728
public void run() {
28-
//noinspection InfiniteLoopStatement
29-
while (true) getAndRunCommand();
29+
//noinspection StatementWithEmptyBody
30+
while (getAndRunCommand()) {}
3031
}
3132

32-
public void getAndRunCommand() {
33-
runCommand(in.getInput(">? "));
33+
public /*returns wantMore */boolean getAndRunCommand() {
34+
String inp;
35+
try {
36+
inp = in.getInput(">? ");
37+
} catch (NoSuchElementException e) {
38+
return false;
39+
}
40+
runCommand(inp);
41+
return true;
3442
}
3543
public void runCommand(String cmd) {
3644
@Nullable MathSymbol sym = parseCmdOrPrintError(cmd);

0 commit comments

Comments
 (0)