gum/write/write.go

63 lines
1.5 KiB
Go
Raw Normal View History

// 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.
// Text entry is completed with CTRL+D and aborted with CTRL+C or Escape.
//
2022-08-05 00:45:19 +02:00
// $ gum write > output.text
package write
import (
"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type model struct {
autoWidth bool
aborted bool
header string
headerStyle lipgloss.Style
quitting bool
textarea textarea.Model
}
func (m model) Init() tea.Cmd { return textarea.Blink }
func (m model) View() string {
if m.quitting {
return ""
}
// Display the header above the text area if it is not empty.
if m.header != "" {
header := m.headerStyle.Render(m.header)
return lipgloss.JoinVertical(lipgloss.Left, header, m.textarea.View())
}
return m.textarea.View()
}
2022-12-13 21:05:10 +01:00
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
if m.autoWidth {
m.textarea.SetWidth(msg.Width)
}
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 "ctrl+d", "esc":
m.quitting = true
return m, tea.Quit
}
}
var cmd tea.Cmd
m.textarea, cmd = m.textarea.Update(msg)
return m, cmd
}