input.go
This file
Path
input/input.go
100 lines · 2.2 KiB · Go
Last changed by
feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
History of this fileTake it
refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 1–40why, 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 (11 "github.com/charmbracelet/bubbles/help"12 "github.com/charmbracelet/bubbles/key"13 "github.com/charmbracelet/bubbles/textinput"14 tea "github.com/charmbracelet/bubbletea"15 "github.com/charmbracelet/lipgloss"16)1718type keymap textinput.KeyMap1920func defaultKeymap() keymap {21 k := textinput.DefaultKeyMap22 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
lines 52–56why, one step back5253func (m model) View() string {54 if m.quitting {55 return ""56 }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 }feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
lines 66–71why, one step back66 return lipgloss.NewStyle().67 Padding(m.padding...).68 Render(lipgloss.JoinVertical(69 lipgloss.Top,70 parts...,71 ))refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 72–77why, one step back72}7374func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {75 switch msg := msg.(type) {76 case tea.WindowSizeMsg:77 if m.autoWidth {feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
lines 78–80why, one step back78 m.textinput.Width = msg.Width - 1 -79 lipgloss.Width(m.textinput.Prompt) -80 m.padding[1] - m.padding[3]refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 81–83why, one step back81 }82 case tea.KeyMsg:83 switch msg.String() {feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 84why, one step back84 case "ctrl+c":refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 85why, one step back85 m.quitting = truefeat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 86why, one step back86 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 87why, one step back87 case "esc":refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 88why, one step back88 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 89–92why, one step back89 return m, tea.Quit90 case "enter":91 m.quitting = true92 m.submitted = truerefactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
lines 93–100why, one step back93 return m, tea.Quit94 }95 }9697 var cmd tea.Cmd98 m.textinput, cmd = m.textinput.Update(msg)99 return m, cmd100}