2022-07-06 18:06:08 +02:00
|
|
|
package input
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-03-28 21:36:14 +01:00
|
|
|
"os"
|
2022-07-06 18:06:08 +02:00
|
|
|
|
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
|
|
|
|
2022-07-13 04:12:02 +02:00
|
|
|
"github.com/charmbracelet/gum/internal/stdin"
|
2022-07-06 18:06:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Run provides a shell script interface for the text input bubble.
|
|
|
|
// https://github.com/charmbracelet/bubbles/textinput
|
2022-07-13 04:12:02 +02:00
|
|
|
func (o Options) Run() error {
|
2024-03-28 21:36:14 +01:00
|
|
|
var value string
|
2023-11-14 22:54:45 +01:00
|
|
|
if o.Value != "" {
|
2024-03-28 21:36:14 +01:00
|
|
|
value = o.Value
|
2023-11-14 22:54:45 +01:00
|
|
|
} else if in, _ := stdin.Read(); in != "" {
|
2024-03-28 21:36:14 +01:00
|
|
|
value = in
|
2022-07-13 04:12:02 +02:00
|
|
|
}
|
|
|
|
|
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
|
2022-07-06 18:06:08 +02:00
|
|
|
|
2022-07-29 17:19:40 +02:00
|
|
|
if o.Password {
|
2024-03-28 21:29:08 +01:00
|
|
|
echoMode = huh.EchoModePassword
|
|
|
|
} else {
|
|
|
|
echoMode = huh.EchoModeNormal
|
2022-07-29 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
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
|
2022-07-06 18:06:08 +02:00
|
|
|
}
|