Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions internal/adapters/ui/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,43 @@ func (t *tui) handleSearchFocus() {
}
}

func (t *tui) handleSearchNavigate(direction int) {
if t.serverList != nil {
t.app.SetFocus(t.serverList)

currentIdx := t.serverList.GetCurrentItem()
itemCount := t.serverList.GetItemCount()

if itemCount == 0 {
return
}

if direction > 0 {
if currentIdx < itemCount-1 {
t.serverList.SetCurrentItem(currentIdx + 1)
} else {
t.serverList.SetCurrentItem(0)
}
} else {
if currentIdx > 0 {
t.serverList.SetCurrentItem(currentIdx - 1)
} else {
t.serverList.SetCurrentItem(itemCount - 1)
}
}

if server, ok := t.serverList.GetSelectedServer(); ok {
t.details.UpdateServer(server)
}
}
}

func (t *tui) handleReturnToSearch() {
if t.searchBar != nil {
t.app.SetFocus(t.searchBar)
}
}

func (t *tui) handleServerConnect() {
if server, ok := t.serverList.GetSelectedServer(); ok {

Expand Down
28 changes: 26 additions & 2 deletions internal/adapters/ui/search_bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (

type SearchBar struct {
*tview.InputField
onSearch func(string)
onEscape func()
onSearch func(string)
onEscape func()
onNavigate func(direction int) // -1 for up, 1 for down
}

func NewSearchBar() *SearchBar {
Expand Down Expand Up @@ -57,6 +58,24 @@ func (s *SearchBar) build() {
}
}
})

s.InputField.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
//nolint:exhaustive // We only handle arrow keys and pass through others
switch event.Key() {
case tcell.KeyDown:
if s.onNavigate != nil {
s.onNavigate(1)
}
return nil
case tcell.KeyUp:
if s.onNavigate != nil {
s.onNavigate(-1)
}
return nil
default:
return event
}
})
}

func (s *SearchBar) OnSearch(fn func(string)) *SearchBar {
Expand All @@ -68,3 +87,8 @@ func (s *SearchBar) OnEscape(fn func()) *SearchBar {
s.onEscape = fn
return s
}

func (s *SearchBar) OnNavigate(fn func(direction int)) *SearchBar {
s.onNavigate = fn
return s
}
18 changes: 18 additions & 0 deletions internal/adapters/ui/server_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ServerList struct {
servers []domain.Server
onSelection func(domain.Server)
onSelectionChange func(domain.Server)
onReturnToSearch func()
}

func NewServerList() *ServerList {
Expand Down Expand Up @@ -52,6 +53,18 @@ func (sl *ServerList) build() {
sl.onSelectionChange(sl.servers[index])
}
})

sl.List.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
//nolint:exhaustive // We only handle specific keys and pass through others
switch event.Key() {
case tcell.KeyLeft, tcell.KeyRight, tcell.KeyBackspace, tcell.KeyBackspace2, tcell.KeyESC:
if sl.onReturnToSearch != nil {
sl.onReturnToSearch()
}
return nil
}
return event
})
}

func (sl *ServerList) UpdateServers(servers []domain.Server) {
Expand Down Expand Up @@ -93,3 +106,8 @@ func (sl *ServerList) OnSelectionChange(fn func(server domain.Server)) *ServerLi
sl.onSelectionChange = fn
return sl
}

func (sl *ServerList) OnReturnToSearch(fn func()) *ServerList {
sl.onReturnToSearch = fn
return sl
}
6 changes: 4 additions & 2 deletions internal/adapters/ui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ func (t *tui) buildComponents() *tui {
t.header = NewAppHeader(t.version, t.commit, RepoURL)
t.searchBar = NewSearchBar().
OnSearch(t.handleSearchInput).
OnEscape(t.blurSearchBar)
OnEscape(t.blurSearchBar).
OnNavigate(t.handleSearchNavigate)
IsForwarding = t.serverService.IsForwarding

t.serverList = NewServerList().
OnSelectionChange(t.handleServerSelectionChange)
OnSelectionChange(t.handleServerSelectionChange).
OnReturnToSearch(t.handleReturnToSearch)
t.details = NewServerDetails()
t.statusBar = NewStatusBar()

Expand Down