gum/input/command.go

63 lines
1.3 KiB
Go
Raw Normal View History

package input
import (
"fmt"
2024-03-28 21:36:14 +01:00
"os"
2024-03-28 21:36:14 +01:00
tea "github.com/charmbracelet/bubbletea"
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 {
2024-03-28 21:36:14 +01:00
var value string
if o.Value != "" {
2024-03-28 21:36:14 +01:00
value = o.Value
} else if in, _ := stdin.Read(); in != "" {
2024-03-28 21:36:14 +01:00
value = in
}
2024-03-28 21:29:08 +01:00
theme := huh.ThemeCharm()
theme.Focused.Base = lipgloss.NewStyle()
2024-03-28 21:36:14 +01:00
// theme.Focused.TextInput.Cursor = o.CursorStyle.ToLipgloss()
2024-03-28 21:29:08 +01:00
theme.Focused.TextInput.Placeholder = o.PlaceholderStyle.ToLipgloss()
theme.Focused.TextInput.Prompt = o.PromptStyle.ToLipgloss()
theme.Focused.Title = o.HeaderStyle.ToLipgloss()
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).
2024-03-28 21:38:24 +01:00
WithWidth(o.Width).
2024-03-28 21:29:08 +01:00
WithTheme(theme).
2024-03-28 21:36:14 +01:00
WithProgramOptions(tea.WithOutput(os.Stderr)).
2024-03-28 21:29:08 +01:00
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
}