Skip to content
Myr

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 file

Take it

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

lines 1–40why, 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 (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 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

lines 52–56why, one step back
52
53func (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 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 }

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

lines 66–71why, one step back
66 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 back
72}
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 back
78 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 back
81 }
82 case tea.KeyMsg:83 switch msg.String() {

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

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

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

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

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

line 86why, one step back
86 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 87why, one step back
87 case "esc":

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

line 88why, one step back
88 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 89–92why, one step back
89 return m, tea.Quit
90 case "enter":91 m.quitting = true92 m.submitted = true

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

lines 93–100why, one step back
93 return m, tea.Quit
94 }95 }9697 var cmd tea.Cmd98 m.textinput, cmd = m.textinput.Update(msg)99 return m, cmd100}