fix: remove the silly map
AI generated code is still not perfect...
This commit is contained in:
parent
7bcf8e8301
commit
d51d1fea56
11
internal/tui/messages.go
Normal file
11
internal/tui/messages.go
Normal file
@ -0,0 +1,11 @@
|
||||
package tui
|
||||
|
||||
import "termtap.dev/internal/model"
|
||||
|
||||
type EventMsg struct {
|
||||
value model.Event
|
||||
}
|
||||
|
||||
type ErrMsg struct {
|
||||
err error
|
||||
}
|
||||
@ -4,58 +4,48 @@ import (
|
||||
"fmt"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/google/uuid"
|
||||
"termtap.dev/internal/model"
|
||||
)
|
||||
|
||||
// TODO: How big can we actually make this?
|
||||
const (
|
||||
maxEvents = 200
|
||||
maxRequests = 200
|
||||
maxEvents = 256
|
||||
maxRequests = 256
|
||||
)
|
||||
|
||||
type EventMsg struct {
|
||||
value model.Event
|
||||
}
|
||||
|
||||
type ErrMsg struct {
|
||||
err error
|
||||
}
|
||||
|
||||
type Model struct {
|
||||
msgCh <-chan model.Event
|
||||
channel <-chan model.Event
|
||||
|
||||
events []model.Event
|
||||
requestOrder []uuid.UUID
|
||||
requests map[uuid.UUID]model.Request
|
||||
events []model.Event
|
||||
requests []model.Request
|
||||
|
||||
width int
|
||||
height int
|
||||
}
|
||||
|
||||
func NewModel(msgCh <-chan model.Event) Model {
|
||||
func NewModel(ch <-chan model.Event) Model {
|
||||
return Model{
|
||||
msgCh: msgCh,
|
||||
events: make([]model.Event, 0, maxEvents),
|
||||
requestOrder: make([]uuid.UUID, 0, maxRequests),
|
||||
requests: map[uuid.UUID]model.Request{},
|
||||
width: 100,
|
||||
height: 28,
|
||||
channel: ch,
|
||||
events: make([]model.Event, 0, maxEvents),
|
||||
requests: make([]model.Request, 0, maxRequests),
|
||||
width: 100,
|
||||
height: 28,
|
||||
}
|
||||
}
|
||||
|
||||
func Run(msgCh <-chan model.Event) error {
|
||||
p := tea.NewProgram(NewModel(msgCh), tea.WithAltScreen())
|
||||
func Run(ch <-chan model.Event) error {
|
||||
p := tea.NewProgram(NewModel(ch), tea.WithAltScreen())
|
||||
_, err := p.Run()
|
||||
return err
|
||||
}
|
||||
|
||||
func (m Model) Init() tea.Cmd {
|
||||
return waitForAppMessage(m.msgCh)
|
||||
return waitForEvent(m.channel)
|
||||
}
|
||||
|
||||
func waitForAppMessage(msgCh <-chan model.Event) tea.Cmd {
|
||||
func waitForEvent(ch <-chan model.Event) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
msg, ok := <-msgCh
|
||||
msg, ok := <-ch
|
||||
if !ok {
|
||||
return ErrMsg{err: fmt.Errorf("event channel closed")}
|
||||
}
|
||||
|
||||
@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/google/uuid"
|
||||
"termtap.dev/internal/model"
|
||||
)
|
||||
|
||||
@ -32,7 +31,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
case EventMsg:
|
||||
m.pushEvent(msg.value)
|
||||
m.applyMessage(msg.value)
|
||||
return m, waitForAppMessage(m.msgCh)
|
||||
return m, waitForEvent(m.channel)
|
||||
}
|
||||
|
||||
return m, nil
|
||||
@ -48,30 +47,29 @@ func (m *Model) pushEvent(msg model.Event) {
|
||||
func (m *Model) applyMessage(msg model.Event) {
|
||||
switch msg.Type {
|
||||
case model.EventTypeRequestStarted:
|
||||
m.upsertRequest(msg.Request, true)
|
||||
m.createRequest(msg.Request)
|
||||
case model.EventTypeRequestFinished, model.EventTypeRequestFailed:
|
||||
m.upsertRequest(msg.Request, false)
|
||||
m.updateRequest(msg.Request)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Model) upsertRequest(req model.Request, addIfMissing bool) {
|
||||
if req.ID == uuid.Nil {
|
||||
return
|
||||
}
|
||||
func (m *Model) createRequest(req model.Request) {
|
||||
m.requests = append(m.requests, req)
|
||||
|
||||
_, exists := m.requests[req.ID]
|
||||
if !exists && !addIfMissing {
|
||||
return
|
||||
// If we passed the max, delete the first one
|
||||
// Maybe we should notify the user?
|
||||
if len(m.requests) > maxRequests {
|
||||
m.requests = m.requests[1:]
|
||||
}
|
||||
}
|
||||
|
||||
if !exists {
|
||||
m.requestOrder = append(m.requestOrder, req.ID)
|
||||
if len(m.requestOrder) > maxRequests {
|
||||
drop := m.requestOrder[0]
|
||||
delete(m.requests, drop)
|
||||
m.requestOrder = m.requestOrder[1:]
|
||||
func (m *Model) updateRequest(req model.Request) {
|
||||
// Traverse backward, since the newest one is at the end, and its likely we will be
|
||||
// updated a new request.
|
||||
for i := len(m.requests) - 1; i >= 0; i-- {
|
||||
if m.requests[i].ID == req.ID {
|
||||
m.requests[i] = req
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
m.requests[req.ID] = req
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ func (m Model) View() string {
|
||||
|
||||
return strings.Join([]string{
|
||||
"termtap - live session",
|
||||
fmt.Sprintf("events=%d requests=%d", len(m.events), len(m.requestOrder)),
|
||||
fmt.Sprintf("events=%d requests=%d", len(m.events), len(m.requests)),
|
||||
"keys: q/esc/ctrl+c quit",
|
||||
"",
|
||||
"Recent events:",
|
||||
@ -42,22 +42,16 @@ func (m Model) renderEvents(limit int) string {
|
||||
}
|
||||
|
||||
func (m Model) renderRequests(limit int) string {
|
||||
if len(m.requestOrder) == 0 {
|
||||
if len(m.requests) == 0 {
|
||||
return " (none yet)"
|
||||
}
|
||||
|
||||
start := len(m.requestOrder) - limit
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
start := max(0, len(m.requests)-limit)
|
||||
|
||||
rows := make([]string, 0, len(m.requestOrder)-start)
|
||||
for i := start; i < len(m.requestOrder); i++ {
|
||||
id := m.requestOrder[i]
|
||||
req, ok := m.requests[id]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
// Traverse backwards since we don't have a stack
|
||||
rows := make([]string, 0, len(m.requests)-start)
|
||||
for i := len(m.requests) - 1; i >= start; i-- {
|
||||
req := m.requests[i]
|
||||
|
||||
state := "done"
|
||||
if req.Pending {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user