Add a comment to some exported functions
This commit is contained in:
parent
6b76ff545b
commit
6905a01295
|
@ -2,8 +2,10 @@ package prompt
|
|||
|
||||
import "strings"
|
||||
|
||||
// Filter is the type to filter the prompt.Suggestion array.
|
||||
type Filter func([]Suggest, string, bool) []Suggest
|
||||
|
||||
// FilterHasPrefix checks whether the string completions.Text begins with sub.
|
||||
func FilterHasPrefix(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
||||
if sub == "" {
|
||||
return completions
|
||||
|
@ -25,6 +27,7 @@ func FilterHasPrefix(completions []Suggest, sub string, ignoreCase bool) []Sugge
|
|||
return ret
|
||||
}
|
||||
|
||||
// FilterHasSuffix checks whether the completion.Text ends with sub.
|
||||
func FilterHasSuffix(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
||||
if sub == "" {
|
||||
return completions
|
||||
|
@ -46,6 +49,7 @@ func FilterHasSuffix(completions []Suggest, sub string, ignoreCase bool) []Sugge
|
|||
return ret
|
||||
}
|
||||
|
||||
// FilterContains checks whether the completion.Text contains sub.
|
||||
func FilterContains(completions []Suggest, sub string, ignoreCase bool) []Suggest {
|
||||
if sub == "" {
|
||||
return completions
|
||||
|
|
2
input.go
2
input.go
|
@ -2,6 +2,7 @@ package prompt
|
|||
|
||||
func dummyExecutor(in string) { return }
|
||||
|
||||
// Input get the input data from the user and return it.
|
||||
func Input(prefix string, completer Completer, opts ...option) string {
|
||||
pt := New(dummyExecutor, completer)
|
||||
pt.renderer.prefixTextColor = DefaultColor
|
||||
|
@ -15,6 +16,7 @@ func Input(prefix string, completer Completer, opts ...option) string {
|
|||
return pt.Input()
|
||||
}
|
||||
|
||||
// Choose to the shortcut of input function to select from string array.
|
||||
func Choose(prefix string, choices []string, opts ...option) string {
|
||||
completer := newChoiceCompleter(choices, FilterHasPrefix)
|
||||
pt := New(dummyExecutor, completer)
|
||||
|
|
63
option.go
63
option.go
|
@ -2,142 +2,150 @@ package prompt
|
|||
|
||||
import "syscall"
|
||||
|
||||
type option func(prompt *Prompt) error
|
||||
// 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
|
||||
|
||||
func OptionParser(x ConsoleParser) option {
|
||||
// OptionParser to set a custom ConsoleParser object. An argument should implement ConsoleParser interface.
|
||||
func OptionParser(x ConsoleParser) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.in = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionWriter(x ConsoleWriter) option {
|
||||
// OptionWriter to set a custom ConsoleWriter object. An argument should implement ConsoleWriter interace.
|
||||
func OptionWriter(x ConsoleWriter) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.out = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionTitle(x string) option {
|
||||
// OptionTitle to set title displayed at the header bar of terminal.
|
||||
func OptionTitle(x string) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.title = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionPrefix(x string) option {
|
||||
// OptionPrefix to set prefix string.
|
||||
func OptionPrefix(x string) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.prefix = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionPrefixTextColor(x Color) option {
|
||||
func OptionPrefixTextColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.prefixTextColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionPrefixBackgroundColor(x Color) option {
|
||||
func OptionPrefixBackgroundColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.prefixBGColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionInputTextColor(x Color) option {
|
||||
func OptionInputTextColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.inputTextColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionInputBGColor(x Color) option {
|
||||
func OptionInputBGColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.inputBGColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionPreviewSuggestionTextColor(x Color) option {
|
||||
func OptionPreviewSuggestionTextColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.previewSuggestionTextColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionPreviewSuggestionBGColor(x Color) option {
|
||||
func OptionPreviewSuggestionBGColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.previewSuggestionBGColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionSuggestionTextColor(x Color) option {
|
||||
func OptionSuggestionTextColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.suggestionTextColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionSuggestionBGColor(x Color) option {
|
||||
func OptionSuggestionBGColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.suggestionBGColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionSelectedSuggestionTextColor(x Color) option {
|
||||
func OptionSelectedSuggestionTextColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.selectedSuggestionTextColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionSelectedSuggestionBGColor(x Color) option {
|
||||
func OptionSelectedSuggestionBGColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.selectedSuggestionBGColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionDescriptionTextColor(x Color) option {
|
||||
func OptionDescriptionTextColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.descriptionTextColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionDescriptionBGColor(x Color) option {
|
||||
func OptionDescriptionBGColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.descriptionBGColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionSelectedDescriptionTextColor(x Color) option {
|
||||
func OptionSelectedDescriptionTextColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.selectedDescriptionTextColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionSelectedDescriptionBGColor(x Color) option {
|
||||
func OptionSelectedDescriptionBGColor(x Color) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.selectedDescriptionBGColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionMaxSuggestion(x uint16) option {
|
||||
// OptionMaxSuggestion specify the max number of displayed suggestions.
|
||||
func OptionMaxSuggestion(x uint16) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.completion.max = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionHistory(x []string) option {
|
||||
// OptionHistory to set history expressed by string array.
|
||||
func OptionHistory(x []string) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.history.histories = x
|
||||
p.history.Clear()
|
||||
|
@ -145,21 +153,28 @@ func OptionHistory(x []string) option {
|
|||
}
|
||||
}
|
||||
|
||||
func SwitchKeyBindMode(m KeyBindMode) option {
|
||||
// OptionSwitchKeyBindMode set a key bind mode.
|
||||
func OptionSwitchKeyBindMode(m KeyBindMode) Option {
|
||||
return func(p *Prompt) error {
|
||||
p.keyBindMode = m
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionAddKeyBind(b ...KeyBind) option {
|
||||
// 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 {
|
||||
return func(p *Prompt) error {
|
||||
p.keyBindings = append(p.keyBindings, b...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func New(executor Executor, completer Completer, opts ...option) *Prompt {
|
||||
// New returns a Prompt with powerful auto-completion.
|
||||
func New(executor Executor, completer Completer, opts ...Option) *Prompt {
|
||||
pt := &Prompt{
|
||||
in: &VT100Parser{fd: syscall.Stdin},
|
||||
renderer: &Render{
|
||||
|
|
Loading…
Reference in New Issue