gum/input/command.go

44 lines
989 B
Go
Raw Normal View History

package input
import (
"fmt"
"os"
2022-07-20 19:39:22 +02:00
"github.com/alecthomas/kong"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/gum/internal/stdin"
2022-07-20 19:39:22 +02:00
"github.com/charmbracelet/gum/style"
)
// Run provides a shell script interface for the text input bubble.
// https://github.com/charmbracelet/bubbles/textinput
func (o Options) Run() error {
in, _ := stdin.Read()
i := textinput.New()
if in != "" && o.Value == "" {
i.SetValue(in)
} else {
i.SetValue(o.Value)
}
i.Focus()
i.Prompt = o.Prompt
i.Placeholder = o.Placeholder
i.Width = o.Width
i.PromptStyle = o.PromptStyle.ToLipgloss()
i.CursorStyle = o.CursorStyle.ToLipgloss()
2022-07-27 19:58:34 +02:00
p := tea.NewProgram(model{i}, tea.WithOutput(os.Stderr))
m, err := p.StartReturningModel()
fmt.Println(m.(model).textinput.Value())
return err
}
2022-07-20 19:39:22 +02:00
// BeforeReset hook. Used to unclutter style flags.
func (o Options) BeforeReset(ctx *kong.Context) error {
return style.HideFlags(ctx)
}