mirror of
https://github.com/Adembc/lazyssh.git
synced 2026-07-14 12:13:34 +02:00
improve(ui): enhance server search experience (#71)
This commit is contained in:
@@ -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() {
|
func (t *tui) handleServerConnect() {
|
||||||
if server, ok := t.serverList.GetSelectedServer(); ok {
|
if server, ok := t.serverList.GetSelectedServer(); ok {
|
||||||
|
|
||||||
|
|||||||
@@ -21,8 +21,9 @@ import (
|
|||||||
|
|
||||||
type SearchBar struct {
|
type SearchBar struct {
|
||||||
*tview.InputField
|
*tview.InputField
|
||||||
onSearch func(string)
|
onSearch func(string)
|
||||||
onEscape func()
|
onEscape func()
|
||||||
|
onNavigate func(direction int) // -1 for up, 1 for down
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSearchBar() *SearchBar {
|
func NewSearchBar() *SearchBar {
|
||||||
@@ -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 {
|
func (s *SearchBar) OnSearch(fn func(string)) *SearchBar {
|
||||||
@@ -68,3 +87,8 @@ func (s *SearchBar) OnEscape(fn func()) *SearchBar {
|
|||||||
s.onEscape = fn
|
s.onEscape = fn
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SearchBar) OnNavigate(fn func(direction int)) *SearchBar {
|
||||||
|
s.onNavigate = fn
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ type ServerList struct {
|
|||||||
servers []domain.Server
|
servers []domain.Server
|
||||||
onSelection func(domain.Server)
|
onSelection func(domain.Server)
|
||||||
onSelectionChange func(domain.Server)
|
onSelectionChange func(domain.Server)
|
||||||
|
onReturnToSearch func()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerList() *ServerList {
|
func NewServerList() *ServerList {
|
||||||
@@ -52,6 +53,18 @@ func (sl *ServerList) build() {
|
|||||||
sl.onSelectionChange(sl.servers[index])
|
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) {
|
func (sl *ServerList) UpdateServers(servers []domain.Server) {
|
||||||
@@ -93,3 +106,8 @@ func (sl *ServerList) OnSelectionChange(fn func(server domain.Server)) *ServerLi
|
|||||||
sl.onSelectionChange = fn
|
sl.onSelectionChange = fn
|
||||||
return sl
|
return sl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sl *ServerList) OnReturnToSearch(fn func()) *ServerList {
|
||||||
|
sl.onReturnToSearch = fn
|
||||||
|
return sl
|
||||||
|
}
|
||||||
|
|||||||
@@ -91,11 +91,13 @@ func (t *tui) buildComponents() *tui {
|
|||||||
t.header = NewAppHeader(t.version, t.commit, RepoURL)
|
t.header = NewAppHeader(t.version, t.commit, RepoURL)
|
||||||
t.searchBar = NewSearchBar().
|
t.searchBar = NewSearchBar().
|
||||||
OnSearch(t.handleSearchInput).
|
OnSearch(t.handleSearchInput).
|
||||||
OnEscape(t.blurSearchBar)
|
OnEscape(t.blurSearchBar).
|
||||||
|
OnNavigate(t.handleSearchNavigate)
|
||||||
IsForwarding = t.serverService.IsForwarding
|
IsForwarding = t.serverService.IsForwarding
|
||||||
|
|
||||||
t.serverList = NewServerList().
|
t.serverList = NewServerList().
|
||||||
OnSelectionChange(t.handleServerSelectionChange)
|
OnSelectionChange(t.handleServerSelectionChange).
|
||||||
|
OnReturnToSearch(t.handleReturnToSearch)
|
||||||
t.details = NewServerDetails()
|
t.details = NewServerDetails()
|
||||||
t.statusBar = NewStatusBar()
|
t.statusBar = NewStatusBar()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user