2017-07-16 08:20:58 +00:00
|
|
|
package prompt
|
|
|
|
|
2017-08-17 16:40:41 +00:00
|
|
|
// 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
|
|
|
|
2017-08-17 16:40:41 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:40:41 +00:00
|
|
|
// OptionWriter to set a custom ConsoleWriter object. An argument should implement ConsoleWriter interace.
|
|
|
|
func OptionWriter(x ConsoleWriter) Option {
|
2017-07-16 08:20:58 +00:00
|
|
|
return func(p *Prompt) error {
|
|
|
|
p.renderer.out = x
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:40:41 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:40:41 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:00:48 +00:00
|
|
|
// 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
|
2017-08-17 16:40:41 +00:00
|
|
|
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
|
2017-08-17 16:40:41 +00:00
|
|
|
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
|
2017-08-17 16:40:41 +00:00
|
|
|
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
|
2017-08-17 16:40:41 +00:00
|
|
|
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
|
2017-08-17 16:40:41 +00:00
|
|
|
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
|
2017-08-17 16:40:41 +00:00
|
|
|
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.
|
2017-08-17 16:40:41 +00:00
|
|
|
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.
|
2017-08-17 16:40:41 +00:00
|
|
|
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.
|
2017-08-17 16:40:41 +00:00
|
|
|
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.
|
2017-08-17 16:40:41 +00:00
|
|
|
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.
|
2017-08-17 16:40:41 +00:00
|
|
|
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.
|
2017-08-17 16:40:41 +00:00
|
|
|
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.
|
2017-08-17 16:40:41 +00:00
|
|
|
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.
|
2017-08-17 16:40:41 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:40:41 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:40:41 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:40:41 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:40:41 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:40:41 +00:00
|
|
|
// New returns a Prompt with powerful auto-completion.
|
|
|
|
func New(executor Executor, completer Completer, opts ...Option) *Prompt {
|
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-02-12 10:12:31 +00:00
|
|
|
out: NewStandardOutputWriter(),
|
2018-02-12 11:00:48 +00:00
|
|
|
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
|
|
|
|
}
|