gum/input/command.go

61 lines
1.2 KiB
Go
Raw Normal View History

package input
import (
"fmt"
"github.com/charmbracelet/bubbles/textinput"
2024-03-28 21:29:08 +01:00
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
2022-08-05 00:45:19 +02:00
"github.com/charmbracelet/gum/internal/stdin"
)
// Run provides a shell script interface for the text input bubble.
// https://github.com/charmbracelet/bubbles/textinput
func (o Options) Run() error {
i := textinput.New()
if o.Value != "" {
i.SetValue(o.Value)
} else if in, _ := stdin.Read(); in != "" {
i.SetValue(in)
}
2024-03-28 21:29:08 +01:00
theme := huh.ThemeCharm()
theme.Focused.Base = lipgloss.NewStyle()
theme.Focused.TextInput.Cursor = o.CursorStyle.ToLipgloss()
theme.Focused.TextInput.Placeholder = o.PlaceholderStyle.ToLipgloss()
theme.Focused.TextInput.Prompt = o.PromptStyle.ToLipgloss()
theme.Focused.Title = o.HeaderStyle.ToLipgloss()
var value string
var echoMode huh.EchoMode
if o.Password {
2024-03-28 21:29:08 +01:00
echoMode = huh.EchoModePassword
} else {
echoMode = huh.EchoModeNormal
}
2024-03-28 21:29:08 +01:00
err := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Prompt(o.Prompt).
Placeholder(o.Placeholder).
CharLimit(o.CharLimit).
EchoMode(echoMode).
Title(o.Header).
Value(&value),
),
).
WithShowHelp(false).
WithTheme(theme).
Run()
2022-07-31 03:29:09 +02:00
2024-03-28 21:29:08 +01:00
if err != nil {
return err
2022-07-31 03:29:09 +02:00
}
2024-03-28 21:29:08 +01:00
fmt.Println(value)
2022-08-05 00:45:19 +02:00
return nil
}