Skip to content

Commit 20a6844

Browse files
committed
feat: improve pretty formatting
1 parent ce8a88d commit 20a6844

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Running **dogstatsd-local** with the `-out raw` flag will output the plain udp p
6363

6464
```bash
6565
$ docker run -t -e "TERM=$TERM" -p 8125:8125/udp mroyme/dogstatsd-local -out pretty
66-
COUNTER namespace | metric 1.00 TAGS = test
66+
COUNTER namespace | metric 1.00 test
6767
```
6868

6969
The output will be colored if your shell supports colors.

cmd/dogstatsd-local/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func main() {
2525
out := flag.String("out", "pretty", "Output format: json|pretty|raw|short")
2626
rawTags := flag.String("tags", "", "Extra tags, comma delimited")
2727
maxNameWidth := flag.Int("max-name-width", 50,
28-
"Maximum length of name. Only used for 'pretty' format, increase if name is truncated")
28+
"Maximum length of name. Only used for 'pretty' format, increase if name is truncated."+
29+
" Values below 50 have no effect.")
2930
maxValueWidth := flag.Int("max-value-width", 15,
3031
"Maximum length of value. Only used for 'pretty' format, increase if value is truncated")
3132
debug := flag.Bool("debug", false, "Enable debug mode")

internal/format/pretty/pretty.go

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ func (h *Handler) New() messages.OutputHandler {
2828
return nil
2929
}
3030

31-
str := h.styledMetricType(metric)
32-
str += h.styledMetricName(metric, h.NameWidth)
33-
str += h.styledMetricValue(metric, h.ValueWidth)
34-
str += h.styledTags(metric, h.ExtraTags)
31+
str := h.StyledMetricType(metric)
32+
str += h.StyledMetricName(metric, h.NameWidth)
33+
str += h.StyledMetricValue(metric, h.ValueWidth)
34+
str += h.StyledTags(metric, h.ExtraTags)
3535
fmt.Println(str)
3636
return nil
3737
}
3838
}
3939

40-
func (h *Handler) styledMetricType(metric messages.DogStatsDMetric) string {
40+
func (h *Handler) StyledMetricType(metric messages.DogStatsDMetric) string {
4141
var fg lipgloss.AdaptiveColor
4242
metricType := metric.MetricType
4343
switch metricType {
@@ -56,44 +56,70 @@ func (h *Handler) styledMetricType(metric messages.DogStatsDMetric) string {
5656
}
5757
style := lipgloss.NewStyle().
5858
Width(11).
59+
Underline(true).
5960
Foreground(fg)
6061
return style.Render(strings.ToUpper(metricType.String()))
6162
}
6263

63-
func (h *Handler) styledMetricName(metric messages.DogStatsDMetric, width int) string {
64-
text := fmt.Sprintf("%s | %s", metric.Namespace, metric.Name)
65-
if len(text) > width {
66-
text = text[:width-3] + "..."
64+
func (h *Handler) StyledMetricName(metric messages.DogStatsDMetric, width int) string {
65+
// Minimum supported width is 50
66+
if width < 50 {
67+
width = 50
6768
}
68-
style := lipgloss.NewStyle().
69-
Width(50).
70-
MaxWidth(50).
71-
Foreground(h.Theme.Lavender())
72-
return style.Render(text)
69+
namespace := metric.Namespace
70+
name := metric.Name
71+
lenNamespace := len(namespace)
72+
lenName := len(name)
73+
textLen := lenNamespace + lenName
74+
75+
// 3 for the separator " | " + 1 for gap with the next field
76+
if textLen > width-4 {
77+
diff := textLen - (width - 4)
78+
if lenName-diff > 20 {
79+
name = name[:lenName-diff-1] + "~"
80+
} else if lenNamespace-diff > 20 {
81+
namespace = namespace[:lenNamespace-diff-1] + "~"
82+
} else {
83+
sub := diff / 2
84+
name = name[:lenName-sub-1] + "~"
85+
if diff%2 != 0 {
86+
sub++
87+
}
88+
namespace = namespace[:lenNamespace-sub-1] + "~"
89+
}
90+
}
91+
text := fmt.Sprintf("%s | %s",
92+
lipgloss.NewStyle().Foreground(h.Theme.Lavender()).Render(namespace),
93+
lipgloss.NewStyle().Bold(true).Foreground(h.Theme.Pink()).Render(name))
94+
return lipgloss.NewStyle().
95+
Width(width).
96+
MaxWidth(width).
97+
Render(text)
7398
}
7499

75-
func (h *Handler) styledMetricValue(metric messages.DogStatsDMetric, width int) string {
100+
func (h *Handler) StyledMetricValue(metric messages.DogStatsDMetric, width int) string {
76101
value := fmt.Sprintf("%.2f", metric.FloatValue)
77102
if len(value) > width {
78103
value = value[:width-3] + "..."
79104
}
80105
style := lipgloss.NewStyle().
81106
Width(15).
82107
MaxWidth(15).
108+
Bold(true).
83109
Foreground(h.Theme.Sapphire())
84110
if metric.MetricType == messages.TimerMetricType {
85111
value += "ms"
86112
}
87113
return style.Render(value)
88114
}
89115

90-
func (h *Handler) styledTags(metric messages.DogStatsDMetric, extraTags []string) string {
116+
func (h *Handler) StyledTags(metric messages.DogStatsDMetric, extraTags []string) string {
91117
style := lipgloss.NewStyle().
92-
Foreground(h.Theme.Subtext0())
118+
Foreground(h.Theme.Overlay0()).
119+
Italic(true)
93120
var tags []string
94121
for _, tag := range append(extraTags, metric.Tags...) {
95122
tags = append(tags, strings.TrimSpace(tag))
96123
}
97-
prefix := style.Foreground(h.Theme.Overlay0()).Render("TAGS =")
98-
return prefix + style.SetString(tags...).Render()
124+
return style.SetString(tags...).Render()
99125
}

0 commit comments

Comments
 (0)