Files
HostKeeper/pkg/tui/tui.go
T
swanadiva 98dfc1c79c feat: Task 16 — SSH Session Tab (multi-session)
- pkg/tui/session.go — SessionTab with live SSH terminal in a tab
- pkg/tui/messages.go — quitMsg, openSessionMsg, sessionOutputMsg
- pkg/tui/tabs.go — Tab.Close() interface, Add() returns tea.Cmd
- pkg/tui/host_list_tab.go — Enter opens session tab, q quits
- pkg/tui/tui.go — handle openSessionMsg/quitMsg, SetDataDir
- cmd/hostkeeper/tui.go — pass dataDir to model
2026-06-23 14:54:44 +07:00

122 lines
2.3 KiB
Go

package tui
import (
tea "github.com/charmbracelet/bubbletea"
"git.tukangketik.id/swanadiva/hostkeeper/internal/models"
)
// Screen represents different TUI screens (deprecated, use tabs)
type Screen int
const (
ScreenHostList Screen = iota
ScreenConnection
ScreenSettings
)
// Model represents the main TUI model
type Model struct {
tabs *TabManager
CurrentScreen Screen // deprecated, kept for backward compat
Hosts []*models.Host // deprecated
SelectedIndex int // deprecated
Error error
Quit bool
dataDir string
}
// New creates a new TUI model
func New() *Model {
hostList := NewHostListTab()
tm := NewTabManager(hostList)
return &Model{
tabs: tm,
CurrentScreen: ScreenHostList,
SelectedIndex: 0,
Quit: false,
}
}
// Init initializes the TUI
func (m *Model) Init() tea.Cmd {
return m.tabs.Init()
}
// Update handles messages and updates the model
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
// Only hard quit on ctrl+c (let tabs handle q)
if msg.String() == "ctrl+c" {
m.Quit = true
return m, tea.Quit
}
case quitMsg:
m.Quit = true
return m, tea.Quit
case openSessionMsg:
tab := NewSessionTab(msg.host, m.dataDir)
cmd := m.tabs.Add(tab)
return m, cmd
case sessionDoneMsg:
if msg.err != nil {
m.Error = msg.err
}
return m, nil
}
cmd, err := m.tabs.Update(msg)
if err != nil {
m.Error = err
}
// Sync deprecated fields
if ht := FindHostListTab(m.tabs.tabs); ht != nil {
m.Hosts = ht.Hosts()
m.SelectedIndex = ht.SelectedIndex()
}
return m, cmd
}
// View renders the TUI
func (m *Model) View() string {
if m.Quit {
m.tabs = nil
return "Thanks for using hostkeeper!\n"
}
if m.tabs == nil || m.tabs.Len() == 0 {
return "No tabs open. Press 'q' to quit.\n"
}
return m.tabs.View()
}
// LoadHosts loads hosts into the TUI model
func (m *Model) LoadHosts(hosts []*models.Host) {
if m.tabs == nil {
return
}
if ht := FindHostListTab(m.tabs.tabs); ht != nil {
ht.SetHosts(hosts)
m.Hosts = hosts
}
}
// SetDataDir sets the data directory for SSH connections
func (m *Model) SetDataDir(dir string) {
m.dataDir = dir
}
// TabManager returns the underlying tab manager
func (m *Model) TabManager() *TabManager {
return m.tabs
}