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 } // 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: switch msg.String() { case "ctrl+c", "q": m.Quit = true return m, tea.Quit } } 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 } } // TabManager returns the underlying tab manager func (m *Model) TabManager() *TabManager { return m.tabs }