input.go
This file
Path
input/input.go
102 lines · 2.2 KiB · Go
Last changed by
chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
History of this fileTake it
refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 1–10why, one step back1// Package input provides a shell script interface for the text input bubble.2// https://github.com/charmbracelet/bubbles/tree/master/textinput3//4// It can be used to prompt the user for some input. The text the user entered5// will be sent to stdout.6//7// $ gum input --placeholder "What's your favorite gum?" > answer.text8package input910import (chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 11–15why, one step back11 "charm.land/bubbles/v2/help"12 "charm.land/bubbles/v2/key"13 "charm.land/bubbles/v2/textinput"14 tea "charm.land/bubbletea/v2"15 "charm.land/lipgloss/v2"refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 16–20why, one step back16)1718type keymap textinput.KeyMap1920func defaultKeymap() keymap {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 21why, one step back21 k := textinput.DefaultKeyMap()refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 22–40why, one step back22 return keymap(k)23}2425// FullHelp implements help.KeyMap.26func (k keymap) FullHelp() [][]key.Binding { return nil }2728// ShortHelp implements help.KeyMap.29func (k keymap) ShortHelp() []key.Binding {30 return []key.Binding{31 key.NewBinding(32 key.WithKeys("enter"),33 key.WithHelp("enter", "submit"),34 ),35 }36}3738type model struct {39 autoWidth bool40 header stringfeat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 41why, one step back41 padding []intrefactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 42–44why, one step back42 headerStyle lipgloss.Style43 textinput textinput.Model44 quitting boolfeat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a
line 45why, one step back45 submitted boolrefactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 46–50why, one step back46 showHelp bool47 help help.Model48 keymap keymap49}50feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 51why, one step back51func (m model) Init() tea.Cmd { return textinput.Blink }refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 52why, one step back52chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 53why, one step back53func (m model) View() tea.View {refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 54why, one step back54 if m.quitting {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 55why, one step back55 return tea.NewView("")refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 56why, one step back56 }feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 57why, one step back57 var parts []stringrefactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 58why, one step back58 if m.header != "" {feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 59why, one step back59 parts = append(parts, m.headerStyle.Render(m.header))refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 60–61why, one step back60 }61feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
lines 62–64why, one step back62 parts = append(parts, m.textinput.View())63 if m.showHelp {64 parts = append(parts, "", m.help.View(m.keymap))refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 65why, one step back65 }chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 66why, one step back66 v := tea.NewView(lipgloss.NewStyle().feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
lines 67–70why, one step back67 Padding(m.padding...).68 Render(lipgloss.JoinVertical(69 lipgloss.Top,70 parts...,chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 71–73why, one step back71 )))72 v.ReportFocus = true73 return vrefactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 74–79why, one step back74}7576func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {77 switch msg := msg.(type) {78 case tea.WindowSizeMsg:79 if m.autoWidth {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 80why, one step back80 m.textinput.SetWidth(msg.Width - 1 -feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 81why, one step back81 lipgloss.Width(m.textinput.Prompt) -chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 82why, one step back82 m.padding[1] - m.padding[3])refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 83why, one step back83 }chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 84why, one step back84 case tea.KeyPressMsg:refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 85why, one step back85 switch msg.String() {feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 86why, one step back86 case "ctrl+c":refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 87why, one step back87 m.quitting = truefeat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 88why, one step back88 return m, tea.Interruptfeat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a
line 89why, one step back89 case "esc":refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 90why, one step back90 m.quitting = truefeat(choose,confirm,file,filter,input,table,write): esc exit 1, ctrl+c exit 130, help arrow order (#771)Carlos Alexandro Beckerbefore adoption774667a
lines 91–94why, one step back91 return m, tea.Quit92 case "enter":93 m.quitting = true94 m.submitted = truerefactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 95–102why, one step back95 return m, tea.Quit96 }97 }9899 var cmd tea.Cmd100 m.textinput, cmd = m.textinput.Update(msg)101 return m, cmd102}