mirror of
https://github.com/charmbracelet/gum
synced 2024-06-05 07:12:19 +02:00
06e8546366
Removes the hardcoded 400 chars CharLimit (default for textarea), and make the CharLimit configurable through an Option.
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package input
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/alecthomas/kong"
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/gum/internal/exit"
|
|
"github.com/charmbracelet/gum/internal/stdin"
|
|
"github.com/charmbracelet/gum/style"
|
|
)
|
|
|
|
// 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 in, _ := stdin.Read(); in != "" && o.Value == "" {
|
|
i.SetValue(in)
|
|
} else {
|
|
i.SetValue(o.Value)
|
|
}
|
|
|
|
i.Focus()
|
|
i.Prompt = o.Prompt
|
|
i.Placeholder = o.Placeholder
|
|
i.Width = o.Width
|
|
i.PromptStyle = o.PromptStyle.ToLipgloss()
|
|
i.CursorStyle = o.CursorStyle.ToLipgloss()
|
|
i.CharLimit = o.CharLimit
|
|
|
|
if o.Password {
|
|
i.EchoMode = textinput.EchoPassword
|
|
i.EchoCharacter = '•'
|
|
}
|
|
|
|
p := tea.NewProgram(model{
|
|
textinput: i,
|
|
aborted: false,
|
|
}, tea.WithOutput(os.Stderr))
|
|
tm, err := p.StartReturningModel()
|
|
m := tm.(model)
|
|
|
|
if m.aborted {
|
|
return exit.ErrAborted
|
|
}
|
|
|
|
fmt.Println(m.textinput.Value())
|
|
return err
|
|
}
|
|
|
|
// BeforeReset hook. Used to unclutter style flags.
|
|
func (o Options) BeforeReset(ctx *kong.Context) error {
|
|
return style.HideFlags(ctx)
|
|
}
|