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

[Feature Request] Call arbitrary code on exit

Open asdine opened this issue 3 years ago • 1 comments

Feature Request

Currently, it seems that there is no way to execute some code between the moment the program receives an exit signal / ctrl D and the call to os.Exit(code). It would be very useful to be able to call some arbitrary code right before termination (e.g. closing resources, displaying a last message, flushing history, etc.)

asdine avatar Sep 20 '20 18:09 asdine

This can be achieved using Go's defer. When you exit the program Go makes sure to call any functions pushed on to the stack via defer and so you can do something like so:

func handleExit() {
	fmt.Println("Exiting...")
}

func main() {
	defer handleExit()
	p := prompt.New(
		executor,
		completer,
	)
	p.Run()
}

Omar-V2 avatar Apr 29 '21 01:04 Omar-V2