From c32fbe4c112c2e64dc7f333ae4029841ebf750b2 Mon Sep 17 00:00:00 2001 From: Mateusz Drewniak Date: Sun, 23 Jul 2023 18:30:22 +0200 Subject: [PATCH] Fix the examples --- CHANGELOG.md | 2 ++ _example/exec-command/main.go | 9 ++------- _example/http-prompt/main.go | 5 ++--- _example/live-prefix/main.go | 9 +++++++-- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c370e03..8672e82d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -74,6 +74,8 @@ This release aims to make the code a bit cleaner, fix a couple of bugs and provi - Rename `prompt.OptionShowCompletionAtStart` to `prompt.WithShowCompletionAtStart` - Rename `prompt.OptionBreakLineCallback` to `prompt.WithBreakLineCallback` - Rename `prompt.OptionExitChecker` to `prompt.WithExitChecker` +- Change the signature of `Completer` from `func(Document) []Suggest` to `func(Document) (suggestions []Suggest, startChar, endChar istrings.RuneNumber)` +- Change the signature of `KeyBindFunc` from `func(*Buffer)` to `func(p *Prompt) (rerender bool)` ### Fixed diff --git a/_example/exec-command/main.go b/_example/exec-command/main.go index 02766bf7..703fa52e 100644 --- a/_example/exec-command/main.go +++ b/_example/exec-command/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "os/exec" @@ -9,6 +10,7 @@ import ( func executor(t string) { if t != "bash" { + fmt.Println("Sorry, I don't understand.") return } @@ -19,16 +21,9 @@ func executor(t string) { cmd.Run() } -func completer(t prompt.Document) []prompt.Suggest { - return []prompt.Suggest{ - {Text: "bash"}, - } -} - func main() { p := prompt.New( executor, - prompt.WithCompleter(completer), ) p.Run() } diff --git a/_example/http-prompt/main.go b/_example/http-prompt/main.go index b77825db..df8e1091 100644 --- a/_example/http-prompt/main.go +++ b/_example/http-prompt/main.go @@ -159,13 +159,12 @@ func executor(in string) { } func completer(in prompt.Document) ([]prompt.Suggest, istrings.RuneNumber, istrings.RuneNumber) { - currentIndex := in.CurrentRuneIndex() + endIndex := in.CurrentRuneIndex() w := in.GetWordBeforeCursor() if w == "" { return []prompt.Suggest{}, 0, 0 } - startIndex := currentIndex - istrings.RuneCount(w) - endIndex := currentIndex + startIndex := endIndex - istrings.RuneCount(w) return prompt.FilterHasPrefix(suggestions, w, true), startIndex, endIndex } diff --git a/_example/live-prefix/main.go b/_example/live-prefix/main.go index 4fe160a5..d4f9cb3c 100644 --- a/_example/live-prefix/main.go +++ b/_example/live-prefix/main.go @@ -4,6 +4,7 @@ import ( "fmt" prompt "github.com/elk-language/go-prompt" + istrings "github.com/elk-language/go-prompt/strings" ) var LivePrefix string = ">>> " @@ -17,14 +18,18 @@ func executor(in string) { LivePrefix = in + "> " } -func completer(in prompt.Document) []prompt.Suggest { +func completer(in prompt.Document) ([]prompt.Suggest, istrings.RuneNumber, istrings.RuneNumber) { + endIndex := in.CurrentRuneIndex() + w := in.GetWordBeforeCursor() + startIndex := endIndex - istrings.RuneCount(w) + s := []prompt.Suggest{ {Text: "users", Description: "Store the username and age"}, {Text: "articles", Description: "Store the article text posted by user"}, {Text: "comments", Description: "Store the text commented to articles"}, {Text: "groups", Description: "Combine users with specific rules"}, } - return prompt.FilterHasPrefix(s, in.GetWordBeforeCursor(), true) + return prompt.FilterHasPrefix(s, w, true), startIndex, endIndex } func changeLivePrefix() string {