feat(input): header values

This commit is contained in:
Maas Lalani 2022-12-13 15:05:10 -05:00
parent 2d54d5394e
commit 240e163f01
4 changed files with 28 additions and 5 deletions

View file

@ -37,8 +37,10 @@ func (o Options) Run() error {
}
p := tea.NewProgram(model{
textinput: i,
aborted: false,
textinput: i,
aborted: false,
header: o.Header,
headerStyle: o.HeaderStyle.ToLipgloss(),
}, tea.WithOutput(os.Stderr))
tm, err := p.Run()
if err != nil {

View file

@ -10,23 +10,41 @@ package input
import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type model struct {
textinput textinput.Model
aborted bool
header string
headerStyle lipgloss.Style
textinput textinput.Model
quitting bool
aborted bool
}
func (m model) Init() tea.Cmd { return textinput.Blink }
func (m model) View() string { return m.textinput.View() }
func (m model) View() string {
if m.quitting {
return ""
}
if m.header != "" {
header := m.headerStyle.Render(m.header)
return lipgloss.JoinVertical(lipgloss.Left, header, m.textinput.View())
}
return m.textinput.View()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc":
m.quitting = true
m.aborted = true
return m, tea.Quit
case "enter":
m.quitting = true
return m, tea.Quit
}
}

View file

@ -12,4 +12,6 @@ type Options struct {
CharLimit int `help:"Maximum value length (0 for no limit)" default:"400"`
Width int `help:"Input width" default:"40" env:"GUM_INPUT_WIDTH"`
Password bool `help:"Mask input characters" default:"false"`
Header string `help:"Header value" default:"" env:"GUM_INPUT_HEADER"`
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_INPUT_HEADER_"`
}

View file

@ -36,6 +36,7 @@ func (m model) View() string {
return m.textarea.View()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg: