Skip to content

Commit

Permalink
Fix the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 23, 2023
1 parent deb8d2b commit c32fbe4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 2 additions & 7 deletions _example/exec-command/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"os"
"os/exec"

Expand All @@ -9,6 +10,7 @@ import (

func executor(t string) {
if t != "bash" {
fmt.Println("Sorry, I don't understand.")
return
}

Expand All @@ -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()
}
5 changes: 2 additions & 3 deletions _example/http-prompt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
9 changes: 7 additions & 2 deletions _example/live-prefix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

prompt "github.com/elk-language/go-prompt"
istrings "github.com/elk-language/go-prompt/strings"
)

var LivePrefix string = ">>> "
Expand All @@ -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 {
Expand Down

0 comments on commit c32fbe4

Please sign in to comment.