prompt util func to reduce code reuse
This commit is contained in:
parent
f21c414f1b
commit
1593736c70
|
@ -13,7 +13,7 @@ Go is more portable than Node.js
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
- [x] Send mode
|
- [x] Send mode
|
||||||
- [ ] Commands
|
- [x] Commands
|
||||||
- [x] Quit (q)
|
- [x] Quit (q)
|
||||||
- [ ] Switch guilds (G)
|
- [ ] Switch guilds (G)
|
||||||
- [ ] Switch channels (g)
|
- [ ] Switch channels (g)
|
||||||
|
@ -29,7 +29,7 @@ Go is more portable than Node.js
|
||||||
- [ ] Cross-guild peek (P)
|
- [ ] Cross-guild peek (P)
|
||||||
- [ ] List channels (l)
|
- [ ] List channels (l)
|
||||||
- [ ] List guilds (L)
|
- [ ] List guilds (L)
|
||||||
- [ ] Clear (c)
|
- [x] Clear (c)
|
||||||
- [ ] Surf channels forwards (>)
|
- [ ] Surf channels forwards (>)
|
||||||
- [ ] Surf channels backwards (<)
|
- [ ] Surf channels backwards (<)
|
||||||
- [ ] AFK toggle (A)
|
- [ ] AFK toggle (A)
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"github.com/Cynosphere/comcord/lib"
|
"github.com/Cynosphere/comcord/lib"
|
||||||
"github.com/Cynosphere/comcord/state"
|
"github.com/Cynosphere/comcord/state"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/ergochat/readline"
|
|
||||||
"github.com/mgutz/ansi"
|
"github.com/mgutz/ansi"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,7 +15,7 @@ func SendMode(session *discordgo.Session) {
|
||||||
channelId := state.GetCurrentChannel()
|
channelId := state.GetCurrentChannel()
|
||||||
if channelId == "" {
|
if channelId == "" {
|
||||||
fmt.Print("<not in a channel>\n\r")
|
fmt.Print("<not in a channel>\n\r")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
state.SetInPrompt(true)
|
state.SetInPrompt(true)
|
||||||
|
@ -29,32 +28,22 @@ func SendMode(session *discordgo.Session) {
|
||||||
prompt = ansi.Color(prompt, "cyan+b")
|
prompt = ansi.Color(prompt, "cyan+b")
|
||||||
}
|
}
|
||||||
|
|
||||||
input, _ := readline.NewFromConfig(&readline.Config{
|
lib.MakePrompt(session, prompt, true, func(session *discordgo.Session, input string, interrupt bool) {
|
||||||
Prompt: prompt,
|
if input == "" {
|
||||||
UniqueEditLine: true,
|
if interrupt {
|
||||||
})
|
fmt.Print("^C<no message sent>\n\r")
|
||||||
defer input.Close()
|
} else {
|
||||||
|
fmt.Print(prompt, "<no message sent>\n\r")
|
||||||
out, err := input.Readline()
|
}
|
||||||
out = strings.TrimSpace(out)
|
|
||||||
input.Close()
|
|
||||||
|
|
||||||
if out == "" {
|
|
||||||
if err == readline.ErrInterrupt {
|
|
||||||
fmt.Print("^C<no message sent>\n\r")
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Print(prompt, "<no message sent>\n\r")
|
fmt.Print(prompt, input, "\n\r")
|
||||||
}
|
_, err := session.ChannelMessageSend(channelId, input)
|
||||||
} else {
|
|
||||||
fmt.Print(prompt, out, "\n\r")
|
|
||||||
_, err := session.ChannelMessageSend(channelId, out)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Print("<failed to send message: ", err, ">\n\r")
|
fmt.Print("<failed to send message: ", err, ">\n\r")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: update afk state
|
// TODO: update afk state
|
||||||
}
|
}
|
||||||
state.SetInPrompt(false)
|
})
|
||||||
lib.ProcessQueue(session)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
|
@ -15,6 +15,7 @@ type ComcordState struct {
|
||||||
CurrentChannel string
|
CurrentChannel string
|
||||||
NameLength int
|
NameLength int
|
||||||
InPrompt bool
|
InPrompt bool
|
||||||
|
PromptText string
|
||||||
AFK bool
|
AFK bool
|
||||||
MessageQueue []*discordgo.Message
|
MessageQueue []*discordgo.Message
|
||||||
LastChannel map[string]string
|
LastChannel map[string]string
|
||||||
|
@ -34,6 +35,7 @@ func Setup(config map[string]string) {
|
||||||
state.CurrentChannel = ""
|
state.CurrentChannel = ""
|
||||||
state.NameLength = 2
|
state.NameLength = 2
|
||||||
state.InPrompt = false
|
state.InPrompt = false
|
||||||
|
state.PromptText = ""
|
||||||
state.AFK = false
|
state.AFK = false
|
||||||
state.MessageQueue = make([]*discordgo.Message, 0)
|
state.MessageQueue = make([]*discordgo.Message, 0)
|
||||||
state.LastChannel = make(map[string]string)
|
state.LastChannel = make(map[string]string)
|
||||||
|
@ -93,6 +95,14 @@ func SetInPrompt(value bool) {
|
||||||
state.InPrompt = value
|
state.InPrompt = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetPromptText() string {
|
||||||
|
return state.PromptText
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetPromptText(value string) {
|
||||||
|
state.PromptText = value
|
||||||
|
}
|
||||||
|
|
||||||
func IsAFK() bool {
|
func IsAFK() bool {
|
||||||
return state.AFK
|
return state.AFK
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue