prompt util func to reduce code reuse
This commit is contained in:
parent
f21c414f1b
commit
1593736c70
4 changed files with 61 additions and 29 deletions
33
lib/prompt.go
Normal file
33
lib/prompt.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/Cynosphere/comcord/state"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/ergochat/readline"
|
||||
)
|
||||
|
||||
func MakePrompt(session *discordgo.Session, prompt string, uniqueLine bool, callback func(session *discordgo.Session, input string, interrupt bool)) {
|
||||
state.SetInPrompt(true)
|
||||
state.SetPromptText(prompt)
|
||||
|
||||
rl, _ := readline.NewFromConfig(&readline.Config{
|
||||
Prompt: prompt,
|
||||
UniqueEditLine: uniqueLine,
|
||||
})
|
||||
defer rl.Close()
|
||||
|
||||
input, err := rl.Readline()
|
||||
input = strings.TrimSpace(input)
|
||||
rl.Close()
|
||||
|
||||
interrupt := err == readline.ErrInterrupt
|
||||
|
||||
callback(session, input, interrupt)
|
||||
|
||||
state.SetInPrompt(false)
|
||||
state.SetPromptText("")
|
||||
|
||||
ProcessQueue(session)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue