Skip to content

1ight181/comptplus-ctrl-c

 
 

Repository files navigation

Русская версия

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.

Supported Signals

  • SIGINT
  • SIGTERM
  • SIGQUIT

Usage Example

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
}

Community

Email: danil.odinzov181@gmail.com

About

cobra-prompt-comptplus with custom interrupt signals handling

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Go 100.0%