1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
package logs
import (
"encoding/json"
"slices"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/opencode-ai/opencode/internal/logging"
"github.com/opencode-ai/opencode/internal/pubsub"
"github.com/opencode-ai/opencode/internal/tui/layout"
"github.com/opencode-ai/opencode/internal/tui/styles"
"github.com/opencode-ai/opencode/internal/tui/theme"
"github.com/opencode-ai/opencode/internal/tui/util"
)
type TableComponent interface {
tea.Model
layout.Sizeable
layout.Bindings
}
type tableCmp struct {
table table.Model
}
type selectedLogMsg logging.LogMessage
func (i *tableCmp) Init() tea.Cmd {
i.setRows()
return nil
}
func (i *tableCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg.(type) {
case pubsub.Event[logging.LogMessage]:
i.setRows()
return i, nil
}
prevSelectedRow := i.table.SelectedRow()
t, cmd := i.table.Update(msg)
cmds = append(cmds, cmd)
i.table = t
selectedRow := i.table.SelectedRow()
if selectedRow != nil {
if prevSelectedRow == nil || selectedRow[0] == prevSelectedRow[0] {
var log logging.LogMessage
for _, row := range logging.List() {
if row.ID == selectedRow[0] {
log = row
break
}
}
if log.ID != "" {
cmds = append(cmds, util.CmdHandler(selectedLogMsg(log)))
}
}
}
return i, tea.Batch(cmds...)
}
func (i *tableCmp) View() string {
t := theme.CurrentTheme()
return styles.ForceReplaceBackgroundWithLipgloss(i.table.View(), t.Background())
}
func (i *tableCmp) GetSize() (int, int) {
return i.table.Width(), i.table.Height()
}
func (i *tableCmp) SetSize(width int, height int) tea.Cmd {
i.table.SetWidth(width)
i.table.SetHeight(height)
cloumns := i.table.Columns()
for i, col := range cloumns {
col.Width = (width / len(cloumns)) - 2
cloumns[i] = col
}
i.table.SetColumns(cloumns)
return nil
}
func (i *tableCmp) BindingKeys() []key.Binding {
return layout.KeyMapToSlice(i.table.KeyMap)
}
func (i *tableCmp) setRows() {
rows := []table.Row{}
logs := logging.List()
slices.SortFunc(logs, func(a, b logging.LogMessage) int {
if a.Time.Before(b.Time) {
return 1
}
if a.Time.After(b.Time) {
return -1
}
return 0
})
for _, log := range logs {
bm, _ := json.Marshal(log.Attributes)
row := table.Row{
log.ID,
log.Time.Format("15:04:05"),
log.Level,
log.Message,
string(bm),
}
rows = append(rows, row)
}
i.table.SetRows(rows)
}
func NewLogsTable() TableComponent {
t := theme.CurrentTheme()
columns := []table.Column{
{Title: "ID", Width: 4},
{Title: "Time", Width: 4},
{Title: "Level", Width: 10},
{Title: "Message", Width: 10},
{Title: "Attributes", Width: 10},
}
defaultStyles := table.DefaultStyles()
defaultStyles.Selected = defaultStyles.Selected.Foreground(t.Primary())
tableModel := table.New(
table.WithColumns(columns),
table.WithStyles(defaultStyles),
)
tableModel.Focus()
return &tableCmp{
table: tableModel,
}
}
|