mirror of
https://github.com/Rudi9719/kbtui.git
synced 2026-03-22 05:17:27 +00:00
Go fmt
This commit is contained in:
216
main.go
216
main.go
@ -203,108 +203,108 @@ func populateList() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func filterStringSlice(ss []string, fv string) []string {
|
func filterStringSlice(ss []string, fv string) []string {
|
||||||
var rs []string
|
var rs []string
|
||||||
for _, s := range ss {
|
for _, s := range ss {
|
||||||
if strings.HasPrefix(s, fv) {
|
if strings.HasPrefix(s, fv) {
|
||||||
rs = append(rs, s)
|
rs = append(rs, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rs
|
return rs
|
||||||
}
|
}
|
||||||
|
|
||||||
func longestCommonPrefix(ss []string) string {
|
func longestCommonPrefix(ss []string) string {
|
||||||
// cover the case where the slice has no or one members
|
// cover the case where the slice has no or one members
|
||||||
switch len(ss) {
|
switch len(ss) {
|
||||||
case 0:
|
case 0:
|
||||||
return ""
|
return ""
|
||||||
case 1:
|
case 1:
|
||||||
return ss[0]
|
return ss[0]
|
||||||
}
|
}
|
||||||
// all strings are compared by bytes here forward (TBD unicode normalization?)
|
// all strings are compared by bytes here forward (TBD unicode normalization?)
|
||||||
// establish min, max lenth members of the slice by iterating over the members
|
// establish min, max lenth members of the slice by iterating over the members
|
||||||
min, max := ss[0], ss[0]
|
min, max := ss[0], ss[0]
|
||||||
for _, s := range ss[1:] {
|
for _, s := range ss[1:] {
|
||||||
switch {
|
switch {
|
||||||
case s < min:
|
case s < min:
|
||||||
min = s
|
min = s
|
||||||
case s > max:
|
case s > max:
|
||||||
max = s
|
max = s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// then iterate over the characters from min to max, as soon as chars don't match return
|
// 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++ {
|
for i := 0; i < len(min) && i < len(max); i++ {
|
||||||
if min[i] != max[i] {
|
if min[i] != max[i] {
|
||||||
return min[:i]
|
return min[:i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// to cover the case where all members are equal, just return one
|
// to cover the case where all members are equal, just return one
|
||||||
return min
|
return min
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringRemainder(aStr, bStr string) string {
|
func stringRemainder(aStr, bStr string) string {
|
||||||
var long, short string
|
var long, short string
|
||||||
//figure out which string is longer
|
//figure out which string is longer
|
||||||
switch {
|
switch {
|
||||||
case len(aStr) < len (bStr):
|
case len(aStr) < len(bStr):
|
||||||
short = aStr
|
short = aStr
|
||||||
long = bStr
|
long = bStr
|
||||||
default:
|
default:
|
||||||
short = bStr
|
short = bStr
|
||||||
long = aStr
|
long = aStr
|
||||||
}
|
}
|
||||||
// iterate over the strings using an external iterator so we don't lose the value
|
// iterate over the strings using an external iterator so we don't lose the value
|
||||||
i := 0
|
i := 0
|
||||||
for i < len(short) && i < len(long) {
|
for i < len(short) && i < len(long) {
|
||||||
if short[i] != long[i] {
|
if short[i] != long[i] {
|
||||||
// the strings aren't equal so don't return anything
|
// the strings aren't equal so don't return anything
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
// return whatever's left of the longer string
|
// return whatever's left of the longer string
|
||||||
return long[i:]
|
return long[i:]
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateTabCompletionSlice(inputWord string) []string {
|
func generateTabCompletionSlice(inputWord string) []string {
|
||||||
// create a slice to hold the values
|
// create a slice to hold the values
|
||||||
var firstSlice []string
|
var firstSlice []string
|
||||||
// iterate over all the conversation results
|
// iterate over all the conversation results
|
||||||
for _, s := range channels {
|
for _, s := range channels {
|
||||||
if s.MembersType == keybase.TEAM {
|
if s.MembersType == keybase.TEAM {
|
||||||
// its a team so add the topic name as a possible tab completion
|
// its a team so add the topic name as a possible tab completion
|
||||||
firstSlice = append(firstSlice, s.TopicName)
|
firstSlice = append(firstSlice, s.TopicName)
|
||||||
firstSlice = append(firstSlice, s.Name)
|
firstSlice = append(firstSlice, s.Name)
|
||||||
} else {
|
} else {
|
||||||
// its a user, so clean the name and append the users name as a possible tab completion
|
// its a user, so clean the name and append the users name as a possible tab completion
|
||||||
firstSlice = append(firstSlice, cleanChannelName(s.Name))
|
firstSlice = append(firstSlice, cleanChannelName(s.Name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// now return the resultSlice which contains all that are prefixed with inputWord
|
// now return the resultSlice which contains all that are prefixed with inputWord
|
||||||
resultSlice := filterStringSlice(firstSlice, inputWord)
|
resultSlice := filterStringSlice(firstSlice, inputWord)
|
||||||
return resultSlice
|
return resultSlice
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleTab() error {
|
func handleTab() error {
|
||||||
inputString, err := getInputString("Input")
|
inputString, err := getInputString("Input")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
// if you successfully get an input string, grab the last word from the string
|
// if you successfully get an input string, grab the last word from the string
|
||||||
ss := strings.Split(inputString, " ")
|
ss := strings.Split(inputString, " ")
|
||||||
s := ss[len(ss)-1]
|
s := ss[len(ss)-1]
|
||||||
// now in case the word (s) is a mention @something, lets remove it to normalize
|
// now in case the word (s) is a mention @something, lets remove it to normalize
|
||||||
if strings.HasPrefix(s, "@") {
|
if strings.HasPrefix(s, "@") {
|
||||||
s = strings.Replace(s, "@", "", 1)
|
s = strings.Replace(s, "@", "", 1)
|
||||||
}
|
}
|
||||||
// now call get the list of all possible cantidates that have that as a prefix
|
// now call get the list of all possible cantidates that have that as a prefix
|
||||||
resultSlice := generateTabCompletionSlice(s)
|
resultSlice := generateTabCompletionSlice(s)
|
||||||
lcp := longestCommonPrefix(resultSlice)
|
lcp := longestCommonPrefix(resultSlice)
|
||||||
if lcp != "" {
|
if lcp != "" {
|
||||||
remainder := stringRemainder(s, lcp)
|
remainder := stringRemainder(s, lcp)
|
||||||
writeToView("Input", remainder)
|
writeToView("Input", remainder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func clearView(viewName string) {
|
func clearView(viewName string) {
|
||||||
@ -323,17 +323,17 @@ func clearView(viewName string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func writeToView(viewName string, message string) {
|
func writeToView(viewName string, message string) {
|
||||||
g.Update(func(g *gocui.Gui) error {
|
g.Update(func(g *gocui.Gui) error {
|
||||||
updatingView, err := g.View(viewName)
|
updatingView, err := g.View(viewName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
for _, c := range message {
|
for _, c := range message {
|
||||||
updatingView.EditWrite(c)
|
updatingView.EditWrite(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func printToView(viewName string, message string) {
|
func printToView(viewName string, message string) {
|
||||||
@ -351,7 +351,7 @@ func printToView(viewName string, message string) {
|
|||||||
func layout(g *gocui.Gui) error {
|
func layout(g *gocui.Gui) error {
|
||||||
maxX, maxY := g.Size()
|
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 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
|
return err
|
||||||
}
|
}
|
||||||
editView.Editable = true
|
editView.Editable = true
|
||||||
@ -430,12 +430,12 @@ func initKeybindings() error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := g.SetKeybinding("Input", gocui.KeyTab, gocui.ModNone,
|
if err := g.SetKeybinding("Input", gocui.KeyTab, gocui.ModNone,
|
||||||
func(g *gocui.Gui, v *gocui.View) error {
|
func(g *gocui.Gui, v *gocui.View) error {
|
||||||
return handleTab()
|
return handleTab()
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := g.SetKeybinding("Edit", gocui.KeyEnter, gocui.ModNone,
|
if err := g.SetKeybinding("Edit", gocui.KeyEnter, gocui.ModNone,
|
||||||
func(g *gocui.Gui, v *gocui.View) error {
|
func(g *gocui.Gui, v *gocui.View) error {
|
||||||
popupView("Chat")
|
popupView("Chat")
|
||||||
|
|||||||
@ -35,7 +35,7 @@ func tcmdShowReactions(m keybase.ChatAPI) {
|
|||||||
clearView("Chat")
|
clearView("Chat")
|
||||||
go populateChat()
|
go populateChat()
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
clearView("Chat")
|
clearView("Chat")
|
||||||
go populateChat()
|
go populateChat()
|
||||||
|
|||||||
Reference in New Issue
Block a user