fix(write): Hide textarea when quitting

When the user is done editing the text in the textarea this change hides the textarea and prints only the value to not clutter the terminal.
This commit is contained in:
Maas Lalani 2022-07-11 17:16:30 -04:00
parent 11e7e18256
commit 66ef277036
No known key found for this signature in database
GPG key ID: 5A6ED5CBF1A0A000
2 changed files with 12 additions and 3 deletions

View file

@ -32,7 +32,7 @@ func (o Options) Run() {
a.SetHeight(o.Height)
a.SetValue(o.Value)
p := tea.NewProgram(model{a}, tea.WithOutput(os.Stderr))
p := tea.NewProgram(model{textarea: a}, tea.WithOutput(os.Stderr))
m, _ := p.StartReturningModel()
fmt.Println(m.(model).textarea.Value())
}

View file

@ -5,15 +5,24 @@ import (
tea "github.com/charmbracelet/bubbletea"
)
type model struct{ textarea textarea.Model }
type model struct {
quitting bool
textarea textarea.Model
}
func (m model) Init() tea.Cmd { return textarea.Blink }
func (m model) View() string { return m.textarea.View() }
func (m model) View() string {
if m.quitting {
return ""
}
return m.textarea.View()
}
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:
m.quitting = true
return m, tea.Quit
}
}