fix(input): ctrl+c exit status 130

This commit is contained in:
Maas Lalani 2022-07-30 21:29:09 -04:00
parent d324abeab5
commit 0baa0b98bd
No known key found for this signature in database
GPG key ID: 5A6ED5CBF1A0A000
2 changed files with 21 additions and 5 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/alecthomas/kong"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/gum/internal/exit"
"github.com/charmbracelet/gum/internal/stdin"
"github.com/charmbracelet/gum/style"
)
@ -33,9 +34,18 @@ func (o Options) Run() error {
i.EchoCharacter = '•'
}
p := tea.NewProgram(model{i}, tea.WithOutput(os.Stderr))
m, err := p.StartReturningModel()
fmt.Println(m.(model).textinput.Value())
p := tea.NewProgram(model{
textinput: i,
aborted: false,
}, tea.WithOutput(os.Stderr))
tm, err := p.StartReturningModel()
m := tm.(model)
if m.aborted {
os.Exit(exit.Aborted)
}
fmt.Println(m.textinput.Value())
return err
}

View file

@ -13,7 +13,10 @@ import (
tea "github.com/charmbracelet/bubbletea"
)
type model struct{ textinput textinput.Model }
type model struct {
textinput textinput.Model
aborted bool
}
func (m model) Init() tea.Cmd { return textinput.Blink }
func (m model) View() string { return m.textinput.View() }
@ -21,7 +24,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEscape, tea.KeyCtrlC, tea.KeyEnter:
case tea.KeyEscape, tea.KeyCtrlC:
m.aborted = true
fallthrough
case tea.KeyEnter:
return m, tea.Quit
}
}