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-08-05 00:45:19 +02:00
|
|
|
|
2022-07-31 03:29:09 +02:00
|
|
|
"github.com/charmbracelet/gum/internal/exit"
|
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-08-03 10:44:00 +02:00
|
|
|
i.CharLimit = o.CharLimit
|
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-31 03:29:09 +02:00
|
|
|
p := tea.NewProgram(model{
|
|
|
|
textinput: i,
|
|
|
|
aborted: false,
|
|
|
|
}, tea.WithOutput(os.Stderr))
|
|
|
|
tm, err := p.StartReturningModel()
|
2022-08-05 00:45:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to run input: %w", err)
|
|
|
|
}
|
2022-07-31 03:29:09 +02:00
|
|
|
m := tm.(model)
|
|
|
|
|
|
|
|
if m.aborted {
|
2022-07-31 03:41:18 +02:00
|
|
|
return exit.ErrAborted
|
2022-07-31 03:29:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(m.textinput.Value())
|
2022-08-05 00:45:19 +02:00
|
|
|
return nil
|
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 {
|
2022-08-05 00:45:19 +02:00
|
|
|
style.HideFlags(ctx)
|
|
|
|
return nil
|
2022-07-20 19:39:22 +02:00
|
|
|
}
|