go-prompt
go-prompt copied to clipboard
Provide a way to retrieve history
I don't see a way to retrieve the history, only a way to set it at program startup.
I would like to persist history between sessions. I can implement persistence/restore if I can access the history. It would be great if go-prompt provided a way to save/load history from a file though.
You can persist histories between sessions via OptionHistory. When executor is called, you save a command in something log file you created. And when starting your program, please open a log file, extract executed commands and pass it to OptionHistory.
Yeah, I was thinking of doing that in the mean time. I was merely making an enhancement request for something more automated. :)
e.g.
func OptionLoadHistory(in io.Reader){
}
func OptionSaveHistory(out io.Writer) {
}
Then prompt.Run() can load lines into history from in at the beginning and write current history to out before returning.
@c-bata please advise, thanks.
I know this issue is quite old but stumbled across the same thing. I can set OptionHistory in order to load history strings as array but there is no way to change the behavior during a shell instantiation.
E.g.
prompt.OptionHistory([]string{"a", "b"})
I'd also expect there is a deferred function which will execute when the application exits or after each command. So a OptionSaveHistory function which takes the last command as string would be great.
That way you could implement the storage of the command history yourself (independent from a file system). Saving to databases would be no problem that way.
In my case I use cobra-shell, which allows passing Options but is static in itself. So I would not want to change cobra-shell but rather use the Option flags.