hosts-go/internal/tui/update.go

76 lines
1.4 KiB
Go

package tui
import (
tea "github.com/charmbracelet/bubbletea"
)
// Update handles all messages for the TUI.
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
case "tab":
if m.focus == listPane {
m.focus = detailPane
} else {
m.focus = listPane
}
case "up", "k":
if m.focus == detailPane {
m.detail.LineUp(1)
} else {
m.list, cmd = m.list.Update(msg)
m.refreshDetail()
}
return m, cmd
case "down", "j":
if m.focus == detailPane {
m.detail.LineDown(1)
} else {
m.list, cmd = m.list.Update(msg)
m.refreshDetail()
}
return m, cmd
}
if m.focus == listPane {
m.list, cmd = m.list.Update(msg)
m.refreshDetail()
}
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height - 1
leftWidth := msg.Width / 2
rightWidth := msg.Width - leftWidth
leftHeight := m.height
rightHeight := m.height
if m.focus == listPane {
leftWidth -= 2
leftHeight -= 2
} else {
rightWidth -= 2
rightHeight -= 2
}
leftWidth -= 2
rightWidth -= 2
m.list.SetSize(leftWidth, leftHeight)
m.detail.Width = rightWidth
m.detail.Height = rightHeight
if m.focus == listPane {
m.list, cmd = m.list.Update(msg)
}
default:
if m.focus == listPane {
m.list, cmd = m.list.Update(msg)
m.refreshDetail()
}
}
return m, cmd
}