2022-07-13 15:55:36 +02:00
|
|
|
// Package write provides a shell script interface for the text area bubble.
|
|
|
|
// https://github.com/charmbracelet/bubbles/tree/master/textarea
|
|
|
|
//
|
|
|
|
// It can be used to ask the user to write some long form of text
|
|
|
|
// (multi-line) input. The text the user entered will be sent to stdout.
|
|
|
|
//
|
|
|
|
// $ gum write > output.text
|
|
|
|
//
|
2022-07-06 20:38:40 +02:00
|
|
|
package write
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/charmbracelet/bubbles/textarea"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
2022-07-11 23:16:30 +02:00
|
|
|
type model struct {
|
2022-07-31 03:41:18 +02:00
|
|
|
aborted bool
|
2022-07-11 23:16:30 +02:00
|
|
|
quitting bool
|
|
|
|
textarea textarea.Model
|
|
|
|
}
|
2022-07-06 20:38:40 +02:00
|
|
|
|
|
|
|
func (m model) Init() tea.Cmd { return textarea.Blink }
|
2022-07-11 23:16:30 +02:00
|
|
|
func (m model) View() string {
|
|
|
|
if m.quitting {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return m.textarea.View()
|
|
|
|
}
|
2022-07-06 20:38:40 +02:00
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.Type {
|
2022-07-31 03:41:18 +02:00
|
|
|
case tea.KeyCtrlC:
|
|
|
|
m.aborted = true
|
|
|
|
fallthrough
|
|
|
|
case tea.KeyEscape, tea.KeyCtrlD:
|
2022-07-11 23:16:30 +02:00
|
|
|
m.quitting = true
|
2022-07-06 20:38:40 +02:00
|
|
|
return m, tea.Quit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmd tea.Cmd
|
|
|
|
m.textarea, cmd = m.textarea.Update(msg)
|
|
|
|
return m, cmd
|
|
|
|
}
|