Skip to content

Commit 07cb2ab

Browse files
authored
improve(ui): enhance server search experience (#71)
1 parent 460bb0f commit 07cb2ab

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

internal/adapters/ui/handlers.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,43 @@ func (t *tui) handleSearchFocus() {
183183
}
184184
}
185185

186+
func (t *tui) handleSearchNavigate(direction int) {
187+
if t.serverList != nil {
188+
t.app.SetFocus(t.serverList)
189+
190+
currentIdx := t.serverList.GetCurrentItem()
191+
itemCount := t.serverList.GetItemCount()
192+
193+
if itemCount == 0 {
194+
return
195+
}
196+
197+
if direction > 0 {
198+
if currentIdx < itemCount-1 {
199+
t.serverList.SetCurrentItem(currentIdx + 1)
200+
} else {
201+
t.serverList.SetCurrentItem(0)
202+
}
203+
} else {
204+
if currentIdx > 0 {
205+
t.serverList.SetCurrentItem(currentIdx - 1)
206+
} else {
207+
t.serverList.SetCurrentItem(itemCount - 1)
208+
}
209+
}
210+
211+
if server, ok := t.serverList.GetSelectedServer(); ok {
212+
t.details.UpdateServer(server)
213+
}
214+
}
215+
}
216+
217+
func (t *tui) handleReturnToSearch() {
218+
if t.searchBar != nil {
219+
t.app.SetFocus(t.searchBar)
220+
}
221+
}
222+
186223
func (t *tui) handleServerConnect() {
187224
if server, ok := t.serverList.GetSelectedServer(); ok {
188225

internal/adapters/ui/search_bar.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import (
2121

2222
type SearchBar struct {
2323
*tview.InputField
24-
onSearch func(string)
25-
onEscape func()
24+
onSearch func(string)
25+
onEscape func()
26+
onNavigate func(direction int) // -1 for up, 1 for down
2627
}
2728

2829
func NewSearchBar() *SearchBar {
@@ -57,6 +58,24 @@ func (s *SearchBar) build() {
5758
}
5859
}
5960
})
61+
62+
s.InputField.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
63+
//nolint:exhaustive // We only handle arrow keys and pass through others
64+
switch event.Key() {
65+
case tcell.KeyDown:
66+
if s.onNavigate != nil {
67+
s.onNavigate(1)
68+
}
69+
return nil
70+
case tcell.KeyUp:
71+
if s.onNavigate != nil {
72+
s.onNavigate(-1)
73+
}
74+
return nil
75+
default:
76+
return event
77+
}
78+
})
6079
}
6180

6281
func (s *SearchBar) OnSearch(fn func(string)) *SearchBar {
@@ -68,3 +87,8 @@ func (s *SearchBar) OnEscape(fn func()) *SearchBar {
6887
s.onEscape = fn
6988
return s
7089
}
90+
91+
func (s *SearchBar) OnNavigate(fn func(direction int)) *SearchBar {
92+
s.onNavigate = fn
93+
return s
94+
}

internal/adapters/ui/server_list.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type ServerList struct {
2525
servers []domain.Server
2626
onSelection func(domain.Server)
2727
onSelectionChange func(domain.Server)
28+
onReturnToSearch func()
2829
}
2930

3031
func NewServerList() *ServerList {
@@ -52,6 +53,18 @@ func (sl *ServerList) build() {
5253
sl.onSelectionChange(sl.servers[index])
5354
}
5455
})
56+
57+
sl.List.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
58+
//nolint:exhaustive // We only handle specific keys and pass through others
59+
switch event.Key() {
60+
case tcell.KeyLeft, tcell.KeyRight, tcell.KeyBackspace, tcell.KeyBackspace2, tcell.KeyESC:
61+
if sl.onReturnToSearch != nil {
62+
sl.onReturnToSearch()
63+
}
64+
return nil
65+
}
66+
return event
67+
})
5568
}
5669

5770
func (sl *ServerList) UpdateServers(servers []domain.Server) {
@@ -93,3 +106,8 @@ func (sl *ServerList) OnSelectionChange(fn func(server domain.Server)) *ServerLi
93106
sl.onSelectionChange = fn
94107
return sl
95108
}
109+
110+
func (sl *ServerList) OnReturnToSearch(fn func()) *ServerList {
111+
sl.onReturnToSearch = fn
112+
return sl
113+
}

internal/adapters/ui/tui.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,13 @@ func (t *tui) buildComponents() *tui {
9191
t.header = NewAppHeader(t.version, t.commit, RepoURL)
9292
t.searchBar = NewSearchBar().
9393
OnSearch(t.handleSearchInput).
94-
OnEscape(t.blurSearchBar)
94+
OnEscape(t.blurSearchBar).
95+
OnNavigate(t.handleSearchNavigate)
9596
IsForwarding = t.serverService.IsForwarding
9697

9798
t.serverList = NewServerList().
98-
OnSelectionChange(t.handleServerSelectionChange)
99+
OnSelectionChange(t.handleServerSelectionChange).
100+
OnReturnToSearch(t.handleReturnToSearch)
99101
t.details = NewServerDetails()
100102
t.statusBar = NewStatusBar()
101103

0 commit comments

Comments
 (0)