diff --git a/README.md b/README.md index 07d8591..f145b1a 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/commands/send.go b/commands/send.go index fba8a53..205a58c 100644 --- a/commands/send.go +++ b/commands/send.go @@ -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("\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\n\r") + lib.MakePrompt(session, prompt, true, func(session *discordgo.Session, input string, interrupt bool) { + if input == "" { + if interrupt { + fmt.Print("^C\n\r") + } else { + fmt.Print(prompt, "\n\r") + } } else { - fmt.Print(prompt, "\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("\n\r") - } + if err != nil { + fmt.Print("\n\r") + } - // TODO: update afk state - } - state.SetInPrompt(false) - lib.ProcessQueue(session) + // TODO: update afk state + } + }) } diff --git a/lib/prompt.go b/lib/prompt.go new file mode 100644 index 0000000..a85709f --- /dev/null +++ b/lib/prompt.go @@ -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) +} diff --git a/state/main.go b/state/main.go index 4499601..cd63ec8 100644 --- a/state/main.go +++ b/state/main.go @@ -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 }