package com.facebook.presto.cli;

import io.airlift.command.Command;
import io.airlift.command.Option;
import jline.console.ConsoleReader;
import jline.console.history.MemoryHistory;

import java.io.Closeable;
import java.io.IOException;
import java.net.URI;

@Command(name = "console", description = "Interactive console")
public class Console
        implements Runnable
{
    @Option(name = "-s", title = "server")
    public URI server = URI.create("http://localhost:8080/v1/presto/query");

    @Override
    public void run()
    {
        MemoryHistory history = new MemoryHistory();
        try (LineReader reader = new LineReader()) {
            reader.setHistory(history);

            while (true) {
                String line = reader.readLine("presto> ");
                if (line == null) {
                    return;
                }

                line = line.trim();
                if (line.isEmpty()) {
                    continue;
                }

                try {
                    if (!process(line)) {
                        return;
                    }
                }
                catch (Exception e) {
                    System.out.println("error running command");
                }
            }
        }
        catch (IOException e) {
            System.err.println("readline error: " + e.getMessage());
        }
    }

    private boolean process(String line)
    {
        if (line.endsWith(";")) {
            line = line.substring(0, line.length() - 1);
        }

        if (line.equalsIgnoreCase("quit")) {
            return false;
        }

        if (line.toLowerCase().startsWith("select ")) {
            line = "sql:" + line;
        }

        Execute query = new Execute();
        query.server = server;
        query.query = line;
        query.run();

        return true;
    }

    private static class LineReader
            extends ConsoleReader
            implements Closeable
    {
        private LineReader()
                throws IOException
        {
            super();
            setExpandEvents(false);
        }

        @Override
        public void close()
        {
            shutdown();
        }
    }
}

@@ -165,97 +165,281 @@
 ory.
-MemoryHistory;%0A%0Aimport java.io.Closeable;%0Aimport java.io.IOException;%0Aimport java.net.URI
+FileHistory;%0Aimport jline.console.history.History;%0Aimport jline.console.history.MemoryHistory;%0A%0Aimport java.io.Closeable;%0Aimport java.io.File;%0Aimport java.io.Flushable;%0Aimport java.io.IOException;%0Aimport java.net.URI;%0A%0Aimport static jline.internal.Configuration.getUserHome
 ;%0A%0A@
@@ -727,146 +727,64 @@
     
-MemoryHistory history = new MemoryHistory();%0A        try (LineReader reader = new LineReader()) %7B%0A            reader.setHistory(history);%0A
+try (LineReader reader = new LineReader(getHistory())) %7B
 %0A   
@@ -1943,32 +1943,621 @@
 rn true;%0A    %7D%0A%0A
+    private static MemoryHistory getHistory()%0A    %7B%0A        MemoryHistory history;%0A        File historyFile = new File(getUserHome(), %22.presto_history%22);%0A        try %7B%0A            history = new FileHistory(historyFile);%0A        %7D%0A        catch (IOException e) %7B%0A            System.err.printf(%22WARNING: Failed to load history file (%25s): %25s. %22 +%0A                    %22History will not be available during this session.%25n%22,%0A                    historyFile, e.getMessage());%0A            history = new MemoryHistory();%0A        %7D%0A        history.setAutoTrim(true);%0A        return history;%0A    %7D%0A%0A
     private stat
@@ -2676,16 +2676,31 @@
 eReader(
+History history
 )%0A      
@@ -2791,24 +2791,393 @@
 nts(false);%0A
+            setHistory(history);%0A        %7D%0A%0A        @Override%0A        public String readLine(String prompt, Character mask)%0A                throws IOException%0A        %7B%0A            String line = super.readLine(prompt, mask);%0A            if (getHistory() instanceof Flushable) %7B%0A                ((Flushable) getHistory()).flush();%0A            %7D%0A            return line;%0A
         %7D%0A%0A 

Store history persistently