command.go
This file
Path
input/command.go
81 lines · 1.9 KiB · Go
Last changed by
chore: prepare v2 (#1116)Drew Smirnoffbefore adoption02d6f06
History of this fileTake it
1package input23import (4 "errors"5 "fmt"6 "os"78 "charm.land/bubbles/v2/help"9 "charm.land/bubbles/v2/textinput"10 tea "charm.land/bubbletea/v2"11 "charm.land/gum/v2/cursor"12 "charm.land/gum/v2/internal/stdin"13 "charm.land/gum/v2/internal/timeout"14 "charm.land/gum/v2/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.SetWidth(o.Width)36 styles := i.Styles()37 styles.Focused.Prompt = o.PromptStyle.ToLipgloss()38 styles.Blurred.Prompt = o.PromptStyle.ToLipgloss()39 styles.Focused.Placeholder = o.PlaceholderStyle.ToLipgloss()40 styles.Blurred.Placeholder = o.PlaceholderStyle.ToLipgloss()41 i.SetStyles(styles)42 cursor.TextInput(&i, o.CursorMode, o.CursorStyle)43 i.CharLimit = o.CharLimit4445 if o.Password {46 i.EchoMode = textinput.EchoPassword47 i.EchoCharacter = '•'48 }4950 top, right, bottom, left := style.ParsePadding(o.Padding)51 m := model{52 textinput: i,53 header: o.Header,54 headerStyle: o.HeaderStyle.ToLipgloss(),55 padding: []int{top, right, bottom, left},56 autoWidth: o.Width < 1,57 showHelp: o.ShowHelp,58 help: help.New(),59 keymap: defaultKeymap(),60 }6162 ctx, cancel := timeout.Context(o.Timeout)63 defer cancel()6465 p := tea.NewProgram(66 m,67 tea.WithOutput(os.Stderr),68 tea.WithContext(ctx),69 )70 tm, err := p.Run()71 if err != nil {72 return fmt.Errorf("failed to run input: %w", err)73 }7475 m = tm.(model)76 if !m.submitted {77 return errors.New("not submitted")78 }79 fmt.Println(m.textinput.Value())80 return nil81}