aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/com/jantuomi/tunkki/repl/Repl.java
blob: 817d74632f5199c5710844786d199306319bcf2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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()));
    }
}