JLine调用之间保存和加载历史记录

JLine调用之间保存和加载历史记录

本文介绍了如何在Scala JLine调用之间保存和加载历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用 Scala我的CLI程序中的JLine .它工作正常,但是每次重新启动程序时,它都会忘记我的历史记录.我看到一个名为FileHistory的类,并且我看到ConsoleReader类具有一个名为setHistory()的方法,该方法采用一个FileHistory的实例.我希望调用该方法将导致它创建或加载并保存包含我的历史记录的文件.但事实并非如此.

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.

不幸的是,该文献几乎不存在.如何做到这一点,以便下次运行启用JLine的程序时,它会记住上一次运行时键入的命令?

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?

下面是米兰多给出的正确答案.感谢mirandes和som-snytt都做出了有益的(肯定的)回应.

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

推荐答案

这对我有用:

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()

这篇关于如何在Scala JLine调用之间保存和加载历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:39