mirror of
https://github.com/charmbracelet/gum
synced 2024-06-19 22:15:09 +02:00
feat(input): header values
This commit is contained in:
parent
2d54d5394e
commit
240e163f01
|
@ -37,8 +37,10 @@ func (o Options) Run() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
p := tea.NewProgram(model{
|
p := tea.NewProgram(model{
|
||||||
textinput: i,
|
textinput: i,
|
||||||
aborted: false,
|
aborted: false,
|
||||||
|
header: o.Header,
|
||||||
|
headerStyle: o.HeaderStyle.ToLipgloss(),
|
||||||
}, tea.WithOutput(os.Stderr))
|
}, tea.WithOutput(os.Stderr))
|
||||||
tm, err := p.Run()
|
tm, err := p.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -10,23 +10,41 @@ package input
|
||||||
import (
|
import (
|
||||||
"github.com/charmbracelet/bubbles/textinput"
|
"github.com/charmbracelet/bubbles/textinput"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
"github.com/charmbracelet/lipgloss"
|
||||||
)
|
)
|
||||||
|
|
||||||
type model struct {
|
type model struct {
|
||||||
textinput textinput.Model
|
header string
|
||||||
aborted bool
|
headerStyle lipgloss.Style
|
||||||
|
textinput textinput.Model
|
||||||
|
quitting bool
|
||||||
|
aborted bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) Init() tea.Cmd { return textinput.Blink }
|
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) {
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "ctrl+c", "esc":
|
case "ctrl+c", "esc":
|
||||||
|
m.quitting = true
|
||||||
m.aborted = true
|
m.aborted = true
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
case "enter":
|
case "enter":
|
||||||
|
m.quitting = true
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,4 +12,6 @@ type Options struct {
|
||||||
CharLimit int `help:"Maximum value length (0 for no limit)" default:"400"`
|
CharLimit int `help:"Maximum value length (0 for no limit)" default:"400"`
|
||||||
Width int `help:"Input width" default:"40" env:"GUM_INPUT_WIDTH"`
|
Width int `help:"Input width" default:"40" env:"GUM_INPUT_WIDTH"`
|
||||||
Password bool `help:"Mask input characters" default:"false"`
|
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_"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,7 @@ func (m model) View() string {
|
||||||
|
|
||||||
return m.textarea.View()
|
return m.textarea.View()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
|
|
Loading…
Reference in a new issue