feat(write): width < 1 uses terminal width

This commit is contained in:
Maas Lalani 2023-05-11 14:04:39 -04:00
parent 11584b5982
commit 7f54b3b289
6 changed files with 15 additions and 2 deletions

View file

@ -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 {

View file

@ -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":

View file

@ -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_"`

View file

@ -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 {

View file

@ -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"`

View file

@ -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":