Skip to content

Commit a158bd9

Browse files
authored
Merge pull request #1380 from dgageot/small-tui-improvements
Small tui code improvements
2 parents 233f403 + 9d560e4 commit a158bd9

File tree

4 files changed

+41
-68
lines changed

4 files changed

+41
-68
lines changed

pkg/app/app.go

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log/slog"
77
"os/exec"
8+
"slices"
89
"strings"
910
"time"
1011

@@ -410,10 +411,8 @@ func (a *App) trackCustomModel(modelRef string) {
410411
}
411412

412413
// Check if already tracked
413-
for _, existing := range a.session.CustomModelsUsed {
414-
if existing == modelRef {
415-
return
416-
}
414+
if slices.Contains(a.session.CustomModelsUsed, modelRef) {
415+
return
417416
}
418417

419418
a.session.CustomModelsUsed = append(a.session.CustomModelsUsed, modelRef)
@@ -508,56 +507,46 @@ func (a *App) applySessionModelOverrides(ctx context.Context, sess *session.Sess
508507
func (a *App) throttleEvents(ctx context.Context, in <-chan tea.Msg) <-chan tea.Msg {
509508
out := make(chan tea.Msg, 128)
510509

511-
go func() {
512-
defer close(out)
513-
514-
var buffer []tea.Msg
515-
ticker := time.NewTicker(a.throttleDuration)
516-
defer ticker.Stop()
510+
var buffer []tea.Msg
511+
var timerCh <-chan time.Time
517512

518-
flush := func() {
519-
if len(buffer) == 0 {
513+
flush := func() {
514+
for _, msg := range a.mergeEvents(buffer) {
515+
select {
516+
case out <- msg:
517+
case <-ctx.Done():
520518
return
521519
}
522-
523-
// Merge events if possible
524-
merged := a.mergeEvents(buffer)
525-
for _, msg := range merged {
526-
select {
527-
case out <- msg:
528-
case <-ctx.Done():
529-
return
530-
}
531-
}
532-
buffer = buffer[:0]
533520
}
534521

522+
buffer = buffer[:0]
523+
timerCh = nil
524+
}
525+
defer flush()
526+
527+
go func() {
528+
defer close(out)
529+
535530
for {
536531
select {
537532
case <-ctx.Done():
538-
flush()
539533
return
540534

541535
case msg, ok := <-in:
542536
if !ok {
543-
flush()
544537
return
545538
}
546539

547-
// Check if this event type should be throttled
540+
buffer = append(buffer, msg)
548541
if a.shouldThrottle(msg) {
549-
buffer = append(buffer, msg)
550-
} else {
551-
// Pass through immediately for important events
552-
flush() // Flush any buffered events first
553-
select {
554-
case out <- msg:
555-
case <-ctx.Done():
556-
return
542+
if timerCh == nil {
543+
timerCh = time.After(a.throttleDuration)
557544
}
545+
} else {
546+
flush()
558547
}
559548

560-
case <-ticker.C:
549+
case <-timerCh:
561550
flush()
562551
}
563552
}
@@ -631,14 +620,14 @@ func (a *App) mergeEvents(events []tea.Msg) []tea.Msg {
631620

632621
case *runtime.PartialToolCallEvent:
633622
// For PartialToolCallEvent, keep only the latest one per tool call ID
634-
// Check if there's a newer one in the buffer
623+
// Only merge consecutive events with the same ID
635624
latest := ev
636-
for j := i + 1; j < len(events); j++ {
637-
if next, ok := events[j].(*runtime.PartialToolCallEvent); ok {
638-
if next.ToolCall.ID == ev.ToolCall.ID {
639-
latest = next
640-
i = j // Skip to this position
641-
}
625+
for i+1 < len(events) {
626+
if next, ok := events[i+1].(*runtime.PartialToolCallEvent); ok && next.ToolCall.ID == ev.ToolCall.ID {
627+
latest = next
628+
i++
629+
} else {
630+
break
642631
}
643632
}
644633
result = append(result, latest)

pkg/tui/components/sidebar/sidebar.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,7 @@ func (m *model) Update(msg tea.Msg) (layout.Model, tea.Cmd) {
373373
cmds = append(cmds, cmd)
374374
}
375375

376-
if len(cmds) > 0 {
377-
return m, tea.Batch(cmds...)
378-
}
379-
return m, nil
376+
return m, tea.Batch(cmds...)
380377
}
381378
}
382379

pkg/tui/page/chat/chat.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,6 @@ func (p *chatPage) Init() tea.Cmd {
277277

278278
// Update handles messages and updates the page state
279279
func (p *chatPage) Update(msg tea.Msg) (layout.Model, tea.Cmd) {
280-
var cmds []tea.Cmd
281-
282280
switch msg := msg.(type) {
283281
case tea.KeyboardEnhancementsMsg:
284282
// Track keyboard enhancement support and update help text
@@ -290,24 +288,20 @@ func (p *chatPage) Update(msg tea.Msg) (layout.Model, tea.Cmd) {
290288
return p, editorCmd
291289

292290
case tea.WindowSizeMsg:
293-
cmd := p.SetSize(msg.Width, msg.Height)
294-
cmds = append(cmds, cmd)
291+
cmdSize := p.SetSize(msg.Width, msg.Height)
295292

296293
// Forward to sidebar component
297294
sidebarModel, sidebarCmd := p.sidebar.Update(msg)
298295
p.sidebar = sidebarModel.(sidebar.Model)
299-
cmds = append(cmds, sidebarCmd)
300296

301297
// Forward to chat component
302298
chatModel, chatCmd := p.messages.Update(msg)
303299
p.messages = chatModel.(messages.Model)
304-
cmds = append(cmds, chatCmd)
305300

306301
// Forward to editor component
307302
editorModel, editorCmd := p.editor.Update(msg)
308303
p.editor = editorModel.(editor.Editor)
309-
cmds = append(cmds, editorCmd)
310-
return p, tea.Batch(cmds...)
304+
return p, tea.Batch(cmdSize, sidebarCmd, chatCmd, editorCmd)
311305

312306
case tea.KeyPressMsg:
313307
if model, cmd, handled := p.handleKeyPress(msg); handled {
@@ -372,23 +366,21 @@ func (p *chatPage) Update(msg tea.Msg) (layout.Model, tea.Cmd) {
372366

373367
sidebarModel, sidebarCmd := p.sidebar.Update(msg)
374368
p.sidebar = sidebarModel.(sidebar.Model)
375-
cmds = append(cmds, sidebarCmd)
376369

377370
chatModel, chatCmd := p.messages.Update(msg)
378371
p.messages = chatModel.(messages.Model)
379-
cmds = append(cmds, chatCmd)
380372

381373
editorModel, editorCmd := p.editor.Update(msg)
382374
p.editor = editorModel.(editor.Editor)
383-
cmds = append(cmds, editorCmd)
384375

376+
var cmdSpinner tea.Cmd
385377
if p.working {
386-
model, cmd := p.spinner.Update(msg)
378+
var model layout.Model
379+
model, cmdSpinner = p.spinner.Update(msg)
387380
p.spinner = model.(spinner.Spinner)
388-
cmds = append(cmds, cmd)
389381
}
390382

391-
return p, tea.Batch(cmds...)
383+
return p, tea.Batch(sidebarCmd, chatCmd, editorCmd, cmdSpinner)
392384
}
393385

394386
func (p *chatPage) setWorking(working bool) tea.Cmd {

pkg/tui/tui.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,18 +387,13 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
387387
return a, dialogCmd
388388
}
389389

390-
var cmds []tea.Cmd
391-
var cmd tea.Cmd
392-
393-
updated, cmd := a.completions.Update(msg)
394-
cmds = append(cmds, cmd)
390+
updated, cmdCompletions := a.completions.Update(msg)
395391
a.completions = updated.(completion.Manager)
396392

397-
updated, cmd = a.chatPage.Update(msg)
398-
cmds = append(cmds, cmd)
393+
updated, cmdChatPage := a.chatPage.Update(msg)
399394
a.chatPage = updated.(chat.Page)
400395

401-
return a, tea.Batch(cmds...)
396+
return a, tea.Batch(cmdCompletions, cmdChatPage)
402397
}
403398
}
404399

0 commit comments

Comments
 (0)