Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make sure the confirm printer can clean up after Ctrl+C #425

Merged
merged 1 commit into from Dec 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions interactive_confirm_printer.go
Expand Up @@ -2,12 +2,12 @@ package pterm

import (
"fmt"
"os"
"strings"

"atomicgo.dev/cursor"
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
"github.com/pterm/pterm/internal"
)

var (
Expand Down Expand Up @@ -92,6 +92,11 @@ func (p InteractiveConfirmPrinter) WithSuffixStyle(style *Style) *InteractiveCon
// result, _ := pterm.DefaultInteractiveConfirm.Show("Are you sure?")
// pterm.Println(result)
func (p InteractiveConfirmPrinter) Show(text ...string) (bool, error) {
// should be the first defer statement to make sure it is executed last
// and all the needed cleanup can be done before
cancel, exit := internal.NewCancelationSignal()
defer exit()

var result bool

if len(text) == 0 || text[0] == "" {
Expand All @@ -101,6 +106,7 @@ func (p InteractiveConfirmPrinter) Show(text ...string) (bool, error) {
p.TextStyle.Print(text[0] + " " + p.getSuffix() + ": ")
y, n := p.getShortHandles()

var interrupted bool
err := keyboard.Listen(func(keyInfo keys.Key) (stop bool, err error) {
key := keyInfo.Code
char := strings.ToLower(keyInfo.String())
Expand Down Expand Up @@ -132,12 +138,15 @@ func (p InteractiveConfirmPrinter) Show(text ...string) (bool, error) {
result = p.DefaultValue
return true, nil
case keys.CtrlC:
os.Exit(1)
cancel()
interrupted = true
return true, nil
}
return false, nil
})
cursor.StartOfLine()
if !interrupted {
cursor.StartOfLine()
}
return result, err
}

Expand Down