Browse Source

Merge pull request #26 from Rudi9719/linting/tabcomplete

Linting/tabcomplete
pull/25/head
Gregory Rudolph 5 years ago committed by GitHub
parent
commit
ade1799115
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      cmdEdit.go
  2. 2
      cmdHelp.go
  3. 12
      cmdReact.go
  4. 40
      cmdSet.go
  5. 60
      main.go
  6. 99
      tabComplete.go

21
cmdEdit.go

@ -11,7 +11,7 @@ import (
func init() { func init() {
command := Command{ command := Command{
Cmd: []string{"edit", "e"}, Cmd: []string{"edit", "e"},
Description: "$messageId - Edit a message (messageID is optional)", Description: "$messageID - Edit a message (messageID is optional)",
Help: "", Help: "",
Exec: cmdEdit, Exec: cmdEdit,
} }
@ -20,22 +20,22 @@ func init() {
} }
func cmdEdit(cmd []string) { func cmdEdit(cmd []string) {
var messageId int var messageID int
chat := k.NewChat(channel) chat := k.NewChat(channel)
if len(cmd) == 2 || len(cmd) == 1 { if len(cmd) == 2 || len(cmd) == 1 {
if len(cmd) == 2 { if len(cmd) == 2 {
messageId, _ = strconv.Atoi(cmd[1]) messageID, _ = strconv.Atoi(cmd[1])
} else if lastMessage.ID != 0 { } else if lastMessage.ID != 0 {
if lastMessage.Type != "text" { if lastMessage.Type != "text" {
printToView("Feed", "Last message isn't editable (is it an edit?)") printToView("Feed", "Last message isn't editable (is it an edit?)")
return return
} }
messageId = lastMessage.ID messageID = lastMessage.ID
} else { } else {
printToView("Feed", "No message to edit") printToView("Feed", "No message to edit")
return return
} }
origMessage, _ := chat.ReadMessage(messageId) origMessage, _ := chat.ReadMessage(messageID)
if origMessage.Result.Messages[0].Msg.Content.Type != "text" { if origMessage.Result.Messages[0].Msg.Content.Type != "text" {
printToView("Feed", fmt.Sprintf("%+v", origMessage)) printToView("Feed", fmt.Sprintf("%+v", origMessage))
return return
@ -47,19 +47,20 @@ func cmdEdit(cmd []string) {
editString := origMessage.Result.Messages[0].Msg.Content.Text.Body editString := origMessage.Result.Messages[0].Msg.Content.Text.Body
clearView("Edit") clearView("Edit")
popupView("Edit") popupView("Edit")
printToView("Edit", fmt.Sprintf("/e %d %s", messageId, editString)) printToView("Edit", fmt.Sprintf("/e %d %s", messageID, editString))
setViewTitle("Edit", fmt.Sprintf(" Editing message %d ", messageId)) setViewTitle("Edit", fmt.Sprintf(" Editing message %d ", messageID))
moveCursorToEnd("Edit")
return return
} }
if len(cmd) < 3 { if len(cmd) < 3 {
printToView("Feed", "Not enough options for Edit") printToView("Feed", "Not enough options for Edit")
return return
} }
messageId, _ = strconv.Atoi(cmd[1]) messageID, _ = strconv.Atoi(cmd[1])
newMessage := strings.Join(cmd[2:], " ") newMessage := strings.Join(cmd[2:], " ")
_, err := chat.Edit(messageId, newMessage) _, err := chat.Edit(messageID, newMessage)
if err != nil { if err != nil {
printToView("Feed", fmt.Sprintf("Error editing message %d, %+v", messageId, err)) printToView("Feed", fmt.Sprintf("Error editing message %d, %+v", messageID, err))
} }
} }

2
cmdHelp.go

@ -28,7 +28,7 @@ func cmdHelp(cmd []string) {
helpText = fmt.Sprintf("%s%s%s\t\t%s\n", helpText, cmdPrefix, c, commands[c].Description) helpText = fmt.Sprintf("%s%s%s\t\t%s\n", helpText, cmdPrefix, c, commands[c].Description)
} }
if len(typeCommands) > 0 { if len(typeCommands) > 0 {
for c, _ := range typeCommands { for c := range typeCommands {
tCommands = append(tCommands, typeCommands[c].Name) tCommands = append(tCommands, typeCommands[c].Name)
} }
sort.Strings(tCommands) sort.Strings(tCommands)

12
cmdReact.go

@ -10,7 +10,7 @@ import (
func init() { func init() {
command := Command{ command := Command{
Cmd: []string{"react", "r", "+"}, Cmd: []string{"react", "r", "+"},
Description: "$messageId $reaction - React to a message (messageID is optional)", Description: "$messageID $reaction - React to a message (messageID is optional)",
Help: "", Help: "",
Exec: cmdReact, Exec: cmdReact,
} }
@ -20,7 +20,7 @@ func init() {
func cmdReact(cmd []string) { func cmdReact(cmd []string) {
if len(cmd) > 2 { if len(cmd) > 2 {
reactToMessageId(cmd[1], strings.Join(cmd[2:], " ")) reactToMessageID(cmd[1], strings.Join(cmd[2:], " "))
} else if len(cmd) == 2 { } else if len(cmd) == 2 {
reactToMessage(cmd[1]) reactToMessage(cmd[1])
} }
@ -30,13 +30,13 @@ func cmdReact(cmd []string) {
func reactToMessage(reaction string) { func reactToMessage(reaction string) {
doReact(lastMessage.ID, reaction) doReact(lastMessage.ID, reaction)
} }
func reactToMessageId(messageId string, reaction string) { func reactToMessageID(messageID string, reaction string) {
ID, _ := strconv.Atoi(messageId) ID, _ := strconv.Atoi(messageID)
doReact(ID, reaction) doReact(ID, reaction)
} }
func doReact(messageId int, reaction string) { func doReact(messageID int, reaction string) {
chat := k.NewChat(channel) chat := k.NewChat(channel)
_, err := chat.React(messageId, reaction) _, err := chat.React(messageID, reaction)
if err != nil { if err != nil {
printToView("Feed", "There was an error reacting to the message.") printToView("Feed", "There was an error reacting to the message.")
} }

40
cmdSet.go

@ -19,32 +19,34 @@ func init() {
RegisterCommand(command) RegisterCommand(command)
} }
func printSetting(cmd []string) {
switch cmd[1] {
case "load":
loadFromToml()
printToView("Feed", fmt.Sprintf("Loading config from toml"))
case "downloadPath":
printToView("Feed", fmt.Sprintf("Setting for %s -> %s", cmd[1], downloadPath))
case "outputFormat":
printToView("Feed", fmt.Sprintf("Setting for %s -> %s", cmd[1], outputFormat))
case "dateFormat":
printToView("Feed", fmt.Sprintf("Setting for %s -> %s", cmd[1], dateFormat))
case "timeFormat":
printToView("Feed", fmt.Sprintf("Setting for %s -> %s", cmd[1], timeFormat))
case "cmdPrefix":
printToView("Feed", fmt.Sprintf("Setting for %s -> %s", cmd[1], cmdPrefix))
default:
printToView("Feed", fmt.Sprintf("Unknown config value %s", cmd[1]))
}
return
}
func cmdSet(cmd []string) { func cmdSet(cmd []string) {
if len(cmd) < 2 { if len(cmd) < 2 {
printToView("Feed", "No config value specified") printToView("Feed", "No config value specified")
return return
} }
if len(cmd) < 3 { if len(cmd) < 3 {
switch cmd[1] { printSetting(cmd)
case "load":
loadFromToml()
printToView("Feed", fmt.Sprintf("Loading config from toml"))
case "downloadPath":
printToView("Feed", fmt.Sprintf("Setting for %s -> %s", cmd[1], downloadPath))
case "outputFormat":
printToView("Feed", fmt.Sprintf("Setting for %s -> %s", cmd[1], outputFormat))
case "dateFormat":
printToView("Feed", fmt.Sprintf("Setting for %s -> %s", cmd[1], dateFormat))
case "timeFormat":
printToView("Feed", fmt.Sprintf("Setting for %s -> %s", cmd[1], timeFormat))
case "cmdPrefix":
printToView("Feed", fmt.Sprintf("Setting for %s -> %s", cmd[1], cmdPrefix))
default:
printToView("Feed", fmt.Sprintf("Unknown config value %s", cmd[1]))
}
return
} }
switch cmd[1] { switch cmd[1] {
case "downloadPath": case "downloadPath":

60
main.go

@ -114,9 +114,8 @@ func initKeybindings() error {
if input != "" { if input != "" {
clearView("Input") clearView("Input")
return nil return nil
} else {
return gocui.ErrQuit
} }
return gocui.ErrQuit
}); err != nil { }); err != nil {
return err return err
} }
@ -168,9 +167,8 @@ func setViewTitle(viewName string, title string) {
updatingView, err := g.View(viewName) updatingView, err := g.View(viewName)
if err != nil { if err != nil {
return err return err
} else {
updatingView.Title = title
} }
updatingView.Title = title
return nil return nil
}) })
} }
@ -180,9 +178,9 @@ func getViewTitle(viewName string) string {
// in case there is active tab completion, filter that to just the view title and not the completion options. // in case there is active tab completion, filter that to just the view title and not the completion options.
printToView("Feed", fmt.Sprintf("Error getting view title: %s", err)) printToView("Feed", fmt.Sprintf("Error getting view title: %s", err))
return "" return ""
} else {
return strings.Split(view.Title, "||")[0]
} }
return strings.Split(view.Title, "||")[0]
} }
func popupView(viewName string) { func popupView(viewName string) {
_, err := g.SetCurrentView(viewName) _, err := g.SetCurrentView(viewName)
@ -197,10 +195,27 @@ func popupView(viewName string) {
updatingView, err := g.View(viewName) updatingView, err := g.View(viewName)
if err != nil { if err != nil {
return err return err
} else {
viewX, viewY := updatingView.Size()
updatingView.MoveCursor(viewX, viewY, true)
} }
updatingView.MoveCursor(0, 0, true)
return nil
})
}
func moveCursorToEnd(viewName string) {
g.Update(func(g *gocui.Gui) error {
inputView, err := g.View(viewName)
if err != nil {
return err
}
inputString, _ := getInputString(viewName)
stringLen := len(inputString)
maxX, _ := inputView.Size()
x := stringLen % maxX
y := stringLen / maxX
inputView.SetCursor(0, 0)
inputView.SetOrigin(0, 0)
inputView.MoveCursor(x, y, true)
return nil return nil
}) })
@ -210,11 +225,11 @@ func clearView(viewName string) {
inputView, err := g.View(viewName) inputView, err := g.View(viewName)
if err != nil { if err != nil {
return err return err
} else {
inputView.Clear()
inputView.SetCursor(0, 0)
inputView.SetOrigin(0, 0)
} }
inputView.Clear()
inputView.SetCursor(0, 0)
inputView.SetOrigin(0, 0)
return nil return nil
}) })
@ -224,11 +239,11 @@ func writeToView(viewName string, message string) {
updatingView, err := g.View(viewName) updatingView, err := g.View(viewName)
if err != nil { if err != nil {
return err return err
} else {
for _, c := range message {
updatingView.EditWrite(c)
}
} }
for _, c := range message {
updatingView.EditWrite(c)
}
return nil return nil
}) })
} }
@ -237,9 +252,8 @@ func printToView(viewName string, message string) {
updatingView, err := g.View(viewName) updatingView, err := g.View(viewName)
if err != nil { if err != nil {
return err return err
} else {
fmt.Fprintf(updatingView, "%s\n", message)
} }
fmt.Fprintf(updatingView, "%s\n", message)
return nil return nil
}) })
} }
@ -275,11 +289,11 @@ func populateChat() {
if err2 != nil { if err2 != nil {
printToView("Feed", fmt.Sprintf("%+v", err)) printToView("Feed", fmt.Sprintf("%+v", err))
return return
} else {
go populateChat()
go generateChannelTabCompletionSlice()
return
} }
go populateChat()
go generateChannelTabCompletionSlice()
return
} }
var printMe []string var printMe []string
var actuallyPrintMe string var actuallyPrintMe string

