logs.go

 1package page
 2
 3import (
 4	"github.com/charmbracelet/bubbles/key"
 5	tea "github.com/charmbracelet/bubbletea"
 6	"github.com/charmbracelet/lipgloss"
 7	"github.com/kujtimiihoxha/opencode/internal/tui/components/logs"
 8	"github.com/kujtimiihoxha/opencode/internal/tui/layout"
 9	"github.com/kujtimiihoxha/opencode/internal/tui/styles"
10)
11
12var LogsPage PageID = "logs"
13
14type LogPage interface {
15	tea.Model
16	layout.Sizeable
17	layout.Bindings
18}
19type logsPage struct {
20	width, height int
21	table         layout.Container
22	details       layout.Container
23}
24
25func (p *logsPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
26	switch msg := msg.(type) {
27	case tea.WindowSizeMsg:
28		p.width = msg.Width
29		p.height = msg.Height
30		p.table.SetSize(msg.Width, msg.Height/2)
31		p.details.SetSize(msg.Width, msg.Height/2)
32	}
33
34	var cmds []tea.Cmd
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() string {
46	style := styles.BaseStyle.Width(p.width).Height(p.height)
47	return style.Render(lipgloss.JoinVertical(lipgloss.Top,
48		p.table.View(),
49		p.details.View(),
50	))
51}
52
53func (p *logsPage) BindingKeys() []key.Binding {
54	return p.table.BindingKeys()
55}
56
57// GetSize implements LogPage.
58func (p *logsPage) GetSize() (int, int) {
59	return p.width, p.height
60}
61
62// SetSize implements LogPage.
63func (p *logsPage) SetSize(width int, height int) {
64	p.width = width
65	p.height = height
66	p.table.SetSize(width, height/2)
67	p.details.SetSize(width, height/2)
68}
69
70func (p *logsPage) Init() tea.Cmd {
71	return tea.Batch(
72		p.table.Init(),
73		p.details.Init(),
74	)
75}
76
77func NewLogsPage() LogPage {
78	return &logsPage{
79		table:   layout.NewContainer(logs.NewLogsTable(), layout.WithBorderAll(), layout.WithBorderColor(styles.ForgroundDim)),
80		details: layout.NewContainer(logs.NewLogsDetails(), layout.WithBorderAll(), layout.WithBorderColor(styles.ForgroundDim)),
81	}
82}