From 7f54b3b28921f912274e01d39a9902177a9dd74f Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Thu, 11 May 2023 14:04:39 -0400 Subject: [PATCH] feat(write): width < 1 uses terminal width --- input/command.go | 1 + input/input.go | 6 ++++++ input/options.go | 2 +- write/command.go | 1 + write/options.go | 2 +- write/write.go | 5 +++++ 6 files changed, 15 insertions(+), 2 deletions(-) diff --git a/input/command.go b/input/command.go index ec6f20e..c15e180 100644 --- a/input/command.go +++ b/input/command.go @@ -41,6 +41,7 @@ func (o Options) Run() error { aborted: false, header: o.Header, headerStyle: o.HeaderStyle.ToLipgloss(), + autoWidth: o.Width < 1, }, tea.WithOutput(os.Stderr)) tm, err := p.Run() if err != nil { diff --git a/input/input.go b/input/input.go index 5b0cae1..fbc84c5 100644 --- a/input/input.go +++ b/input/input.go @@ -11,9 +11,11 @@ import ( "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "github.com/mattn/go-runewidth" ) type model struct { + autoWidth bool header string headerStyle lipgloss.Style textinput textinput.Model @@ -37,6 +39,10 @@ func (m model) View() string { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { + case tea.WindowSizeMsg: + if m.autoWidth { + m.textinput.Width = msg.Width - runewidth.StringWidth(m.textinput.Prompt) - 1 + } case tea.KeyMsg: switch msg.String() { case "ctrl+c", "esc": diff --git a/input/options.go b/input/options.go index 2e103f8..86eef6a 100644 --- a/input/options.go +++ b/input/options.go @@ -10,7 +10,7 @@ type Options struct { CursorStyle style.Styles `embed:"" prefix:"cursor." set:"defaultForeground=212" envprefix:"GUM_INPUT_CURSOR_"` Value string `help:"Initial value (can also be passed via stdin)" default:""` 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 (0 for terminal 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_"` diff --git a/write/command.go b/write/command.go index 2177561..84fcd76 100644 --- a/write/command.go +++ b/write/command.go @@ -52,6 +52,7 @@ func (o Options) Run() error { textarea: a, header: o.Header, headerStyle: o.HeaderStyle.ToLipgloss(), + autoWidth: o.Width < 1, }, tea.WithOutput(os.Stderr)) tm, err := p.Run() if err != nil { diff --git a/write/options.go b/write/options.go index 59eb5c0..cfae3c9 100644 --- a/write/options.go +++ b/write/options.go @@ -4,7 +4,7 @@ import "github.com/charmbracelet/gum/style" // Options are the customization options for the textarea. type Options struct { - Width int `help:"Text area width" default:"50" env:"GUM_WRITE_WIDTH"` + Width int `help:"Text area width (0 for terminal width)" default:"50" env:"GUM_WRITE_WIDTH"` Height int `help:"Text area height" default:"5" env:"GUM_WRITE_HEIGHT"` Header string `help:"Header value" default:"" env:"GUM_WRITE_HEADER"` Placeholder string `help:"Placeholder value" default:"Write something..." env:"GUM_WRITE_PLACEHOLDER"` diff --git a/write/write.go b/write/write.go index 2e15415..674b66d 100644 --- a/write/write.go +++ b/write/write.go @@ -15,6 +15,7 @@ import ( ) type model struct { + autoWidth bool aborted bool header string headerStyle lipgloss.Style @@ -39,6 +40,10 @@ func (m model) View() string { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { + case tea.WindowSizeMsg: + if m.autoWidth { + m.textarea.SetWidth(msg.Width) + } case tea.KeyMsg: switch msg.String() { case "ctrl+c", "esc":