From 5cc22635eec2fd5801c7f2a7464368653de5d539 Mon Sep 17 00:00:00 2001 From: Gregory Rudolph Date: Thu, 10 Oct 2019 13:44:41 -0400 Subject: [PATCH 1/6] Remove unused layout2() --- main.go | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/main.go b/main.go index ce1340f..727436c 100644 --- a/main.go +++ b/main.go @@ -393,27 +393,6 @@ func layout(g *gocui.Gui) error { } return nil } -func layout2(g *gocui.Gui) error { - maxX, maxY := g.Size() - if feedView, err := g.SetView("Feed2", maxX/2-maxX/3, 0, maxX-1, maxY/5, 0); err != nil { - if !gocui.IsUnknownView(err) { - return err - } - feedView.Autoscroll = true - feedView.Wrap = true - fmt.Fprintln(feedView, "Feed Window - If you are mentioned or receive a PM it will show here") - } - if chatView, err2 := g.SetView("Chat2", maxX/2-maxX/3, maxY/5+1, maxX-1, maxY-5, 0); err2 != nil { - if !gocui.IsUnknownView(err2) { - return err2 - } - chatView.Autoscroll = true - chatView.Wrap = true - fmt.Fprintf(chatView, "Welcome %s!\n\nYour chats will appear here.\nSupported commands are as follows:\n\n", k.Username) - RunCommand("help") - } - return nil -} func getInputString(viewName string) (string, error) { inputView, err := g.View(viewName) From 39cb8f2519a4ce75e36d3c429caf34490e397773 Mon Sep 17 00:00:00 2001 From: Gregory Rudolph Date: Fri, 11 Oct 2019 07:55:33 -0400 Subject: [PATCH 2/6] Wallet cmd for sending transactions etc --- cmdWallet.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 cmdWallet.go diff --git a/cmdWallet.go b/cmdWallet.go new file mode 100644 index 0000000..4f610ca --- /dev/null +++ b/cmdWallet.go @@ -0,0 +1,63 @@ +// ignore +// +build allcommands walletcmd + +package main + +import ( + "fmt" + "math/rand" + "strings" + "time" +) + +var walletConfirmationCode string +var walletConfirmationUser string +var walletTransactionAmnt string + +func init() { + command := Command{ + Cmd: []string{"wallet", "confirm"}, + Description: "$user $amount / $user $confirmation - Send or confirm a wallet payment", + Help: "", + Exec: cmdWallet, + } + + RegisterCommand(command) +} + +func cmdWallet(cmd []string) { + if len(cmd) < 3 { + return + } + if cmd[0] == "wallet" { + rand.Seed(time.Now().UnixNano()) + chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + + "abcdefghijklmnopqrstuvwxyz" + + "0123456789") + length := 5 + var b strings.Builder + for i := 0; i < length; i++ { + b.WriteRune(chars[rand.Intn(len(chars))]) + } + walletConfirmationCode = b.String() + walletConfirmationUser = cmd[1] + walletTransactionAmnt = cmd[2] + printToView("Feed", fmt.Sprintf("To confirm sending %s to %s, type /confirm %s %s", cmd[2], cmd[1], cmd[1], walletConfirmationCode)) + + } else if cmd[0] == "confirm" { + if cmd[1] == walletConfirmationUser && cmd[2] == walletConfirmationCode { + txWallet := k.NewWallet() + wAPI, err := txWallet.SendXLM(walletConfirmationUser, walletTransactionAmnt, "") + if err != nil { + printToView("Feed", fmt.Sprintf("There was an error with your wallet tx:\n\t%+v", err)) + } else { + printToView("Feed", fmt.Sprintf("You have sent %sXLM to %s with tx ID: %s", wAPI.Result.Amount, wAPI.Result.ToUsername, wAPI.Result.TxID)) + } + + } else { + printToView("Feed", "There was an error validating your confirmation. Your wallet has been untouched.") + } + + } + +} From 301ead6e3af8cec14a39a0407a54abb221e0b810 Mon Sep 17 00:00:00 2001 From: Gregory Rudolph Date: Fri, 11 Oct 2019 08:11:42 -0400 Subject: [PATCH 3/6] Change from BufferLines() to Buffer() to get rid of spaces at line breaks --- main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 727436c..6df5796 100644 --- a/main.go +++ b/main.go @@ -399,7 +399,8 @@ func getInputString(viewName string) (string, error) { if err != nil { return "", err } - retString := strings.Join(inputView.BufferLines(), " ") + retString := inputView.Buffer() + retString = strings.ReplaceAll(retString, "\n", "") return retString, err } From c1e0e1452a3cd0104f1249f641d7f1e5147128f4 Mon Sep 17 00:00:00 2001 From: Gregory Rudolph Date: Fri, 11 Oct 2019 08:45:34 -0400 Subject: [PATCH 4/6] Don't try to edit other peoples messages --- cmdEdit.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmdEdit.go b/cmdEdit.go index aaebe84..cd4fdff 100644 --- a/cmdEdit.go +++ b/cmdEdit.go @@ -34,6 +34,10 @@ func cmdEdit(cmd []string) { printToView("Feed", fmt.Sprintf("%+v", origMessage)) return } + if origMessage.Result.Messages[0].Msg.Sender.Username != k.Username { + printToView("Feed", "You cannot edit another user's messages.") + return + } editString := origMessage.Result.Messages[0].Msg.Content.Text.Body clearView("Edit") popupView("Edit") From 2650e06f2d4ca26697ee6345ff2dff537af08efa Mon Sep 17 00:00:00 2001 From: Gregory Rudolph Date: Fri, 11 Oct 2019 10:00:53 -0400 Subject: [PATCH 5/6] First step to configurations --- cmdSet.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 cmdSet.go diff --git a/cmdSet.go b/cmdSet.go new file mode 100644 index 0000000..fabdc06 --- /dev/null +++ b/cmdSet.go @@ -0,0 +1,64 @@ +// +build !rm_basic_commands allcommands setcmd + +package main + +import ( + "fmt" + "strings" +) + +func init() { + command := Command{ + Cmd: []string{"set", "config"}, + Description: "Change various settings", + Help: "", + Exec: cmdSet, + } + + RegisterCommand(command) +} + +func cmdSet(cmd []string) { + if len(cmd) < 2 { + printToView("Feed", "No config value specified") + return + } + if len(cmd) < 3 { + switch cmd[1] { + case "load": + printToView("Feed", "Load values from file?") + 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] { + case "downloadPath": + if len(cmd) != 3 { + printToView("Feed", "Invalid download path.") + } + downloadPath = cmd[2] + case "outputFormat": + outputFormat = strings.Join(cmd[1:], " ") + case "dateFormat": + dateFormat = strings.Join(cmd[1:], " ") + case "timeFormat": + timeFormat = strings.Join(cmd[1:], " ") + case "cmdPrefix": + cmdPrefix = cmd[2] + default: + printToView("Feed", fmt.Sprintf("Unknown config value %s", cmd[1])) + } + +} From 4d24c38fe8da483fca38551934348ab9cb903b4e Mon Sep 17 00:00:00 2001 From: Gregory Rudolph Date: Fri, 11 Oct 2019 10:30:27 -0400 Subject: [PATCH 6/6] Go fmt --- main.go | 216 +++++++++++++++++++++---------------------- tcmdShowReactions.go | 2 +- 2 files changed, 109 insertions(+), 109 deletions(-) diff --git a/main.go b/main.go index 24e581e..f1247fd 100644 --- a/main.go +++ b/main.go @@ -203,108 +203,108 @@ func populateList() { } func filterStringSlice(ss []string, fv string) []string { - var rs []string - for _, s := range ss { - if strings.HasPrefix(s, fv) { - rs = append(rs, s) - } - } - return rs + var rs []string + for _, s := range ss { + if strings.HasPrefix(s, fv) { + rs = append(rs, s) + } + } + return rs } func longestCommonPrefix(ss []string) string { - // cover the case where the slice has no or one members - switch len(ss) { - case 0: - return "" - case 1: - return ss[0] - } - // all strings are compared by bytes here forward (TBD unicode normalization?) - // establish min, max lenth members of the slice by iterating over the members - min, max := ss[0], ss[0] - for _, s := range ss[1:] { - switch { - case s < min: - min = s - case s > max: - max = s - } - } - // then iterate over the characters from min to max, as soon as chars don't match return - for i := 0; i < len(min) && i < len(max); i++ { - if min[i] != max[i] { - return min[:i] - } - } - // to cover the case where all members are equal, just return one - return min + // cover the case where the slice has no or one members + switch len(ss) { + case 0: + return "" + case 1: + return ss[0] + } + // all strings are compared by bytes here forward (TBD unicode normalization?) + // establish min, max lenth members of the slice by iterating over the members + min, max := ss[0], ss[0] + for _, s := range ss[1:] { + switch { + case s < min: + min = s + case s > max: + max = s + } + } + // then iterate over the characters from min to max, as soon as chars don't match return + for i := 0; i < len(min) && i < len(max); i++ { + if min[i] != max[i] { + return min[:i] + } + } + // to cover the case where all members are equal, just return one + return min } func stringRemainder(aStr, bStr string) string { - var long, short string - //figure out which string is longer - switch { - case len(aStr) < len (bStr): - short = aStr - long = bStr - default: - short = bStr - long = aStr - } - // iterate over the strings using an external iterator so we don't lose the value - i := 0 - for i < len(short) && i < len(long) { - if short[i] != long[i] { - // the strings aren't equal so don't return anything - return "" - } - i++ - } - // return whatever's left of the longer string - return long[i:] + var long, short string + //figure out which string is longer + switch { + case len(aStr) < len(bStr): + short = aStr + long = bStr + default: + short = bStr + long = aStr + } + // iterate over the strings using an external iterator so we don't lose the value + i := 0 + for i < len(short) && i < len(long) { + if short[i] != long[i] { + // the strings aren't equal so don't return anything + return "" + } + i++ + } + // return whatever's left of the longer string + return long[i:] } func generateTabCompletionSlice(inputWord string) []string { - // create a slice to hold the values - var firstSlice []string - // iterate over all the conversation results - for _, s := range channels { - if s.MembersType == keybase.TEAM { - // its a team so add the topic name as a possible tab completion - firstSlice = append(firstSlice, s.TopicName) - firstSlice = append(firstSlice, s.Name) - } else { - // its a user, so clean the name and append the users name as a possible tab completion - firstSlice = append(firstSlice, cleanChannelName(s.Name)) - } - } - // now return the resultSlice which contains all that are prefixed with inputWord - resultSlice := filterStringSlice(firstSlice, inputWord) - return resultSlice + // create a slice to hold the values + var firstSlice []string + // iterate over all the conversation results + for _, s := range channels { + if s.MembersType == keybase.TEAM { + // its a team so add the topic name as a possible tab completion + firstSlice = append(firstSlice, s.TopicName) + firstSlice = append(firstSlice, s.Name) + } else { + // its a user, so clean the name and append the users name as a possible tab completion + firstSlice = append(firstSlice, cleanChannelName(s.Name)) + } + } + // now return the resultSlice which contains all that are prefixed with inputWord + resultSlice := filterStringSlice(firstSlice, inputWord) + return resultSlice } func handleTab() error { - inputString, err := getInputString("Input") - if err != nil { - return err - } else { - // if you successfully get an input string, grab the last word from the string - ss := strings.Split(inputString, " ") - s := ss[len(ss)-1] - // now in case the word (s) is a mention @something, lets remove it to normalize - if strings.HasPrefix(s, "@") { - s = strings.Replace(s, "@", "", 1) - } - // now call get the list of all possible cantidates that have that as a prefix - resultSlice := generateTabCompletionSlice(s) - lcp := longestCommonPrefix(resultSlice) - if lcp != "" { - remainder := stringRemainder(s, lcp) - writeToView("Input", remainder) - } - } - return nil + inputString, err := getInputString("Input") + if err != nil { + return err + } else { + // if you successfully get an input string, grab the last word from the string + ss := strings.Split(inputString, " ") + s := ss[len(ss)-1] + // now in case the word (s) is a mention @something, lets remove it to normalize + if strings.HasPrefix(s, "@") { + s = strings.Replace(s, "@", "", 1) + } + // now call get the list of all possible cantidates that have that as a prefix + resultSlice := generateTabCompletionSlice(s) + lcp := longestCommonPrefix(resultSlice) + if lcp != "" { + remainder := stringRemainder(s, lcp) + writeToView("Input", remainder) + } + } + return nil } func clearView(viewName string) { @@ -323,17 +323,17 @@ func clearView(viewName string) { } func writeToView(viewName string, message string) { - g.Update(func(g *gocui.Gui) error { - updatingView, err := g.View(viewName) - if err != nil { - return err - } else { - for _, c := range message { - updatingView.EditWrite(c) - } - } - return nil - }) + g.Update(func(g *gocui.Gui) error { + updatingView, err := g.View(viewName) + if err != nil { + return err + } else { + for _, c := range message { + updatingView.EditWrite(c) + } + } + return nil + }) } func printToView(viewName string, message string) { @@ -351,7 +351,7 @@ func printToView(viewName string, message string) { func layout(g *gocui.Gui) error { maxX, maxY := g.Size() if editView, err := g.SetView("Edit", maxX/2-maxX/3+1, maxY/2, maxX-2, maxY/2+10, 0); err != nil { - if !gocui.IsUnknownView(err) { + if !gocui.IsUnknownView(err) { return err } editView.Editable = true @@ -430,12 +430,12 @@ func initKeybindings() error { }); err != nil { return err } - if err := g.SetKeybinding("Input", gocui.KeyTab, gocui.ModNone, - func(g *gocui.Gui, v *gocui.View) error { - return handleTab() - }); err != nil { - return err - } + if err := g.SetKeybinding("Input", gocui.KeyTab, gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + return handleTab() + }); err != nil { + return err + } if err := g.SetKeybinding("Edit", gocui.KeyEnter, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error { popupView("Chat") diff --git a/tcmdShowReactions.go b/tcmdShowReactions.go index 56b1471..328bcea 100644 --- a/tcmdShowReactions.go +++ b/tcmdShowReactions.go @@ -35,7 +35,7 @@ func tcmdShowReactions(m keybase.ChatAPI) { clearView("Chat") go populateChat() } - + } else { clearView("Chat") go populateChat()