prompt util func to reduce code reuse

This commit is contained in:
Cynthia Foxwell 2023-07-10 21:41:51 -06:00
parent f21c414f1b
commit 1593736c70
4 changed files with 61 additions and 29 deletions

View File

@ -13,7 +13,7 @@ Go is more portable than Node.js
## TODO
- [x] Send mode
- [ ] Commands
- [x] Commands
- [x] Quit (q)
- [ ] Switch guilds (G)
- [ ] Switch channels (g)
@ -29,7 +29,7 @@ Go is more portable than Node.js
- [ ] Cross-guild peek (P)
- [ ] List channels (l)
- [ ] List guilds (L)
- [ ] Clear (c)
- [x] Clear (c)
- [ ] Surf channels forwards (>)
- [ ] Surf channels backwards (<)
- [ ] AFK toggle (A)

View File

@ -8,7 +8,6 @@ import (
"github.com/Cynosphere/comcord/lib"
"github.com/Cynosphere/comcord/state"
"github.com/bwmarrin/discordgo"
"github.com/ergochat/readline"
"github.com/mgutz/ansi"
)
@ -16,7 +15,7 @@ func SendMode(session *discordgo.Session) {
channelId := state.GetCurrentChannel()
if channelId == "" {
fmt.Print("<not in a channel>\n\r")
return
return
}
state.SetInPrompt(true)
@ -29,32 +28,22 @@ func SendMode(session *discordgo.Session) {
prompt = ansi.Color(prompt, "cyan+b")
}
input, _ := readline.NewFromConfig(&readline.Config{
Prompt: prompt,
UniqueEditLine: true,
})
defer input.Close()
out, err := input.Readline()
out = strings.TrimSpace(out)
input.Close()
if out == "" {
if err == readline.ErrInterrupt {
fmt.Print("^C<no message sent>\n\r")
lib.MakePrompt(session, prompt, true, func(session *discordgo.Session, input string, interrupt bool) {
if input == "" {
if interrupt {
fmt.Print("^C<no message sent>\n\r")
} else {
fmt.Print(prompt, "<no message sent>\n\r")
}
} else {
fmt.Print(prompt, "<no message sent>\n\r")
}
} else {
fmt.Print(prompt, out, "\n\r")
_, err := session.ChannelMessageSend(channelId, out)
fmt.Print(prompt, input, "\n\r")
_, err := session.ChannelMessageSend(channelId, input)
if err != nil {
fmt.Print("<failed to send message: ", err, ">\n\r")
}
if err != nil {
fmt.Print("<failed to send message: ", err, ">\n\r")
}
// TODO: update afk state
}
state.SetInPrompt(false)
lib.ProcessQueue(session)
// TODO: update afk state
}
})
}

33
lib/prompt.go Normal file
View 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)
}

View File

@ -15,6 +15,7 @@ type ComcordState struct {
CurrentChannel string
NameLength int
InPrompt bool
PromptText string
AFK bool
MessageQueue []*discordgo.Message
LastChannel map[string]string
@ -34,6 +35,7 @@ func Setup(config map[string]string) {
state.CurrentChannel = ""
state.NameLength = 2
state.InPrompt = false
state.PromptText = ""
state.AFK = false
state.MessageQueue = make([]*discordgo.Message, 0)
state.LastChannel = make(map[string]string)
@ -93,6 +95,14 @@ func SetInPrompt(value bool) {
state.InPrompt = value
}
func GetPromptText() string {
return state.PromptText
}
func SetPromptText(value string) {
state.PromptText = value
}
func IsAFK() bool {
return state.AFK
}