go-prompt icon indicating copy to clipboard operation
go-prompt copied to clipboard

Provide a way to retrieve history

Open sandipb opened this issue 7 years ago • 4 comments

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.

sandipb avatar Sep 12 '18 01:09 sandipb

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.

c-bata avatar Sep 12 '18 01:09 c-bata

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.

sandipb avatar Sep 12 '18 07:09 sandipb

@c-bata please advise, thanks.

rohityadavcloud avatar Oct 20 '18 20:10 rohityadavcloud

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.

cmprmsd avatar May 02 '22 13:05 cmprmsd