1
0
mirror of https://github.com/Rudi9719/kbtui.git synced 2026-03-22 09:57:24 +00:00

updated to use cached conversation list

working autocomplete on input line
still only a partial list of team/channel pairs, need to see what i'm missing
This commit is contained in:
2019-10-10 09:48:03 -06:00
parent 9c92a64bf4
commit 316fbfa9aa

79
main.go
View File

@ -241,28 +241,46 @@ func longestCommonPrefix(ss []string) string {
return min return min
} }
func generateTabCompletionSlice(inputWord string) []string { func stringRemainder(aStr, bStr string) string {
// gets all possible tab completion cantidates var long, short string
if testVar, err := k.ChatList(); err != nil { //figure out which string is longer
log.Printf("%+v", err) switch {
} else { case len(aStr) < len (bStr):
// create a slice to hold the values short = aStr
var firstSlice []string long = bStr
// iterate over all the conversation results default:
for _, s := range testVar.Result.Conversations { short = bStr
if s.Channel.MembersType == keybase.TEAM { long = aStr
// its a team so add the topic name as a possible tab completion
firstSlice = append(firstSlice, s.Channel.TopicName)
} else {
// its a user, so clean the name and append the users name as a possible tab completion
firstSlice = append(firstSlice, cleanChannelName(s.Channel.Name))
}
}
// now return the resultSlice which contains all that are prefixed with inputWord
resultSlice := filterStringSlice(firstSlice, inputWord)
return resultSlice
} }
return nil // 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)
} 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 { func handleTab() error {
@ -275,8 +293,9 @@ func handleTab() error {
s := ss[len(ss)-1] s := ss[len(ss)-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)
result := longestCommonPrefix(resultSlice) lcp := longestCommonPrefix(resultSlice)
printToView("Feed", fmt.Sprintf("TabCompletion: %s", result)) remainder := stringRemainder(s, lcp)
writeToView("Input", remainder)
} }
return nil return nil
} }
@ -296,6 +315,20 @@ 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
})
}
func printToView(viewName string, message string) { func printToView(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)