Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: pre-render table row data #116

Merged
merged 6 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions internal/logging/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ func (w *writer) Write(p []byte) (int, error) {
msg.Message = string(d.Value())
default:
msg.Attributes = append(msg.Attributes, Attr{
Key: string(d.Key()),
Value: string(d.Value()),
Key: string(d.Key()),
Value: string(d.Value()),
Common: resource.New(resource.LogAttr, msg),
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/pubsub/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ type Logger interface {
}

// Broker allows clients to publish events and subscribe to events
type Broker[T any] struct {
type Broker[T resource.Resource] struct {
subs map[chan resource.Event[T]]struct{} // subscriptions
mu sync.Mutex // sync access to map
done chan struct{} // close when broker is shutting down
logger Logger
}

// NewBroker constructs a pub/sub broker.
func NewBroker[T any](logger Logger) *Broker[T] {
func NewBroker[T resource.Resource](logger Logger) *Broker[T] {
b := &Broker[T]{
subs: make(map[chan resource.Event[T]]struct{}),
done: make(chan struct{}),
Expand Down
6 changes: 1 addition & 5 deletions internal/resource/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type (
EventType string

// Event represents an event in the lifecycle of a resource
Event[T any] struct {
Event[T Resource] struct {
Type EventType
Payload T
}
Expand All @@ -20,7 +20,3 @@ type (
Publish(EventType, T)
}
)

func NewEvent[T any](t EventType, payload T) Event[T] {
return Event[T]{Type: t, Payload: payload}
}
37 changes: 19 additions & 18 deletions internal/tui/logs/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,29 @@ func (mm *Maker) Make(id resource.ID, width, height int) (tea.Model, error) {
valueColumn.Key: attr.Value,
}
}
items := map[resource.ID]logging.Attr{
resource.NewID(resource.LogAttr): {
Key: timeAttrKey,
Value: msg.Time.Format(timeFormat),
},
resource.NewID(resource.LogAttr): {
Key: messageAttrKey,
Value: msg.Message,
},
resource.NewID(resource.LogAttr): {
Key: levelAttrKey,
Value: coloredLogLevel(msg.Level),
},
}
for _, attr := range msg.Attributes {
items[resource.NewID(resource.LogAttr)] = attr
}
table := table.New(columns, renderer, width, height,
table.WithSortFunc(byAttribute),
table.WithSelectable[logging.Attr](false),
)
table.SetItems(items)
items := []logging.Attr{
{
Key: timeAttrKey,
Value: msg.Time.Format(timeFormat),
Common: resource.New(resource.LogAttr, resource.GlobalResource),
},
{
Key: messageAttrKey,
Value: msg.Message,
Common: resource.New(resource.LogAttr, resource.GlobalResource),
},
{
Key: levelAttrKey,
Value: coloredLogLevel(msg.Level),
Common: resource.New(resource.LogAttr, resource.GlobalResource),
},
}
items = append(items, msg.Attributes...)
table.SetItems(items...)

return model{
msg: msg,
Expand Down
6 changes: 6 additions & 0 deletions internal/tui/module/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ func (m list) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
)

switch msg := msg.(type) {
case resource.Event[*task.Task]:
// Re-render module whenever a task event is received belonging to the
// module.
if mod := msg.Payload.Module(); mod != nil {
m.table.AddItems(mod.(*module.Module))
}
case tea.KeyMsg:
switch {
case key.Matches(msg, localKeys.ReloadModules):
Expand Down
Loading
Loading