From 44cc74e49651c270cb085a141bc5bbba04d953aa Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Wed, 27 Jul 2022 13:56:52 -0400 Subject: [PATCH] feat: autoresize textinput --- input/command.go | 5 ++++- input/input.go | 12 +++++++++++- input/options.go | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/input/command.go b/input/command.go index ec529b8..01be0ad 100644 --- a/input/command.go +++ b/input/command.go @@ -31,7 +31,10 @@ func (o Options) Run() error { i.PromptStyle = o.PromptStyle.ToLipgloss() i.CursorStyle = o.CursorStyle.ToLipgloss() - p := tea.NewProgram(model{i}, tea.WithOutput(os.Stderr)) + p := tea.NewProgram(model{ + textinput: i, + autosize: o.Width == 0, + }, tea.WithOutput(os.Stderr)) m, err := p.StartReturningModel() fmt.Println(m.(model).textinput.Value()) return err diff --git a/input/input.go b/input/input.go index 294bfa9..8663a6c 100644 --- a/input/input.go +++ b/input/input.go @@ -11,14 +11,24 @@ package input import ( "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" ) -type model struct{ textinput textinput.Model } +type model struct { + textinput textinput.Model + autosize bool +} func (m model) Init() tea.Cmd { return textinput.Blink } func (m model) View() string { return m.textinput.View() } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { + case tea.WindowSizeMsg: + if !m.autosize { + return m, nil + } + m.textinput.Width = msg.Width - (lipgloss.Width(m.textinput.Prompt) + 1) + return m, nil case tea.KeyMsg: switch msg.Type { case tea.KeyEscape, tea.KeyCtrlC, tea.KeyEnter: diff --git a/input/options.go b/input/options.go index 63c931b..b45ef61 100644 --- a/input/options.go +++ b/input/options.go @@ -9,5 +9,5 @@ type Options struct { PromptStyle style.Styles `embed:"" prefix:"prompt." set:"name=prompt"` CursorStyle style.Styles `embed:"" prefix:"cursor." set:"defaultForeground=212" set:"name=cursor"` Value string `help:"Initial value (can also be passed via stdin)" default:""` - Width int `help:"Input width" default:"20"` + Width int `help:"Input width" default:"0"` }