How to save and load history between calls to Scala JLine

I am using Scala JLine in my CLI program. It works great, but it forgets my story every time I restart my program. I see a class called FileHistory , and I see that the ConsoleReader class has a method called setHistory() that accepts an instance of FileHistory . I would expect a call to this method to make it create or load and save a file containing my history. But this is not so.

Unfortunately, the documentation is almost absent. How can I do this, since the next time I run my program with JLine support, it remembers the commands that I typed in the previous run?

Update

The correct answer given below is Miranda. Thanks mirandes and som-snytt for both their useful (yes solvents) answers.

+4
source share
2 answers

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 application, make sure that you reset the history.

 reader.getHistory.asInstanceOf[FileHistory].flush() 
+5
source

There is a comment. I thought you said that there is no 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 with Scala's internal history of the REPL .

+1
source

Source: https://habr.com/ru/post/1494153/


All Articles