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/opencode-ai/opencode/internal/tui/components/logs"
 8	"github.com/opencode-ai/opencode/internal/tui/layout"
 9	"github.com/opencode-ai/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	var cmds []tea.Cmd
27	switch msg := msg.(type) {
28	case tea.WindowSizeMsg:
29		p.width = msg.Width
30		p.height = msg.Height
31		return p, p.SetSize(msg.Width, msg.Height)
32	}
33
34	table, cmd := p.table.Update(msg)
35	cmds = append(cmds, cmd)
36	p.table = table.(layout.Container)
37	details, cmd := p.details.Update(msg)
38	cmds = append(cmds, cmd)
39	p.details = details.(layout.Container)
40
41	return p, tea.Batch(cmds...)
42}
43
44func (p *logsPage) View() string {
45	style := styles.BaseStyle().Width(p.width).Height(p.height)
46	return style.Render(lipgloss.JoinVertical(lipgloss.Top,
47		p.table.View(),
48		p.details.View(),
49	))
50}
51
52func (p *logsPage) BindingKeys() []key.Binding {
53	return p.table.BindingKeys()
54}
55
56// GetSize implements LogPage.
57func (p *logsPage) GetSize() (int, int) {
58	return p.width, p.height
59}
60
61// SetSize implements LogPage.
62func (p *logsPage) SetSize(width int, height int) tea.Cmd {
63	p.width = width
64	p.height = height
65	return tea.Batch(
66		p.table.SetSize(width, height/2),
67		p.details.SetSize(width, height/2),
68	)
69}
70
71func (p *logsPage) Init() tea.Cmd {
72	return tea.Batch(
73		p.table.Init(),
74		p.details.Init(),
75	)
76}
77
78func NewLogsPage() LogPage {
79	return &logsPage{
80		table:   layout.NewContainer(logs.NewLogsTable(), layout.WithBorderAll()),
81		details: layout.NewContainer(logs.NewLogsDetails(), layout.WithBorderAll()),
82	}
83}