Skip to content
Draft
Changes from 3 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
63 changes: 52 additions & 11 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ui
import (
"fmt"
"log"
"slices"
"sort"
"strings"

Expand All @@ -18,6 +17,28 @@ const (
keepRoutineHist = 100
)

// statusAbbreviations maps common goroutine status names to consistent abbreviations
var statusAbbreviations = map[string]string{
"running": "run",
"runnable": "rbl",
"waiting": "wai",
"IO wait": "IO",
"chan receive": "cha",
"chan send": "ch1",
"select": "sel",
"sync.Mutex.Lock": "syn",
"sync.Cond.Wait": "syw",
"syscall": "sys",
"sleep": "slp",
"idle": "idl",
"dead": "ded",
"copystack": "cps",
"preempted": "pre",
"GC assist wait": "gca",
"GC sweep wait": "gcs",
"GC scavenge wait": "gcv",
}

// UI contains all user interface elements
type UI struct {
list *widgets.List
Expand Down Expand Up @@ -90,7 +111,6 @@ func NewUI() *UI {
barchart.BarWidth = 3
barchart.BarGap = 1
barchart.BarColors = []termui.Color{termui.ColorGreen}
barchart.NumStyles = []termui.Style{termui.NewStyle(termui.ColorBlack)}
barchart.LabelStyles = []termui.Style{termui.NewStyle(termui.ColorWhite)}
barchart.PaddingTop = padding
barchart.PaddingRight = padding
Expand Down Expand Up @@ -181,21 +201,42 @@ func (ui *UI) updateStatus() {
sort.Strings(types)
data := make([]float64, len(types))
labels := make([]string, len(types))
label := ""
uniqueID := 1
legend := ""
usedAbbrevs := make(map[string]bool)

for idx, t := range types {
data[idx] = typeCount[t]
newLabel := t[:3]
if slices.Contains(labels, newLabel) {
newLabel = fmt.Sprintf("%s%d", t[:2], uniqueID)
uniqueID++

// Use predefined abbreviation or fallback to custom logic
abbrev, exists := statusAbbreviations[t]
if !exists {
// Fallback: use first 3 characters
abbrev = t[:min(3, len(t))]
}

// Handle collisions by adding a number suffix
originalAbbrev := abbrev
counter := 2
for usedAbbrevs[abbrev] {
abbrev = fmt.Sprintf("%s%d", originalAbbrev[:min(2, len(originalAbbrev))], counter)
counter++
}
labels[idx] = newLabel
label = fmt.Sprintf("%s%s: %s\n", label, newLabel, t)
usedAbbrevs[abbrev] = true

labels[idx] = abbrev
// Add to legend with count
legend = fmt.Sprintf("%s%s: %s (%.0f)\n", legend, abbrev, t, typeCount[t])
}
ui.barchart.Data = data
ui.barchart.Labels = labels
ui.barchartLegend.Text = label
ui.barchartLegend.Text = legend
}

func min(a, b int) int {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go 1.21, min and max are available as builtins and you do not need to write them at all.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The min function has been removed in commit 2b9f941 as part of the simplified approach that no longer uses abbreviations.

if a < b {
return a
}
return b
}

func (ui *UI) updateList() {
Expand Down