Skip to content

Commit

Permalink
Make resizable span name column in timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
ymtdzzz committed Jul 2, 2024
1 parent 78ba1b3 commit 9f040c6
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
34 changes: 31 additions & 3 deletions tuiexporter/internal/tui/component/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
)

const (
TIMELINE_DETAILS_IDX = 1 // index of details in the base flex container
TIMELINE_TREE_TITLE = "Details (d)"
TIMELINE_DETAILS_IDX = 1 // index of details in the base flex container
TIMELINE_TREE_TITLE = "Details (d)"
SPAN_NAME_COLUMN_WIDTH_RESIZE_UNIT = 5
SPAN_NAME_COLUMN_WIDTH_DEFAULT = 30
)

var colors = []tcell.Color{
Expand Down Expand Up @@ -77,8 +79,9 @@ func DrawTimeline(traceID string, tcache *telemetry.TraceCache, lcache *telemetr
})

// place spans on the timeline
snameWidth := SPAN_NAME_COLUMN_WIDTH_DEFAULT
grid := tview.NewGrid().
SetColumns(30, 0). // TODO: dynamic width
SetColumns(snameWidth, 0). // TODO: dynamic width
SetBorders(true).
AddItem(title, 0, 0, 1, 1, 0, 0, false).
AddItem(timeline, 0, 1, 1, 1, 0, 0, false)
Expand Down Expand Up @@ -139,6 +142,15 @@ func DrawTimeline(traceID string, tcache *telemetry.TraceCache, lcache *telemetr
traceContainer.AddItem(details, 0, 3, false)
}
return nil
case tcell.KeyCtrlL:
_, _, w, _ := grid.GetInnerRect()
snameWidth = widenInLimit(SPAN_NAME_COLUMN_WIDTH_RESIZE_UNIT, snameWidth, w)
grid.SetColumns(snameWidth, 0)
return nil
case tcell.KeyCtrlH:
snameWidth = narrowInLimit(SPAN_NAME_COLUMN_WIDTH_RESIZE_UNIT, snameWidth, SPAN_NAME_COLUMN_WIDTH_DEFAULT)
grid.SetColumns(snameWidth, 0)
return nil
}
return event
})
Expand Down Expand Up @@ -179,7 +191,23 @@ func DrawTimeline(traceID string, tcache *telemetry.TraceCache, lcache *telemetr
return base, KeyMaps{
*tcell.NewEventKey(tcell.KeyUp, ' ', tcell.ModNone): "Move up",
*tcell.NewEventKey(tcell.KeyDown, ' ', tcell.ModNone): "Move down",
*tcell.NewEventKey(tcell.KeyRune, 'L', tcell.ModCtrl): "Widen side col",
*tcell.NewEventKey(tcell.KeyRune, 'H', tcell.ModCtrl): "Narrow side col",
}
}

func narrowInLimit(step, curr, limit int) int {
if curr-step >= limit {
return curr - step
}
return curr
}

func widenInLimit(step, curr, limit int) int {
if curr+step <= limit {
return curr + step
}
return curr
}

func calculateTimelineUnit(duration time.Duration) (unit time.Duration, count int) {
Expand Down
64 changes: 64 additions & 0 deletions tuiexporter/internal/tui/component/timeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,67 @@ func TestRoundDownDuration(t *testing.T) {
})
}
}

func TestNarrowInLimit(t *testing.T) {
tests := []struct {
name string
step int
curr int
limit int
want int
}{
{
name: "Modified",
step: 5,
curr: 35,
limit: 30,
want: 30,
},
{
name: "No_Effect_Limit_Over",
step: 5,
curr: 34,
limit: 30,
want: 34,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := narrowInLimit(tt.step, tt.curr, tt.limit)
assert.Equal(t, tt.want, got)
})
}
}

func TestWidenInLimit(t *testing.T) {
tests := []struct {
name string
step int
curr int
limit int
want int
}{
{
name: "Modified",
step: 5,
curr: 35,
limit: 40,
want: 40,
},
{
name: "No_Effect_Limit_Over",
step: 5,
curr: 30,
limit: 34,
want: 30,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := widenInLimit(tt.step, tt.curr, tt.limit)
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 9f040c6

Please sign in to comment.