How to save and load history between invocations of Scala JLine

冷暖自知 提交于 2019-12-10 09:37:47

问题


I'm using Scala JLine in my CLI program. It's working fine, but it forgets my history every time I restart my program. I see a class called FileHistory, and I see the ConsoleReader class has a method called setHistory() which takes an instance of FileHistory. I would expect calling that method would cause it to create or load and save a file containing my history. But it doesn't.

Unfortunately the documentation is nigh nonexistent. How can I make it so the next time I run my JLine-enabled program it remembers the commands that I had typed in the previous run?

Update

Correct answer given below by mirandes. Thanks to mirandes and som-snytt both for their helpful (yea solvent) responses.


回答1:


This worked for me:

import scala.tools.jline.console.ConsoleReader
import scala.tools.jline.console.history.FileHistory
import java.io.File

val reader : ConsoleReader = new ConsoleReader() 

val history = new FileHistory(new File(".history"))
reader.setHistory(history) 

Before exiting the app, make sure you flush the history.

reader.getHistory.asInstanceOf[FileHistory].flush()



回答2:


There's a comment. I thought you said there wasn't any documentation?

/**
 * {@link History} using a file for persistent backing.
 * <p/>
 * Implementers should install shutdown hook to call {@link FileHistory#flush}
 * to save history to disk.
 *
 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
 * @since 2.0
 */
public class FileHistory

Compare to Scala REPL internal history.



来源:https://stackoverflow.com/questions/17935184/how-to-save-and-load-history-between-invocations-of-scala-jline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!