mirror of
https://github.com/Adembc/lazyssh.git
synced 2026-07-14 12:13:34 +02:00
improve ui architecture
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Adembc/lazyssh/internal/core/domain"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
func (t *tui) handleGlobalKeys(event *tcell.EventKey) *tcell.EventKey {
|
||||
// =============================================================================
|
||||
// Event Handlers (handle user input/events)
|
||||
// =============================================================================
|
||||
|
||||
func (t *tui) handleGlobalKeys(event *tcell.EventKey) *tcell.EventKey {
|
||||
// Don't handle global keys when search has focus
|
||||
if t.app.GetFocus() == t.searchBar {
|
||||
return event
|
||||
@@ -17,31 +22,31 @@ func (t *tui) handleGlobalKeys(event *tcell.EventKey) *tcell.EventKey {
|
||||
t.app.Stop()
|
||||
return nil
|
||||
case '/':
|
||||
t.showSearchBar()
|
||||
t.handleSearchToggle()
|
||||
return nil
|
||||
case 'a':
|
||||
t.showAddServerForm()
|
||||
t.handleServerAdd()
|
||||
return nil
|
||||
case 'e':
|
||||
t.showEditServerForm()
|
||||
t.handleServerEdit()
|
||||
return nil
|
||||
case 'd':
|
||||
t.deleteSelectedServer()
|
||||
t.handleServerDelete()
|
||||
return nil
|
||||
case '?':
|
||||
t.showHelpModal()
|
||||
t.handleHelpShow()
|
||||
return nil
|
||||
}
|
||||
|
||||
if event.Key() == tcell.KeyEnter {
|
||||
t.connectToSelectedServer()
|
||||
t.handleServerConnect()
|
||||
return nil
|
||||
}
|
||||
|
||||
return event
|
||||
}
|
||||
|
||||
func (t *tui) handleSearch(query string) {
|
||||
func (t *tui) handleSearchInput(query string) {
|
||||
filtered, _ := t.serverService.ListServers(query)
|
||||
t.serverList.UpdateServers(filtered)
|
||||
if len(filtered) == 0 {
|
||||
@@ -49,42 +54,32 @@ func (t *tui) handleSearch(query string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *tui) handleServerConnect(server domain.Server) {
|
||||
showConnectModal(t.app, t.root, server)
|
||||
func (t *tui) handleSearchToggle() {
|
||||
t.showSearchBar()
|
||||
}
|
||||
|
||||
func (t *tui) handleServerConnect() {
|
||||
if server, ok := t.serverList.GetSelectedServer(); ok {
|
||||
t.showConnectModal(server)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *tui) handleServerSelectionChange(server domain.Server) {
|
||||
t.details.UpdateServer(server)
|
||||
}
|
||||
|
||||
func (t *tui) showSearchBar() {
|
||||
t.left.Clear()
|
||||
t.left.AddItem(t.searchBar, 3, 0, true)
|
||||
t.left.AddItem(t.serverList, 0, 1, false)
|
||||
t.app.SetFocus(t.searchBar)
|
||||
t.searchVisible = true
|
||||
}
|
||||
|
||||
func (t *tui) hideSearchBar() {
|
||||
t.left.Clear()
|
||||
t.left.AddItem(t.hintBar, 1, 0, false)
|
||||
t.left.AddItem(t.serverList, 0, 1, true)
|
||||
t.app.SetFocus(t.serverList)
|
||||
t.searchVisible = false
|
||||
}
|
||||
|
||||
func (t *tui) showAddServerForm() {
|
||||
func (t *tui) handleServerAdd() {
|
||||
form := NewServerForm(ServerFormAdd, nil).
|
||||
OnSave(t.handleServerSave).
|
||||
OnCancel(t.returnToMain)
|
||||
OnCancel(t.handleFormCancel)
|
||||
t.app.SetRoot(form, true)
|
||||
}
|
||||
|
||||
func (t *tui) showEditServerForm() {
|
||||
func (t *tui) handleServerEdit() {
|
||||
if server, ok := t.serverList.GetSelectedServer(); ok {
|
||||
form := NewServerForm(ServerFormEdit, &server).
|
||||
OnSave(t.handleServerSave).
|
||||
OnCancel(t.returnToMain)
|
||||
OnCancel(t.handleFormCancel)
|
||||
t.app.SetRoot(form, true)
|
||||
}
|
||||
}
|
||||
@@ -99,16 +94,91 @@ func (t *tui) handleServerSave(server domain.Server, original *domain.Server) {
|
||||
}
|
||||
|
||||
t.refreshServerList()
|
||||
t.returnToMain()
|
||||
t.handleFormCancel()
|
||||
}
|
||||
|
||||
func (t *tui) deleteSelectedServer() {
|
||||
func (t *tui) handleServerDelete() {
|
||||
if server, ok := t.serverList.GetSelectedServer(); ok {
|
||||
_ = t.serverService.DeleteServer(server)
|
||||
t.refreshServerList()
|
||||
}
|
||||
}
|
||||
|
||||
func (t *tui) handleFormCancel() {
|
||||
t.returnToMain()
|
||||
}
|
||||
|
||||
func (t *tui) handleHelpShow() {
|
||||
t.showHelpModal()
|
||||
}
|
||||
|
||||
func (t *tui) handleModalClose() {
|
||||
t.returnToMain()
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// UI Display Functions (show UI elements/modals)
|
||||
// =============================================================================
|
||||
|
||||
func (t *tui) showSearchBar() {
|
||||
t.left.Clear()
|
||||
t.left.AddItem(t.searchBar, 3, 0, true)
|
||||
t.left.AddItem(t.serverList, 0, 1, false)
|
||||
t.app.SetFocus(t.searchBar)
|
||||
t.searchVisible = true
|
||||
}
|
||||
|
||||
func (t *tui) showConnectModal(server domain.Server) {
|
||||
msg := fmt.Sprintf("SSH to %s (%s@%s:%d)\n\nThis is a mock action.",
|
||||
server.Alias, server.User, server.Host, server.Port)
|
||||
|
||||
modal := tview.NewModal().
|
||||
SetText(msg).
|
||||
AddButtons([]string{"OK"}).
|
||||
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
|
||||
t.handleModalClose()
|
||||
})
|
||||
|
||||
t.app.SetRoot(modal, true)
|
||||
}
|
||||
|
||||
func (t *tui) showHelpModal() {
|
||||
text := "Keyboard shortcuts:\n\n" +
|
||||
" ↑/↓ Navigate\n" +
|
||||
" Enter SSH connect (mock)\n" +
|
||||
" a Add server (mock)\n" +
|
||||
" e Edit server (mock)\n" +
|
||||
" d Delete entry (mock)\n" +
|
||||
" / Focus search\n" +
|
||||
" q Quit\n" +
|
||||
" ? Help\n"
|
||||
|
||||
modal := tview.NewModal().
|
||||
SetText(text).
|
||||
AddButtons([]string{"Close"}).
|
||||
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
|
||||
t.handleModalClose()
|
||||
})
|
||||
|
||||
t.app.SetRoot(modal, true)
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// UI State Management (hide UI elements)
|
||||
// =============================================================================
|
||||
|
||||
func (t *tui) hideSearchBar() {
|
||||
t.left.Clear()
|
||||
t.left.AddItem(t.hintBar, 1, 0, false)
|
||||
t.left.AddItem(t.serverList, 0, 1, true)
|
||||
t.app.SetFocus(t.serverList)
|
||||
t.searchVisible = false
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Internal Operations (perform actual work)
|
||||
// =============================================================================
|
||||
|
||||
func (t *tui) refreshServerList() {
|
||||
query := ""
|
||||
if t.searchVisible {
|
||||
@@ -118,16 +188,6 @@ func (t *tui) refreshServerList() {
|
||||
t.serverList.UpdateServers(filtered)
|
||||
}
|
||||
|
||||
func (t *tui) connectToSelectedServer() {
|
||||
if server, ok := t.serverList.GetSelectedServer(); ok {
|
||||
t.handleServerConnect(server)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *tui) returnToMain() {
|
||||
t.app.SetRoot(t.root, true)
|
||||
}
|
||||
|
||||
func (t *tui) showHelpModal() {
|
||||
showHelpModal(t.app, t.root)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user