command.go
This file
Path
input/command.go
79 lines · 1.8 KiB · Go
Last changed by
feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
History of this fileTake it
1package input23import (4 "errors"5 "fmt"6 "os"78 "github.com/charmbracelet/bubbles/help"9 "github.com/charmbracelet/bubbles/textinput"10 tea "github.com/charmbracelet/bubbletea"11 "github.com/charmbracelet/gum/cursor"12 "github.com/charmbracelet/gum/internal/stdin"13 "github.com/charmbracelet/gum/internal/timeout"14 "github.com/charmbracelet/gum/style"15)1617// Run provides a shell script interface for the text input bubble.18// https://github.com/charmbracelet/bubbles/textinput19func (o Options) Run() error {20 if o.Value == "" {21 if in, _ := stdin.Read(stdin.StripANSI(o.StripANSI)); in != "" {22 o.Value = in23 }24 }2526 i := textinput.New()27 if o.Value != "" {28 i.SetValue(o.Value)29 } else if in, _ := stdin.Read(stdin.StripANSI(o.StripANSI)); in != "" {30 i.SetValue(in)31 }32 i.Focus()33 i.Prompt = o.Prompt34 i.Placeholder = o.Placeholder35 i.Width = o.Width36 i.PromptStyle = o.PromptStyle.ToLipgloss()37 i.PlaceholderStyle = o.PlaceholderStyle.ToLipgloss()38 i.Cursor.Style = o.CursorStyle.ToLipgloss()39 i.Cursor.SetMode(cursor.Modes[o.CursorMode])40 i.CharLimit = o.CharLimit4142 if o.Password {43 i.EchoMode = textinput.EchoPassword44 i.EchoCharacter = '•'45 }4647 top, right, bottom, left := style.ParsePadding(o.Padding)48 m := model{49 textinput: i,50 header: o.Header,51 headerStyle: o.HeaderStyle.ToLipgloss(),52 padding: []int{top, right, bottom, left},53 autoWidth: o.Width < 1,54 showHelp: o.ShowHelp,55 help: help.New(),56 keymap: defaultKeymap(),57 }5859 ctx, cancel := timeout.Context(o.Timeout)60 defer cancel()6162 p := tea.NewProgram(63 m,64 tea.WithOutput(os.Stderr),65 tea.WithReportFocus(),66 tea.WithContext(ctx),67 )68 tm, err := p.Run()69 if err != nil {70 return fmt.Errorf("failed to run input: %w", err)71 }7273 m = tm.(model)74 if !m.submitted {75 return errors.New("not submitted")76 }77 fmt.Println(m.textinput.Value())78 return nil79}