Files
lazyssh/internal/adapters/ui/handlers.go
T

209 lines
5.1 KiB
Go
Raw Normal View History

2025-08-17 17:48:24 +01:00
// Copyright 2025.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2025-08-17 11:09:20 +01:00
package ui
import (
2025-08-17 14:02:53 +01:00
"fmt"
2025-08-17 15:36:32 +01:00
2025-08-17 11:09:20 +01:00
"github.com/Adembc/lazyssh/internal/core/domain"
2025-08-17 17:48:24 +01:00
tcell "github.com/gdamore/tcell/v2" // Explicit alias
2025-08-17 14:02:53 +01:00
"github.com/rivo/tview"
2025-08-17 11:09:20 +01:00
)
2025-08-17 14:02:53 +01:00
// =============================================================================
// Event Handlers (handle user input/events)
// =============================================================================
2025-08-17 11:09:20 +01:00
2025-08-17 14:02:53 +01:00
func (t *tui) handleGlobalKeys(event *tcell.EventKey) *tcell.EventKey {
2025-08-17 11:09:20 +01:00
// Don't handle global keys when search has focus
if t.app.GetFocus() == t.searchBar {
return event
}
switch event.Rune() {
case 'q':
t.app.Stop()
return nil
case '/':
2025-08-17 14:02:53 +01:00
t.handleSearchToggle()
2025-08-17 11:09:20 +01:00
return nil
case 'a':
2025-08-17 14:02:53 +01:00
t.handleServerAdd()
2025-08-17 11:09:20 +01:00
return nil
case 'e':
2025-08-17 14:02:53 +01:00
t.handleServerEdit()
2025-08-17 11:09:20 +01:00
return nil
case 'd':
2025-08-17 14:02:53 +01:00
t.handleServerDelete()
2025-08-17 11:09:20 +01:00
return nil
case '?':
2025-08-17 14:02:53 +01:00
t.handleHelpShow()
2025-08-17 11:09:20 +01:00
return nil
}
if event.Key() == tcell.KeyEnter {
2025-08-17 14:02:53 +01:00
t.handleServerConnect()
2025-08-17 11:09:20 +01:00
return nil
}
return event
}
2025-08-17 14:02:53 +01:00
func (t *tui) handleSearchInput(query string) {
2025-08-17 11:09:20 +01:00
filtered, _ := t.serverService.ListServers(query)
t.serverList.UpdateServers(filtered)
if len(filtered) == 0 {
t.details.ShowEmpty()
}
}
2025-08-17 14:02:53 +01:00
func (t *tui) handleSearchToggle() {
t.showSearchBar()
}
func (t *tui) handleServerConnect() {
if server, ok := t.serverList.GetSelectedServer(); ok {
t.showConnectModal(server)
}
2025-08-17 11:09:20 +01:00
}
func (t *tui) handleServerSelectionChange(server domain.Server) {
t.details.UpdateServer(server)
}
2025-08-17 14:02:53 +01:00
func (t *tui) handleServerAdd() {
2025-08-17 11:09:20 +01:00
form := NewServerForm(ServerFormAdd, nil).
OnSave(t.handleServerSave).
2025-08-17 14:02:53 +01:00
OnCancel(t.handleFormCancel)
2025-08-17 11:09:20 +01:00
t.app.SetRoot(form, true)
}
2025-08-17 14:02:53 +01:00
func (t *tui) handleServerEdit() {
2025-08-17 11:09:20 +01:00
if server, ok := t.serverList.GetSelectedServer(); ok {
form := NewServerForm(ServerFormEdit, &server).
OnSave(t.handleServerSave).
2025-08-17 14:02:53 +01:00
OnCancel(t.handleFormCancel)
2025-08-17 11:09:20 +01:00
t.app.SetRoot(form, true)
}
}
func (t *tui) handleServerSave(server domain.Server, original *domain.Server) {
if original != nil {
// Edit mode
2025-08-17 15:36:32 +01:00
_ = t.serverService.UpdateServer(*original, server)
2025-08-17 11:09:20 +01:00
} else {
// Add mode
2025-08-17 15:36:32 +01:00
_ = t.serverService.AddServer(server)
2025-08-17 11:09:20 +01:00
}
t.refreshServerList()
2025-08-17 14:02:53 +01:00
t.handleFormCancel()
2025-08-17 11:09:20 +01:00
}
2025-08-17 14:02:53 +01:00
func (t *tui) handleServerDelete() {
2025-08-17 11:09:20 +01:00
if server, ok := t.serverList.GetSelectedServer(); ok {
_ = t.serverService.DeleteServer(server)
t.refreshServerList()
}
}
2025-08-17 14:02:53 +01:00
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)
// =============================================================================
2025-08-17 11:09:20 +01:00
func (t *tui) refreshServerList() {
query := ""
if t.searchVisible {
2025-08-17 17:48:24 +01:00
query = t.searchBar.InputField.GetText()
2025-08-17 11:09:20 +01:00
}
filtered, _ := t.serverService.ListServers(query)
t.serverList.UpdateServers(filtered)
}
func (t *tui) returnToMain() {
t.app.SetRoot(t.root, true)
}