2018-02-12 11:00:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-07-12 01:51:19 +00:00
|
|
|
prompt "git.tcp.direct/tcp.direct/go-prompt"
|
2018-02-12 11:00:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var LivePrefixState struct {
|
|
|
|
LivePrefix string
|
2018-02-13 14:24:49 +00:00
|
|
|
IsEnable bool
|
2018-02-12 11:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-02-13 14:24:49 +00:00
|
|
|
return LivePrefixState.LivePrefix, LivePrefixState.IsEnable
|
2018-02-12 11:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
p := prompt.New(
|
|
|
|
executor,
|
|
|
|
completer,
|
|
|
|
prompt.OptionPrefix(">>> "),
|
|
|
|
prompt.OptionLivePrefix(changeLivePrefix),
|
|
|
|
prompt.OptionTitle("live-prefix-example"),
|
|
|
|
)
|
|
|
|
p.Run()
|
2018-02-13 14:24:49 +00:00
|
|
|
}
|