Skip to content

Commit

Permalink
Refactor the API
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 6, 2023
1 parent 27e9a8c commit 2fede7b
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 115 deletions.
7 changes: 1 addition & 6 deletions _example/even-lexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
func main() {
p := prompt.New(
executor,
completer,
prompt.OptionSetLexer(prompt.NewEagerLexer(lexer)),
prompt.WithLexer(prompt.NewEagerLexer(lexer)),
)

p.Run()
Expand All @@ -38,10 +37,6 @@ func lexer(line string) []prompt.Token {
return elements
}

func completer(in prompt.Document) []prompt.Suggest {
return []prompt.Suggest{}
}

func executor(s string) {
fmt.Println("You printed: " + s)
}
2 changes: 1 addition & 1 deletion _example/exec-command/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func completer(t prompt.Document) []prompt.Suggest {
func main() {
p := prompt.New(
executor,
completer,
prompt.WithCompleter(completer)

Check failure on line 30 in _example/exec-command/main.go

View workflow job for this annotation

GitHub Actions / Build examples

syntax error: unexpected newline in argument list; possibly missing comma or )
)
p.Run()
}
8 changes: 4 additions & 4 deletions _example/http-prompt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ func main() {

p := prompt.New(
executor,
completer,
prompt.OptionPrefix(u.String()+"> "),
prompt.OptionLivePrefix(livePrefix),
prompt.OptionTitle("http-prompt"),
prompt.WithPrefix(u.String()+"> "),
prompt.WithLivePrefix(livePrefix),
prompt.WithTitle("http-prompt"),
prompt.WithCompleter(completer),
)
p.Run()
}
8 changes: 4 additions & 4 deletions _example/live-prefix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func changeLivePrefix() (string, bool) {
func main() {
p := prompt.New(
executor,
completer,
prompt.OptionPrefix(">>> "),
prompt.OptionLivePrefix(changeLivePrefix),
prompt.OptionTitle("live-prefix-example"),
prompt.WithPrefix(">>> "),
prompt.WithLivePrefix(changeLivePrefix),
prompt.WithTitle("live-prefix-example"),
prompt.WithCompleter(completer),
)
p.Run()
}
4 changes: 2 additions & 2 deletions _example/simple-echo/cjk-cyrillic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func main() {
p := prompt.New(
executor,
completer,

Check failure on line 26 in _example/simple-echo/cjk-cyrillic/main.go

View workflow job for this annotation

GitHub Actions / Build examples

cannot use completer (value of type func(in prompt.Document) []prompt.Suggest) as prompt.Option value in argument to prompt.New
prompt.OptionPrefix(">>> "),
prompt.OptionTitle("sql-prompt for multi width characters"),
prompt.WithPrefix(">>> "),
prompt.WithTitle("sql-prompt for multi width characters"),
)
p.Run()
}
14 changes: 7 additions & 7 deletions _example/simple-echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func completer(in prompt.Document) []prompt.Suggest {
func main() {
in := prompt.Input(
">>> ",
completer,
prompt.OptionTitle("sql-prompt"),
prompt.OptionHistory([]string{"SELECT * FROM users;"}),
prompt.OptionPrefixTextColor(prompt.Yellow),
prompt.OptionPreviewSuggestionTextColor(prompt.Blue),
prompt.OptionSelectedSuggestionBGColor(prompt.LightGray),
prompt.OptionSuggestionBGColor(prompt.DarkGray),
prompt.WithTitle("sql-prompt"),
prompt.WithHistory([]string{"SELECT * FROM users;"}),
prompt.WithPrefixTextColor(prompt.Yellow),
prompt.WithPreviewSuggestionTextColor(prompt.Blue),
prompt.WithSelectedSuggestionBGColor(prompt.LightGray),
prompt.WithSuggestionBGColor(prompt.DarkGray),
prompt.WithCompleter(completer),
)
fmt.Println("Your input: " + in)
}
4 changes: 2 additions & 2 deletions _tools/complete_file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func main() {
p := prompt.New(
executor,
completerFunc,
prompt.OptionPrefix(">>> "),
prompt.OptionCompletionWordSeparator(completer.FilePathCompletionSeparator),
prompt.WithPrefix(">>> "),
prompt.WithCompletionWordSeparator(completer.FilePathCompletionSeparator),
)
p.Run()
}
2 changes: 1 addition & 1 deletion completer/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
)

// FilePathCompleter is a completer for your local file system.
// Please caution that you need to set OptionCompletionWordSeparator(completer.FilePathCompletionSeparator)
// Please caution that you need to set WithCompletionWordSeparator(completer.FilePathCompletionSeparator)
// when you use this completer.
type FilePathCompleter struct {
Filter func(fi os.FileInfo) bool
Expand Down
27 changes: 21 additions & 6 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,30 @@ func formatSuggestions(suggests []Suggest, max int) (new []Suggest, width int) {
return new, leftWidth + rightWidth
}

// NewCompletionManager returns an initialized CompletionManager object.
func NewCompletionManager(completer Completer, max uint16) *CompletionManager {
return &CompletionManager{
selected: -1,
max: max,
completer: completer,
// Constructor option for CompletionManager.
type CompletionManagerOption func(*CompletionManager)

// Set a custom completer.
func CompletionManagerWithCompleter(completer Completer) CompletionManagerOption {
return func(c *CompletionManager) {
c.completer = completer
}
}

// NewCompletionManager returns an initialized CompletionManager object.
func NewCompletionManager(max uint16, opts ...CompletionManagerOption) *CompletionManager {
c := &CompletionManager{
selected: -1,
max: max,
completer: NoopCompleter,
verticalScroll: 0,
}

for _, opt := range opts {
opt(c)
}

return c
}

var _ Completer = NoopCompleter
Expand Down
Loading

0 comments on commit 2fede7b

Please sign in to comment.