99
tabComplete.go

@ -20,46 +20,46 @@ func handleTab(viewName string) error {
inputString, err := getInputString(viewName) inputString, err := getInputString(viewName)
if err != nil { if err != nil {
return err return err
}
// if you successfully get an input string, grab the last word from the string
ss := regexp.MustCompile(`[ #]`).Split(inputString, -1)
s := ss[len(ss)-1]
// create a variable in which to store the result
var resultSlice []string
// if the word starts with a : its an emoji lookup
if strings.HasPrefix(s, ":") {
resultSlice = getEmojiTabCompletionSlice(s)
} else if strings.HasPrefix(s, "/") {
generateCommandTabCompletionSlice()
s = strings.Replace(s, "/", "", 1)
resultSlice = getCommandTabCompletionSlice(s)
} else { } else {
// if you successfully get an input string, grab the last word from the string if strings.HasPrefix(s, "@") {
ss := regexp.MustCompile(`[ #]`).Split(inputString, -1) // now in case the word (s) is a mention @something, lets remove it to normalize
s := ss[len(ss)-1] s = strings.Replace(s, "@", "", 1)
// create a variable in which to store the result
var resultSlice []string
// if the word starts with a : its an emoji lookup
if strings.HasPrefix(s, ":") {
resultSlice = getEmojiTabCompletionSlice(s)
} else if strings.HasPrefix(s, "/") {
generateCommandTabCompletionSlice()
s = strings.Replace(s, "/", "", 1)
resultSlice = getCommandTabCompletionSlice(s)
} else {
if strings.HasPrefix(s, "@") {
// now in case the word (s) is a mention @something, lets remove it to normalize
s = strings.Replace(s, "@", "", 1)
}
// now call get the list of all possible cantidates that have that as a prefix
resultSlice = getChannelTabCompletionSlice(s)
} }
rLen := len(resultSlice) // now call get the list of all possible cantidates that have that as a prefix
lcp := longestCommonPrefix(resultSlice) resultSlice = getChannelTabCompletionSlice(s)
if lcp != "" { }
originalViewTitle := getViewTitle("Input") rLen := len(resultSlice)
newViewTitle := "" lcp := longestCommonPrefix(resultSlice)
if rLen >= 1 && originalViewTitle != "" { if lcp != "" {
if rLen == 1 { originalViewTitle := getViewTitle("Input")
newViewTitle = originalViewTitle newViewTitle := ""
} else if rLen <= 5 { if rLen >= 1 && originalViewTitle != "" {
newViewTitle = fmt.Sprintf("%s|| %s", originalViewTitle, strings.Join(resultSlice, " ")) if rLen == 1 {
} else if rLen > 5 { newViewTitle = originalViewTitle
newViewTitle = fmt.Sprintf("%s|| %s +%d more", originalViewTitle, strings.Join(resultSlice[:6], " "), rLen-5) } else if rLen <= 5 {
} newViewTitle = fmt.Sprintf("%s|| %s", originalViewTitle, strings.Join(resultSlice, " "))
setViewTitle(viewName, newViewTitle) } else if rLen > 5 {
remainder := stringRemainder(s, lcp) newViewTitle = fmt.Sprintf("%s|| %s +%d more", originalViewTitle, strings.Join(resultSlice[:6], " "), rLen-5)
writeToView(viewName, remainder)
} }
setViewTitle(viewName, newViewTitle)
remainder := stringRemainder(s, lcp)
writeToView(viewName, remainder)
} }
} }
return nil return nil
} }
@ -123,22 +123,23 @@ func getCurrentChannelMembership() []string {
var rs []string var rs []string
if channel.Name != "" { if channel.Name != "" {
t := k.NewTeam(channel.Name) t := k.NewTeam(channel.Name)
if testVar, err := t.MemberList(); err != nil { testVar, err := t.MemberList()
if err != nil {
return rs // then this isn't a team, its a PM or there was an error in the API call return rs // then this isn't a team, its a PM or there was an error in the API call
} else {
for _, m := range testVar.Result.Members.Owners {
rs = append(rs, fmt.Sprintf("%+v", m.Username))
}
for _, m := range testVar.Result.Members.Admins {
rs = append(rs, fmt.Sprintf("%+v", m.Username))
}
for _, m := range testVar.Result.Members.Writers {
rs = append(rs, fmt.Sprintf("%+v", m.Username))
}
for _, m := range testVar.Result.Members.Readers {
rs = append(rs, fmt.Sprintf("%+v", m.Username))
}
} }
for _, m := range testVar.Result.Members.Owners {
rs = append(rs, fmt.Sprintf("%+v", m.Username))
}
for _, m := range testVar.Result.Members.Admins {
rs = append(rs, fmt.Sprintf("%+v", m.Username))
}
for _, m := range testVar.Result.Members.Writers {
rs = append(rs, fmt.Sprintf("%+v", m.Username))
}
for _, m := range testVar.Result.Members.Readers {
rs = append(rs, fmt.Sprintf("%+v", m.Username))
}
} }
return rs return rs
} }

Loading…
Cancel
Save