e15ebadefe
* Make possible to change the prefix dynamically * Add an example for using live-prefix * Apply live-prefix if it's enabled * Fix: rendering issue happened when the prefix is empty string Close #27 * Fix the title of propmt * Remove an excessive blank line * Refactor: Call `*Render.getCurrentPrefix()` in renderPrefix()
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/c-bata/go-prompt"
|
|
)
|
|
|
|
var LivePrefixState struct {
|
|
LivePrefix string
|
|
IsEnable bool
|
|
}
|
|
|
|
func executor(in string) {
|
|
fmt.Println("Your input: " + in)
|
|
if in == "" {
|
|
LivePrefixState.IsEnable = false
|
|
LivePrefixState.LivePrefix = in
|
|
return
|
|
}
|
|
LivePrefixState.LivePrefix = in + "> "
|
|
LivePrefixState.IsEnable = true
|
|
}
|
|
|
|
func completer(in prompt.Document) []prompt.Suggest {
|
|
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)
|
|
}
|
|
|
|
func changeLivePrefix() (string, bool) {
|
|
return LivePrefixState.LivePrefix , LivePrefixState.IsEnable
|
|
}
|
|
|
|
func main() {
|
|
p := prompt.New(
|
|
executor,
|
|
completer,
|
|
prompt.OptionPrefix(">>> "),
|
|
prompt.OptionLivePrefix(changeLivePrefix),
|
|
prompt.OptionTitle("live-prefix-example"),
|
|
)
|
|
p.Run()
|
|
} |