Add new feature that allows jumping to items in a treeview
MODIFY
internal/config/config.go
internal/config/config.go
@@ -21,6 +21,17 @@ const (
HourAndMinute = 1
//NoTime means that not time at all will be displayed.
NoTime = 2
+
+ // DoNothingOnTypeInList means that when typing in a list (treeview) simply
+ // nothing will happen.
+ DoNothingOnTypeInList = 0
+ // SearchOnTypeInList will cause the widget to search through it's
+ // children, trying to find anything that is prefixed with the
+ // previously entered characters.
+ SearchOnTypeInList = 1
+ // FocusMessageInputOnTypeInList will automatically focus the message input
+ // component and transfer the typed character into it as well.
+ FocusMessageInputOnTypeInList = 2
)
var (
@@ -34,6 +45,7 @@ var (
FixedSizeRight: 12,
FocusChannelAfterGuildSelection: true,
FocusMessageInputAfterChannelSelection: true,
+ OnTypeInListBehaviour: SearchOnTypeInList,
}
)
@@ -68,6 +80,10 @@ type Config struct {
FixedSizeLeft int
//FixedSizeRight defines the size of the users container on the right.
FixedSizeRight int
+
+ // OnTypeInListBehaviour defines wether the application focus the input
+ // input field on typing, searches the list or does nothing.
+ OnTypeInListBehaviour int
}
var cachedConfigDir string

MODIFY
internal/ui/window.go
internal/ui/window.go
@@ -28,6 +28,8 @@ var (
mentionRegex = regexp.MustCompile("@.*?(?:$|\\s)")
)
+// Window is basically the whole application, as it contains all the
+// components and the necccessary global state.
type Window struct {
app *tview.Application
rootContainer *tview.Flex
@@ -108,6 +110,51 @@ func NewWindow(discord *discordgo.Session) (*Window, error) {
guildList.SetBorder(true)
guildList.SetTopLevel(1)
+ createInputHandler := func(treeView *tview.TreeView, rootNode *tview.TreeNode, jumpTime *time.Time, jumpBuffer *string) func(event *tcell.EventKey) *tcell.EventKey {
+ var traversalFunction func(node *tview.TreeNode) bool
+ traversalFunction = func(node *tview.TreeNode) bool {
+ if len(node.GetChildren()) > 0 {
+ for _, subNode := range node.GetChildren() {
+ returnValue := traversalFunction(subNode)
+ if returnValue {
+ return true
+ }
+ }
+ } else if node.IsSelectable() {
+ if strings.HasPrefix(strings.ToLower(node.GetText()), *jumpBuffer) {
+ treeView.SetCurrentNode(node)
+ return true
+ }
+ }
+
+ return false
+ }
+
+ eventHandler := func(event *tcell.EventKey) *tcell.EventKey {
+ if time.Since(*jumpTime) > (500 * time.Millisecond) {
+ *jumpBuffer = ""
+ }
+
+ *jumpTime = time.Now()
+
+ if event.Rune() != 0 {
+ *jumpBuffer += string(event.Rune())
+ *jumpBuffer = strings.ToLower(*jumpBuffer)
+
+ for _, node := range rootNode.GetChildren() {
+ stopIterating := traversalFunction(node)
+ if stopIterating {
+ return nil
+ }
+ }
+ }
+
+ return event
+ }
+
+ return eventHandler
+ }
+
var selectedGuildNode *tview.TreeNode
for _, tempGuild := range guilds {
@@ -631,6 +678,21 @@ func NewWindow(discord *discordgo.Session) (*Window, error) {
SetCycleSelection(true)
window.userContainer.SetBorder(true)
+ if config.GetConfig().OnTypeInListBehaviour == config.SearchOnTypeInList {
+ var guildJumpBuffer string
+ var guildJumpTime time.Time
+ guildList.SetInputCapture(createInputHandler(guildList, guildRootNode, &guildJumpTime, &guildJumpBuffer))
+ var channelJumpBuffer string
+ var channelJumpTime time.Time
+ channelTree.SetInputCapture(createInputHandler(channelTree, channelRootNode, &channelJumpTime, &channelJumpBuffer))
+ var userJumpBuffer string
+ var userJumpTime time.Time
+ window.userContainer.SetInputCapture(createInputHandler(window.userContainer, window.userRootNode, &userJumpTime, &userJumpBuffer))
+ var privateJumpBuffer string
+ var privateJumpTime time.Time
+ window.friendsList.SetInputCapture(createInputHandler(window.friendsList, friendsNode, &privateJumpTime, &privateJumpBuffer))
+ }
+
window.rootContainer = tview.NewFlex().
SetDirection(tview.FlexColumn)
window.rootContainer.SetTitleAlign(tview.AlignCenter)

Add new feature that allows jumping to items in a treeview