logs.go

 1package page
 2
 3import (
 4	"github.com/charmbracelet/bubbles/v2/key"
 5	tea "github.com/charmbracelet/bubbletea/v2"
 6	"github.com/charmbracelet/crush/internal/tui/components/logs"
 7	"github.com/charmbracelet/crush/internal/tui/layout"
 8	"github.com/charmbracelet/crush/internal/tui/styles"
 9	"github.com/charmbracelet/crush/internal/tui/util"
10	"github.com/charmbracelet/lipgloss/v2"
11)
12
13var LogsPage PageID = "logs"
14
15type LogPage interface {
16	util.Model
17	layout.Sizeable
18	layout.Bindings
19}
20type logsPage struct {
21	width, height int
22	table         layout.Container
23	details       layout.Container
24}
25
26func (p *logsPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
27	var cmds []tea.Cmd
28	switch msg := msg.(type) {
29	case tea.WindowSizeMsg:
30		p.width = msg.Width
31		p.height = msg.Height
32		return p, p.SetSize(msg.Width, msg.Height)
33	}
34
35	table, cmd := p.table.Update(msg)
36	cmds = append(cmds, cmd)
37	p.table = table.(layout.Container)
38	details, cmd := p.details.Update(msg)
39	cmds = append(cmds, cmd)
40	p.details = details.(layout.Container)
41
42	return p, tea.Batch(cmds...)
43}
44
45func (p *logsPage) View() tea.View {
46	style := styles.CurrentTheme().S().Base.Width(p.width).Height(p.height)
47	return tea.NewView(
48		style.Render(
49			lipgloss.JoinVertical(lipgloss.Top,
50				p.table.View().String(),
51				p.details.View().String(),
52			),
53		),
54	)
55}
56
57func (p *logsPage) BindingKeys() []key.Binding {
58	return p.table.BindingKeys()
59}
60
61// GetSize implements LogPage.
62func (p *logsPage) GetSize() (int, int) {
63	return p.width, p.height
64}
65
66// SetSize implements LogPage.
67func (p *logsPage) SetSize(width int, height int) tea.Cmd {
68	p.width = width
69	p.height = height
70	return tea.Batch(
71		p.table.SetSize(width, height/2),
72		p.details.SetSize(width, height/2),
73	)
74}
75
76func (p *logsPage) Init() tea.Cmd {
77	return tea.Batch(
78		p.table.Init(),
79		p.details.Init(),
80	)
81}
82
83func NewLogsPage() LogPage {
84	return &logsPage{
85		table:   layout.NewContainer(logs.NewLogsTable(), layout.WithBorderAll()),
86		details: layout.NewContainer(logs.NewLogsDetails(), layout.WithBorderAll()),
87	}
88}