Skip to content
Myr

write.go

This file

Path

write/write.go

204 lines · 4.3 KiB · Go

Last changed by

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

History of this file

Take it

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 1–14why, one step back
1// Package write provides a shell script interface for the text area bubble.
2// https://github.com/charmbracelet/bubbles/tree/master/textarea3//4// It can be used to ask the user to write some long form of text (multi-line)5// input. The text the user entered will be sent to stdout.6// Text entry is completed with CTRL+D and aborted with CTRL+C or Escape.7//8// $ gum write > output.text9package write1011import (12 "io"13 "os"14

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

lines 15–19why, one step back
15 "charm.land/bubbles/v2/help"
16 "charm.land/bubbles/v2/key"17 "charm.land/bubbles/v2/textarea"18 tea "charm.land/bubbletea/v2"19 "charm.land/lipgloss/v2"

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 20–25why, one step back
20 "github.com/charmbracelet/x/editor"
21)2223type keymap struct {24 textarea.KeyMap25 Submit key.Binding

feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a

line 26why, one step back
26 Quit key.Binding

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

line 27why, one step back
27 Abort key.Binding

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 28–43why, one step back
28 OpenInEditor key.Binding
29}3031// FullHelp implements help.KeyMap.32func (k keymap) FullHelp() [][]key.Binding { return nil }3334// ShortHelp implements help.KeyMap.35func (k keymap) ShortHelp() []key.Binding {36 return []key.Binding{37 k.InsertNewline,38 k.OpenInEditor,39 k.Submit,40 }41}4243func defaultKeymap() keymap {

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

line 44why, one step back
44 km := textarea.DefaultKeyMap()

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 45–50why, one step back
45 km.InsertNewline = key.NewBinding(
46 key.WithKeys("ctrl+j"),47 key.WithHelp("ctrl+j", "insert newline"),48 )49 return keymap{50 KeyMap: km,

feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a

lines 51–54why, one step back
51 Quit: key.NewBinding(
52 key.WithKeys("esc"),53 key.WithHelp("esc", "quit"),54 ),

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

line 55why, one step back
55 Abort: key.NewBinding(

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 56–74why, one step back
56 key.WithKeys("ctrl+c"),
57 key.WithHelp("ctrl+c", "cancel"),58 ),59 OpenInEditor: key.NewBinding(60 key.WithKeys("ctrl+e"),61 key.WithHelp("ctrl+e", "open editor"),62 ),63 Submit: key.NewBinding(64 key.WithKeys("enter"),65 key.WithHelp("enter", "submit"),66 ),67 }68}6970type model struct {71 autoWidth bool72 header string73 headerStyle lipgloss.Style74 quitting bool

feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a

line 75why, one step back
75 submitted bool

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 76–79why, one step back
76 textarea textarea.Model
77 showHelp bool78 help help.Model79 keymap keymap

feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525

line 80why, one step back
80 padding []int

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 81–83why, one step back
81}
8283func (m model) Init() tea.Cmd { return textarea.Blink }

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

line 84why, one step back
84

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

line 85why, one step back
85func (m model) View() tea.View {

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

line 86why, one step back
86 if m.quitting {

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

line 87why, one step back
87 return tea.NewView("")

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 88–97why, one step back
88 }
8990 var parts []string9192 // Display the header above the text area if it is not empty.93 if m.header != "" {94 parts = append(parts, m.headerStyle.Render(m.header))95 }96 parts = append(parts, m.textarea.View())97 if m.showHelp {

fix: make empty line before help consistentCarlos Alexandro Beckerbefore adoption4fea9a0

line 98why, one step back
98 parts = append(parts, "", m.help.View(m.keymap))

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

line 99why, one step back
99 }

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

line 100why, one step back
100 v := tea.NewView(lipgloss.NewStyle().

feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525

lines 101–104why, one step back
101 Padding(m.padding...).
102 Render(lipgloss.JoinVertical(103 lipgloss.Left,104 parts...,

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

lines 105–107why, one step back
105 )))
106 v.ReportFocus = true107 return v

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 108–113why, one step back
108}
109110func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {111 switch msg := msg.(type) {112 case tea.WindowSizeMsg:113 if m.autoWidth {

feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525

line 114why, one step back
114 m.textarea.SetWidth(msg.Width - m.padding[1] - m.padding[3])

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 115–124why, one step back
115 }
116 case tea.FocusMsg, tea.BlurMsg:117 var cmd tea.Cmd118 m.textarea, cmd = m.textarea.Update(msg)119 return m, cmd120 case startEditorMsg:121 return m, openEditor(msg.path, msg.lineno)122 case editorFinishedMsg:123 if msg.err != nil {124 m.quitting = true

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

line 125why, one step back
125 return m, tea.Interrupt

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 126–127why, one step back
126 }
127 m.textarea.SetValue(msg.content)

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

line 128why, one step back
128 case tea.KeyPressMsg:

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 129–130why, one step back
129 km := m.keymap
130 switch {

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

line 131why, one step back
131 case key.Matches(msg, km.Abort):

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

line 132why, one step back
132 m.quitting = true

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

line 133why, one step back
133 return m, tea.Interrupt

feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a

lines 134–136why, one step back
134 case key.Matches(msg, km.Quit):
135 m.quitting = true136 return m, tea.Quit

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 137–138why, one step back
137 case key.Matches(msg, km.Submit):
138 m.quitting = true

feat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a

line 139why, one step back
139 m.submitted = true

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 140–141why, one step back
140 return m, tea.Quit
141 case key.Matches(msg, km.OpenInEditor):

fix: write was not compilingAndrey Neringbefore adoption057099c

line 142why, one step back
142 return m, createTempFile(m.textarea.Value(), m.textarea.Line()+1)

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 143–152why, one step back
143 }
144 }145146 var cmd tea.Cmd147 m.textarea, cmd = m.textarea.Update(msg)148 return m, cmd149}150151type startEditorMsg struct {152 path string

fix: write was not compilingAndrey Neringbefore adoption057099c

line 153why, one step back
153 lineno int

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 154–160why, one step back
154}
155156type editorFinishedMsg struct {157 content string158 err error159}160

fix: write was not compilingAndrey Neringbefore adoption057099c

line 161why, one step back
161func createTempFile(content string, lineno int) tea.Cmd {

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 162–178why, one step back
162 return func() tea.Msg {
163 f, err := os.CreateTemp("", "gum.*.md")164 if err != nil {165 return editorFinishedMsg{err: err}166 }167 _, err = io.WriteString(f, content)168 if err != nil {169 return editorFinishedMsg{err: err}170 }171 _ = f.Close()172 return startEditorMsg{173 path: f.Name(),174 lineno: lineno,175 }176 }177}178

fix: write was not compilingAndrey Neringbefore adoption057099c

line 179why, one step back
179func openEditor(path string, lineno int) tea.Cmd {

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

lines 180–204why, one step back
180 cb := func(err error) tea.Msg {
181 if err != nil {182 return editorFinishedMsg{183 err: err,184 }185 }186 bts, err := os.ReadFile(path)187 if err != nil {188 return editorFinishedMsg{err: err}189 }190 return editorFinishedMsg{191 content: string(bts),192 }193 }194 cmd, err := editor.Cmd(195 "Gum",196 path,197 editor.LineNumber(lineno),198 editor.EndOfLine(),199 )200 if err != nil {201 return func() tea.Msg { return cb(err) }202 }203 return tea.ExecProcess(cmd, cb)204}