aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/com/jantuomi/tunkki/repl/Repl.java
diff options
context:
space:
mode:
authorJan Tuomi <jan-sebastian.tuomi@aalto.fi>2016-07-31 22:50:34 +0300
committerJan Tuomi <jan-sebastian.tuomi@aalto.fi>2016-07-31 22:50:34 +0300
commit20a3fb57ae9ee1e70a2d57169742d481eac82fe9 (patch)
tree7c464d9ecc6274a3b43762b17e2dacec2d267c30 /src/main/com/jantuomi/tunkki/repl/Repl.java
parentae7275e7ab7ea2b4599b613454d4c1149855bced (diff)
Implement enhanced REPL, relates to #13
Add command history and welcome message
Diffstat (limited to 'src/main/com/jantuomi/tunkki/repl/Repl.java')
-rw-r--r--src/main/com/jantuomi/tunkki/repl/Repl.java52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/main/com/jantuomi/tunkki/repl/Repl.java b/src/main/com/jantuomi/tunkki/repl/Repl.java
new file mode 100644
index 0000000..817d746
--- /dev/null
+++ b/src/main/com/jantuomi/tunkki/repl/Repl.java
@@ -0,0 +1,52 @@
+package com.jantuomi.tunkki.repl;
+
+import com.jantuomi.tunkki.Tunkki;
+import com.jantuomi.tunkki.exception.TunkkiError;
+import jline.console.*;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+/**
+ * Created by jan on 31.7.2016.
+ */
+public class Repl {
+
+ private Tunkki tunkki;
+
+ public Repl(Tunkki tunkki) {
+ this.tunkki = tunkki;
+ }
+
+ public void run() throws IOException, InterruptedException {
+ showWelcomeText();
+
+ ConsoleReader reader = new ConsoleReader();
+ reader.setBellEnabled(false);
+
+ String line;
+ PrintWriter out = new PrintWriter(System.out);
+
+ while ((line = readLine(reader, "")) != null) {
+ try {
+ tunkki.run(line);
+ }
+ catch (TunkkiError ex) {
+ ex.printStackTrace();
+ Thread.sleep(50);
+ }
+
+ out.flush();
+ }
+ }
+
+ private String readLine(ConsoleReader reader, String promptMessage)
+ throws IOException {
+ String line = reader.readLine(promptMessage + "\ntunkki> ");
+ return line.trim();
+ }
+
+ private void showWelcomeText() {
+ System.out.println(String.format("tunkki REPL version %s", tunkki.versionString()));
+ }
+}