Browse Source

New command stuff

pull/1/head
Sam 5 years ago
parent
commit
c4c209fb88
  1. 104
      main.go
  2. 12
      types.go

104
main.go

@ -13,6 +13,8 @@ import (
const cmdPrefix = "/" const cmdPrefix = "/"
var commands = make(map[string]Command)
// Configurable section // Configurable section
var downloadPath = "/tmp/" var downloadPath = "/tmp/"
var outputFormat = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG" var outputFormat = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG"
@ -356,86 +358,48 @@ func reactToMessageId(messageId string, reaction string) {
chat.React(ID, reaction) chat.React(ID, reaction)
} }
func handleInput(g *gocui.Gui) error { func handleInput(g *gocui.Gui) error {
clearView(g, "Input")
inputString, _ := getInputString(g) inputString, _ := getInputString(g)
if inputString == "" { if inputString == "" {
return nil return nil
} }
command := strings.Split(inputString, " ") if strings.HasPrefix(inputString, cmdPrefix) {
cmd := strings.Split(inputString[len(cmdPrefix):], " ")
switch strings.ToLower(command[0]) { if c, ok := commands[cmd[0]]; ok {
case "/q": c.Exec(g, cmd)
return gocui.ErrQuit return nil
case "/j": } else if cmd[0] == "q" || cmd[0] == "quit" {
stream = false return gocui.ErrQuit
if len(command) == 3 {
channel.MembersType = keybase.TEAM
channel.Name = command[1]
channel.TopicName = command[2]
printToView(g, "Feed", fmt.Sprintf("You are joining: @%s#%s", channel.Name, channel.TopicName))
clearView(g, "Chat")
go populateChat(g)
} else if len(command) == 2 {
channel.MembersType = keybase.USER
channel.Name = command[1]
channel.TopicName = ""
printToView(g, "Feed", fmt.Sprintf("You are joining: @%s", channel.Name))
clearView(g, "Chat")
go populateChat(g)
} else { } else {
printToView(g, "Feed", "To join a team use /j <team> <channel>") printToView(g, "Feed", fmt.Sprintf("Command '%s' not recognized", cmd[0]))
printToView(g, "Feed", "To join a PM use /j <user>") return nil
}
case "/u":
if len(command) == 3 {
filePath := command[1]
fileName := command[2]
uploadFile(g, filePath, fileName)
} else if len(command) == 2 {
filePath := command[1]
fileName := ""
uploadFile(g, filePath, fileName)
} else {
printToView(g, "Feed", "To upload a file, supply full path and optional title (no spaces)")
}
case "/d":
if len(command) == 3 {
messageId, err := strconv.Atoi(command[1])
if err != nil {
printToView(g, "Feed", "Invalid message ID")
} else {
fileName := command[2]
downloadFile(g, messageId, fileName)
}
} else if len(command) == 2 {
messageId, err := strconv.Atoi(command[1])
if err != nil {
printToView(g, "Feed", "Invalid message ID")
} else {
downloadFile(g, messageId, command[1])
}
} }
case "/s":
clearView(g, "Chat")
stream = true
printToView(g, "Feed", "You have begun viewing the formatted stream.")
case "/r":
if len(command) == 3 {
reactToMessageId(command[1], command[2])
} else {
printToView(g, "Feed", "/r $messageId $desiredReaction")
}
default:
if inputString[:1] == "+" {
reactToMessage(strings.Replace(inputString, "+", "", 1))
} else {
go sendChat(inputString, g)
}
go populateList(g)
} }
clearView(g, "Input") if inputString[:1] == "+" {
reactToMessage(strings.Replace(inputString, "+", "", 1))
} else {
go sendChat(inputString, g)
}
go populateList(g)
return nil return nil
} }
func quit(g *gocui.Gui, v *gocui.View) error { func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit return gocui.ErrQuit
} }
// RegisterCommand registers a command to be used within the client
func RegisterCommand(c Command) error {
var notAdded string
for _, cmd := range c.Cmd {
if _, ok := commands[cmd]; !ok {
commands[cmd] = c
continue
}
notAdded = fmt.Sprintf("%s, %s", notAdded, cmd)
}
if notAdded != "" {
return fmt.Errorf("The following aliases were not added because they already exist: %s", notAdded)
}
return nil
}

12
types.go

@ -1,9 +1,13 @@
package main package main
import (
"github.com/jroimartin/gocui"
)
// Command outlines a command // Command outlines a command
type Command struct { type Command struct {
Cmd []string // Any aliases that trigger this command Cmd []string // Any aliases that trigger this command
Description string // A short description of the command Description string // A short description of the command
Help string // The full help text explaining how to use the command Help string // The full help text explaining how to use the command
Exec func([]string) // A function that takes the command (arg[0]) and any arguments (arg[1:]) as input Exec func(*gocui.Gui, []string) // A function that takes the command (arg[0]) and any arguments (arg[1:]) as input
} }

Loading…
Cancel
Save