This is a fork of comptplus. The original package is based on go-prompt, which—like the original go-prompt—does not support custom interrupt handling. This fork solves that issue by using go-prompt-ctrl-c internally, which introduces a new constructor option WithInterruptCallback. This option allows you to pass a function that will be invoked when interrupt signals are received.
SIGINTSIGTERMSIGQUIT
package main
import (
"fmt"
cobraprompt "github.com/1ight181/comptplus-ctrl-c"
prompt "github.com/1ight181/go-prompt-ctrl-c"
"github.com/spf13/cobra"
)
func main() {
rootCmd := &cobra.Command{
SilenceUsage: true,
SilenceErrors: true,
Short: "root cmd example",
}
cobraPrompt := cobraprompt.CobraPrompt{
RootCmd: rootCmd,
ShowHelpCommandAndFlags: true,
GoPromptOptions: []prompt.Option{
prompt.WithTitle("example 0.1"),
prompt.WithMaxSuggestion(5),
prompt.WithPrefix("example> "),
prompt.WithInterruptCallback(interruptCallback),
},
}
cobraPrompt.Run()
}
// Custom Ctrl+C handling
func interruptCallback(code int) {
fmt.Println("\nCtrl+C pressed, but we handle it ourselves")
// os.Exit(code) // Default behavior
}Email: danil.odinzov181@gmail.com