mirror of
https://github.com/charmbracelet/gum.git
synced 2024-10-26 07:10:39 +03:00
46328de806
Change ESC from aborting to successful quitting. 'vi' users press ESC as an uncontrollable tick, making using 'gum write' painful when all their work is lost.
63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
// 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.
|
|
// Text entry is completed with CTRL+D and aborted with CTRL+C or Escape.
|
|
//
|
|
// $ 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()
|
|
}
|
|
|
|
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:
|
|
switch msg.String() {
|
|
case "ctrl+c":
|
|
m.aborted = true
|
|
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
|
|
}
|