[cli-repl] Small refactoring

Add `flushHistory` to ReplCommandReader
Pass `WhatNextAfterOneLine` to `readLine`
This commit is contained in:
Dmitry Kovanikov
2015-09-18 18:42:17 +03:00
committed by Pavel V. Talanov
parent 8aa701ec2d
commit e4f44d7754
4 changed files with 17 additions and 11 deletions
@@ -136,9 +136,7 @@ public class ReplFromTerminal {
}
finally {
try {
if (commandReader instanceof ConsoleReplCommandReader) {
((ConsoleReplCommandReader) commandReader).getFileHistory().flush();
}
commandReader.flushHistory();
}
catch (Exception e) {
replErrorLogger.logException(e);
@@ -146,7 +144,7 @@ public class ReplFromTerminal {
}
}
private enum WhatNextAfterOneLine {
public enum WhatNextAfterOneLine {
READ_LINE,
INCOMPLETE,
QUIT,
@@ -155,8 +153,7 @@ public class ReplFromTerminal {
@NotNull
private WhatNextAfterOneLine one(@NotNull WhatNextAfterOneLine next) {
try {
String prompt = next == WhatNextAfterOneLine.INCOMPLETE ? "... " : ">>> ";
String line = commandReader.readLine(prompt);
String line = commandReader.readLine(next);
if (line == null) {
return WhatNextAfterOneLine.QUIT;
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.cli.jvm.repl.reader
import jline.console.ConsoleReader
import jline.console.history.FileHistory
import org.jetbrains.kotlin.cli.jvm.repl.ReplFromTerminal
import java.io.File
public class ConsoleReplCommandReader : ReplCommandReader {
@@ -27,8 +28,10 @@ public class ConsoleReplCommandReader : ReplCommandReader {
history = FileHistory(File(File(System.getProperty("user.home")), ".kotlin_history"))
}
public val fileHistory: FileHistory
get() = consoleReader.history as FileHistory
override fun readLine(next: ReplFromTerminal.WhatNextAfterOneLine): String? {
val prompt = if (next == ReplFromTerminal.WhatNextAfterOneLine.INCOMPLETE) "... " else ">>> ";
return consoleReader.readLine(prompt)
}
override fun readLine(prompt: String?) = consoleReader.readLine(prompt)
override fun flushHistory() = (consoleReader.history as FileHistory).flush()
}
@@ -16,6 +16,9 @@
package org.jetbrains.kotlin.cli.jvm.repl.reader
import org.jetbrains.kotlin.cli.jvm.repl.ReplFromTerminal
public class IdeReplCommandReader : ReplCommandReader {
override fun readLine(prompt: String?) = readLine()
override fun readLine(next: ReplFromTerminal.WhatNextAfterOneLine) = readLine()
override fun flushHistory() = Unit
}
@@ -16,6 +16,9 @@
package org.jetbrains.kotlin.cli.jvm.repl.reader
import org.jetbrains.kotlin.cli.jvm.repl.ReplFromTerminal
public interface ReplCommandReader {
public fun readLine(prompt: String?): String?
public fun readLine(next: ReplFromTerminal.WhatNextAfterOneLine): String?
public fun flushHistory()
}