Browse Source

gofmt changes

pull/3/head
David Haukeness 5 years ago
parent
commit
c3104b3695
No known key found for this signature in database
GPG Key ID: A7F1091956853EF9
  1. 13
      mage.go
  2. 238
      main.go
  3. 2
      tcmdShowReactions.go

13
mage.go

@ -5,12 +5,12 @@ package main @@ -5,12 +5,12 @@ package main
import (
"encoding/json"
"fmt"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/magefile/mage/sh"
"github.com/magefile/mage/mg"
"strings"
)
// emoji related constants
@ -54,7 +54,7 @@ func createEmojiSlice() ([]string, error) { @@ -54,7 +54,7 @@ func createEmojiSlice() ([]string, error) {
for _, emj := range emojis {
if len(emj.Shortcode) == 0 || strings.Contains(emj.Shortcode, "_tone") {
// dont add them
continue
continue
}
emojiSlice = append(emojiSlice, emj.Shortcode)
}
@ -79,7 +79,7 @@ func BuildEmoji() error { @@ -79,7 +79,7 @@ func BuildEmoji() error {
return err
}
f.Sync()
return nil
return nil
}
// Build kbtui with just the basic commands.
@ -114,7 +114,6 @@ func BuildAllCommandsT() { @@ -114,7 +114,6 @@ func BuildAllCommandsT() {
// Build kbtui with beta functionality
func BuildBeta() {
mg.Deps(BuildEmoji)
mg.Deps(BuildEmoji)
sh.Run("go", "build", "-tags", "allcommands,showreactionscmd,emojiList")
}

238
main.go

@ -203,122 +203,122 @@ func populateList() { @@ -203,122 +203,122 @@ 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 generateChannelTabCompletionSlice(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 generateEmojiTabCompletionSlice(inputWord string) []string {
// use the emojiSlice from emojiList.go and filter it for the input word
resultSlice := filterStringSlice(emojiSlice, inputWord)
return resultSlice
// use the emojiSlice from emojiList.go and filter it for the input word
resultSlice := filterStringSlice(emojiSlice, 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]
// 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 = generateEmojiTabCompletionSlice(s)
} else {
// 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 = generateChannelTabCompletionSlice(s)
}
lcp := longestCommonPrefix(resultSlice)
if lcp != "" {
printToView("Feed", fmt.Sprintf("%d tab completion options", len(resultSlice)))
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]
// 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 = generateEmojiTabCompletionSlice(s)
} else {
// 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 = generateChannelTabCompletionSlice(s)
}
lcp := longestCommonPrefix(resultSlice)
if lcp != "" {
printToView("Feed", fmt.Sprintf("%d tab completion options", len(resultSlice)))
remainder := stringRemainder(s, lcp)
writeToView("Input", remainder)
}
}
return nil
}
func clearView(viewName string) {
@ -337,17 +337,17 @@ func clearView(viewName string) { @@ -337,17 +337,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) {
@ -365,7 +365,7 @@ func printToView(viewName string, message string) { @@ -365,7 +365,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
@ -464,12 +464,12 @@ func initKeybindings() error { @@ -464,12 +464,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")

2
tcmdShowReactions.go

@ -35,7 +35,7 @@ func tcmdShowReactions(m keybase.ChatAPI) { @@ -35,7 +35,7 @@ func tcmdShowReactions(m keybase.ChatAPI) {
clearView("Chat")
go populateChat()
}
} else {
clearView("Chat")
go populateChat()

Loading…
Cancel
Save