go-prompt/option.go

311 lines
8.5 KiB
Go
Raw Normal View History

2017-07-16 08:20:58 +00:00
package prompt
// Option is the type to replace default parameters.
// prompt.New accepts any number of options (this is functional option pattern).
type Option func(prompt *Prompt) error
2017-07-16 08:20:58 +00:00
// OptionParser to set a custom ConsoleParser object. An argument should implement ConsoleParser interface.
func OptionParser(x ConsoleParser) Option {
2017-07-16 08:20:58 +00:00
return func(p *Prompt) error {
p.in = x
return nil
}
}
2018-02-18 09:18:42 +00:00
// OptionWriter to set a custom ConsoleWriter object. An argument should implement ConsoleWriter interface.
func OptionWriter(x ConsoleWriter) Option {
2017-07-16 08:20:58 +00:00
return func(p *Prompt) error {
2018-10-20 04:49:52 +00:00
registerConsoleWriter(x)
2017-07-16 08:20:58 +00:00
p.renderer.out = x
return nil
}
}
// OptionTitle to set title displayed at the header bar of terminal.
func OptionTitle(x string) Option {
2017-07-16 08:20:58 +00:00
return func(p *Prompt) error {
2017-07-16 17:18:11 +00:00
p.renderer.title = x
2017-07-16 08:20:58 +00:00
return nil
}
}
// OptionPrefix to set prefix string.
func OptionPrefix(x string) Option {
2017-07-16 08:20:58 +00:00
return func(p *Prompt) error {
2017-07-16 17:11:52 +00:00
p.renderer.prefix = x
return nil
}
}
// OptionInitialBufferText to set the initial buffer text
func OptionInitialBufferText(x string) Option {
return func(p *Prompt) error {
p.buf.InsertText(x, false, true)
return nil
}
}
// OptionCompletionWordSeparator to set word separators. Enable only ' ' if empty.
func OptionCompletionWordSeparator(x string) Option {
return func(p *Prompt) error {
p.completion.wordSeparator = x
return nil
}
}
// OptionLivePrefix to change the prefix dynamically by callback function
func OptionLivePrefix(f func() (prefix string, useLivePrefix bool)) Option {
return func(p *Prompt) error {
p.renderer.livePrefixCallback = f
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionPrefixTextColor change a text color of prefix string
func OptionPrefixTextColor(x Color) Option {
2017-07-16 17:11:52 +00:00
return func(p *Prompt) error {
2017-07-17 13:32:13 +00:00
p.renderer.prefixTextColor = x
2017-07-16 08:20:58 +00:00
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionPrefixBackgroundColor to change a background color of prefix string
func OptionPrefixBackgroundColor(x Color) Option {
2017-07-16 19:55:05 +00:00
return func(p *Prompt) error {
2017-07-17 13:32:13 +00:00
p.renderer.prefixBGColor = x
2017-07-16 19:55:05 +00:00
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionInputTextColor to change a color of text which is input by user
func OptionInputTextColor(x Color) Option {
2017-07-16 19:55:05 +00:00
return func(p *Prompt) error {
2017-07-17 13:32:13 +00:00
p.renderer.inputTextColor = x
2017-07-16 19:55:05 +00:00
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionInputBGColor to change a color of background which is input by user
func OptionInputBGColor(x Color) Option {
2017-07-17 13:32:13 +00:00
return func(p *Prompt) error {
p.renderer.inputBGColor = x
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionPreviewSuggestionTextColor to change a text color which is completed
func OptionPreviewSuggestionTextColor(x Color) Option {
2017-07-17 13:32:13 +00:00
return func(p *Prompt) error {
p.renderer.previewSuggestionTextColor = x
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionPreviewSuggestionBGColor to change a background color which is completed
func OptionPreviewSuggestionBGColor(x Color) Option {
2017-07-17 13:32:13 +00:00
return func(p *Prompt) error {
p.renderer.previewSuggestionBGColor = x
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionSuggestionTextColor to change a text color in drop down suggestions.
func OptionSuggestionTextColor(x Color) Option {
2017-07-16 19:32:42 +00:00
return func(p *Prompt) error {
2017-07-17 13:32:13 +00:00
p.renderer.suggestionTextColor = x
2017-07-16 19:32:42 +00:00
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionSuggestionBGColor change a background color in drop down suggestions.
func OptionSuggestionBGColor(x Color) Option {
2017-07-16 19:32:42 +00:00
return func(p *Prompt) error {
2017-07-17 13:32:13 +00:00
p.renderer.suggestionBGColor = x
2017-07-16 19:32:42 +00:00
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionSelectedSuggestionTextColor to change a text color for completed text which is selected inside suggestions drop down box.
func OptionSelectedSuggestionTextColor(x Color) Option {
2017-07-16 19:32:42 +00:00
return func(p *Prompt) error {
2017-07-17 13:32:13 +00:00
p.renderer.selectedSuggestionTextColor = x
2017-07-16 19:32:42 +00:00
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionSelectedSuggestionBGColor to change a background color for completed text which is selected inside suggestions drop down box.
func OptionSelectedSuggestionBGColor(x Color) Option {
2017-07-16 19:32:42 +00:00
return func(p *Prompt) error {
2017-07-17 13:32:13 +00:00
p.renderer.selectedSuggestionBGColor = x
2017-07-16 19:32:42 +00:00
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionDescriptionTextColor to change a background color of description text in drop down suggestions.
func OptionDescriptionTextColor(x Color) Option {
2017-07-18 15:36:16 +00:00
return func(p *Prompt) error {
p.renderer.descriptionTextColor = x
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionDescriptionBGColor to change a background color of description text in drop down suggestions.
func OptionDescriptionBGColor(x Color) Option {
2017-07-18 15:36:16 +00:00
return func(p *Prompt) error {
p.renderer.descriptionBGColor = x
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionSelectedDescriptionTextColor to change a text color of description which is selected inside suggestions drop down box.
func OptionSelectedDescriptionTextColor(x Color) Option {
2017-07-18 15:36:16 +00:00
return func(p *Prompt) error {
p.renderer.selectedDescriptionTextColor = x
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionSelectedDescriptionBGColor to change a background color of description which is selected inside suggestions drop down box.
func OptionSelectedDescriptionBGColor(x Color) Option {
2017-07-18 15:36:16 +00:00
return func(p *Prompt) error {
p.renderer.selectedDescriptionBGColor = x
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionScrollbarThumbColor to change a thumb color on scrollbar.
2018-02-13 14:50:24 +00:00
func OptionScrollbarThumbColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.scrollbarThumbColor = x
return nil
}
}
2018-02-15 07:36:35 +00:00
// OptionScrollbarBGColor to change a background color of scrollbar.
2018-02-13 14:50:24 +00:00
func OptionScrollbarBGColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.scrollbarBGColor = x
return nil
}
}
// OptionMaxSuggestion specify the max number of displayed suggestions.
func OptionMaxSuggestion(x uint16) Option {
2017-07-16 08:20:58 +00:00
return func(p *Prompt) error {
2017-08-12 09:59:10 +00:00
p.completion.max = x
2017-07-16 08:20:58 +00:00
return nil
}
}
// OptionHistory to set history expressed by string array.
func OptionHistory(x []string) Option {
2017-08-09 03:50:07 +00:00
return func(p *Prompt) error {
p.history.histories = x
p.history.Clear()
return nil
}
}
// OptionSwitchKeyBindMode set a key bind mode.
func OptionSwitchKeyBindMode(m KeyBindMode) Option {
2017-08-13 04:33:51 +00:00
return func(p *Prompt) error {
p.keyBindMode = m
return nil
}
}
2019-08-27 06:00:09 +00:00
// OptionCompletionOnDown allows for Down arrow key to trigger completion.
2019-08-26 16:25:26 +00:00
func OptionCompletionOnDown() Option {
return func(p *Prompt) error {
p.completionOnDown = true
return nil
}
}
// SwitchKeyBindMode to set a key bind mode.
// Deprecated: Please use OptionSwitchKeyBindMode.
var SwitchKeyBindMode = OptionSwitchKeyBindMode
// OptionAddKeyBind to set a custom key bind.
func OptionAddKeyBind(b ...KeyBind) Option {
2017-08-13 04:09:45 +00:00
return func(p *Prompt) error {
p.keyBindings = append(p.keyBindings, b...)
return nil
}
}
// OptionAddASCIICodeBind to set a custom key bind.
func OptionAddASCIICodeBind(b ...ASCIICodeBind) Option {
return func(p *Prompt) error {
p.ASCIICodeBindings = append(p.ASCIICodeBindings, b...)
return nil
}
}
2018-10-18 10:41:40 +00:00
// OptionShowCompletionAtStart to set completion window is open at start.
func OptionShowCompletionAtStart() Option {
return func(p *Prompt) error {
p.completion.showAtStart = true
return nil
}
}
// OptionBreakLineCallback to run a callback at every break line
func OptionBreakLineCallback(fn func(*Document)) Option {
return func(p *Prompt) error {
p.renderer.breakLineCallback = fn
return nil
}
}
// OptionSetExitCheckerOnInput set an exit function which checks if go-prompt exits its Run loop
func OptionSetExitCheckerOnInput(fn ExitChecker) Option {
return func(p *Prompt) error {
p.exitChecker = fn
return nil
}
}
// New returns a Prompt with powerful auto-completion.
func New(executor Executor, completer Completer, opts ...Option) *Prompt {
defaultWriter := NewStdoutWriter()
2018-10-20 04:49:52 +00:00
registerConsoleWriter(defaultWriter)
2018-10-20 04:06:36 +00:00
2017-07-16 08:20:58 +00:00
pt := &Prompt{
2018-02-12 10:12:31 +00:00
in: NewStandardInputParser(),
2017-07-16 08:20:58 +00:00
renderer: &Render{
2017-07-18 15:36:16 +00:00
prefix: "> ",
2018-10-20 04:06:36 +00:00
out: defaultWriter,
livePrefixCallback: func() (string, bool) { return "", false },
2017-07-18 15:36:16 +00:00
prefixTextColor: Blue,
prefixBGColor: DefaultColor,
inputTextColor: DefaultColor,
inputBGColor: DefaultColor,
previewSuggestionTextColor: Green,
previewSuggestionBGColor: DefaultColor,
suggestionTextColor: White,
suggestionBGColor: Cyan,
selectedSuggestionTextColor: Black,
selectedSuggestionBGColor: Turquoise,
descriptionTextColor: Black,
descriptionBGColor: Turquoise,
selectedDescriptionTextColor: White,
selectedDescriptionBGColor: Cyan,
2018-02-12 08:40:47 +00:00
scrollbarThumbColor: DarkGray,
scrollbarBGColor: Cyan,
2017-07-16 08:20:58 +00:00
},
2017-08-13 04:33:51 +00:00
buf: NewBuffer(),
executor: executor,
history: NewHistory(),
completion: NewCompletionManager(completer, 6),
keyBindMode: EmacsKeyBind, // All the above assume that bash is running in the default Emacs setting
2017-07-16 08:20:58 +00:00
}
for _, opt := range opts {
if err := opt(pt); err != nil {
panic(err)
}
}
return pt
}