2022-07-06 18:06:08 +02:00
|
|
|
package input
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2022-07-20 19:39:22 +02:00
|
|
|
"github.com/alecthomas/kong"
|
2022-07-06 18:06:08 +02:00
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2022-07-13 04:12:02 +02:00
|
|
|
"github.com/charmbracelet/gum/internal/stdin"
|
2022-07-20 19:39:22 +02:00
|
|
|
"github.com/charmbracelet/gum/style"
|
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 {
|
2022-07-06 18:06:08 +02:00
|
|
|
i := textinput.New()
|
2022-07-30 23:40:33 +02:00
|
|
|
if in, _ := stdin.Read(); in != "" && o.Value == "" {
|
2022-07-13 04:12:02 +02:00
|
|
|
i.SetValue(in)
|
|
|
|
} else {
|
|
|
|
i.SetValue(o.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Focus()
|
2022-07-06 18:06:08 +02:00
|
|
|
i.Prompt = o.Prompt
|
|
|
|
i.Placeholder = o.Placeholder
|
|
|
|
i.Width = o.Width
|
2022-07-12 22:05:40 +02:00
|
|
|
i.PromptStyle = o.PromptStyle.ToLipgloss()
|
|
|
|
i.CursorStyle = o.CursorStyle.ToLipgloss()
|
2022-07-06 18:06:08 +02:00
|
|
|
|
2022-07-29 17:19:40 +02:00
|
|
|
if o.Password {
|
|
|
|
i.EchoMode = textinput.EchoPassword
|
|
|
|
i.EchoCharacter = '•'
|
|
|
|
}
|
|
|
|
|
2022-07-27 19:58:34 +02:00
|
|
|
p := tea.NewProgram(model{i}, tea.WithOutput(os.Stderr))
|
2022-07-13 04:12:02 +02:00
|
|
|
m, err := p.StartReturningModel()
|
2022-07-06 18:06:08 +02:00
|
|
|
fmt.Println(m.(model).textinput.Value())
|
2022-07-13 04:12:02 +02:00
|
|
|
return err
|
2022-07-06 18:06:08 +02:00
|
|
|
}
|
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)
|
|
|
|
}
|