Skip to content
Myr

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 file

Take it

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

lines 1–10why, one step back
1// 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 back
11 "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 back
16)
1718type keymap textinput.KeyMap1920func defaultKeymap() keymap {

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

line 21why, one step back
21 k := textinput.DefaultKeyMap()

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

lines 22–40why, one step back
22 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 string

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

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

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

lines 42–44why, one step back
42 headerStyle lipgloss.Style
43 textinput textinput.Model44 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 45why, one step back
45 submitted bool

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

lines 46–50why, one step back
46 showHelp bool
47 help help.Model48 keymap keymap49}50

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

line 51why, one step back
51func (m model) Init() tea.Cmd { return textinput.Blink }

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

line 52why, one step back
52

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

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

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

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

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

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

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

line 56why, one step back
56 }

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

line 57why, one step back
57 var parts []string

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

line 58why, one step back
58 if m.header != "" {

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

line 59why, one step back
59 parts = append(parts, m.headerStyle.Render(m.header))

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

lines 60–61why, one step back
60 }
61

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

lines 62–64why, one step back
62 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 back
65 }

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

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

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

lines 67–70why, one step back
67 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 back
71 )))
72 v.ReportFocus = true73 return v

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

lines 74–79why, one step back
74}
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 back
80 m.textinput.SetWidth(msg.Width - 1 -

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

line 81why, one step back
81 lipgloss.Width(m.textinput.Prompt) -

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

line 82why, one step back
82 m.padding[1] - m.padding[3])

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

line 83why, one step back
83 }

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

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

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

line 85why, one step back
85 switch msg.String() {

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

line 86why, one step back
86 case "ctrl+c":

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

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

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

line 88why, one step back
88 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

line 89why, one step back
89 case "esc":

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

line 90why, one step back
90 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

lines 91–94why, one step back
91 return m, tea.Quit
92 case "enter":93 m.quitting = true94 m.submitted = true

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

lines 95–102why, one step back
95 return m, tea.Quit
96 }97 }9899 var cmd tea.Cmd100 m.textinput, cmd = m.textinput.Update(msg)101 return m, cmd102}