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
|
|
|
|
//
|
2022-08-05 00:45:19 +02:00
|
|
|
// 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.
|
2022-08-05 14:48:31 +02:00
|
|
|
// Text entry is completed with CTRL+D and aborted with CTRL+C or Escape.
|
2022-07-13 15:55:36 +02:00
|
|
|
//
|
2022-08-05 00:45:19 +02:00
|
|
|
// $ 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:
|
2022-08-05 00:45:19 +02:00
|
|
|
switch msg.String() {
|
|
|
|
case "ctrl+c":
|
2022-07-31 03:41:18 +02:00
|
|
|
m.aborted = true
|
2022-08-05 00:45:19 +02:00
|
|
|
m.quitting = true
|
|
|
|
return m, tea.Quit
|
|
|
|
case "esc", "ctrl+d":
|
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
|
|
|
|
}
|