Skip to content

Commit

Permalink
refactor: simplify text styling when using CI mode (jsdelivr#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
radulucut authored Jul 23, 2024
1 parent c73170e commit d2423b3
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 80 deletions.
4 changes: 4 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func (r *Root) updateContext(cmd string, args []string) error {
r.ctx.CIMode = true
}

if r.ctx.CIMode {
r.printer.DisableStyling()
}

return nil
}

Expand Down
6 changes: 1 addition & 5 deletions cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ func (r *Root) pingInfinite(opts *globalping.MeasurementCreate) error {
e, ok := err.(*globalping.MeasurementError)
if ok && e.Code == http.StatusTooManyRequests {
r.Cmd.SilenceErrors = true
if r.ctx.CIMode {
r.printer.Printf("> %s\n", e.Message)
} else {
r.printer.Printf(r.printer.Color("> "+e.Message, view.ColorLightYellow) + "\n")
}
r.printer.Printf(r.printer.Color("> "+e.Message, view.ColorLightYellow) + "\n")
}
}
r.viewer.OutputShare()
Expand Down
20 changes: 15 additions & 5 deletions view/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ func Test_Output_Default_HTTP_Get(t *testing.T) {
}

w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
printer.DisableStyling()
viewer := NewViewer(&Context{
Cmd: "http",
CIMode: true,
}, NewPrinter(nil, w, w), nil, gbMock)
}, printer, nil, gbMock)

viewer.Output(measurementID1, m)

Expand Down Expand Up @@ -127,11 +129,13 @@ func Test_Output_Default_HTTP_Get_Share(t *testing.T) {
},
}
w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
printer.DisableStyling()
viewer := NewViewer(&Context{
Cmd: "http",
CIMode: true,
Share: true,
}, NewPrinter(nil, w, w), nil, gbMock)
}, printer, nil, gbMock)

viewer.Output(measurementID1, m)

Expand Down Expand Up @@ -193,11 +197,13 @@ func Test_Output_Default_HTTP_Get_Full(t *testing.T) {
},
}
w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
printer.DisableStyling()
viewer := NewViewer(&Context{
Cmd: "http",
CIMode: true,
Full: true,
}, NewPrinter(nil, w, w), nil, gbMock)
}, printer, nil, gbMock)

viewer.Output(measurementID1, m)

Expand Down Expand Up @@ -259,10 +265,12 @@ func Test_Output_Default_HTTP_Head(t *testing.T) {
},
}
w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
printer.DisableStyling()
viewer := NewViewer(&Context{
Cmd: "http",
CIMode: true,
}, NewPrinter(nil, w, w), nil, gbMock)
}, printer, nil, gbMock)

viewer.Output(measurementID1, m)

Expand Down Expand Up @@ -314,10 +322,12 @@ func Test_Output_Default_Ping(t *testing.T) {

m := &globalping.MeasurementCreate{}
w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
printer.DisableStyling()
viewer := NewViewer(&Context{
Cmd: "ping",
CIMode: true,
}, NewPrinter(nil, w, w), nil, gbMock)
}, printer, nil, gbMock)

viewer.Output(measurementID1, m)

Expand Down
12 changes: 3 additions & 9 deletions view/infinite.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (v *viewer) generateTable(hm *HistoryItem, m *globalping.Measurement, areaW
table[i][0] = strings.ReplaceAll(table[i][0], "\t", " ") // Replace tabs with spaces
lines := strings.Split(table[i][0], "\n") // Split first column into lines
color := ColorNone // No color
if i == 0 && !v.ctx.CIMode {
if i == 0 {
color = ColorLightCyan
}
for k := range lines {
Expand Down Expand Up @@ -391,9 +391,6 @@ func computeMdev(tsum float64, tsum2 float64, rcv int, avg float64) float64 {
}

func (v *viewer) getAPICreditInfo() string {
if v.ctx.CIMode {
return apiCreditInfo
}
return v.printer.Color(apiCreditInfo, ColorLightYellow)
}

Expand All @@ -411,10 +408,7 @@ func (v *viewer) getAPICreditConsumptionInfo(width int) string {
if len(info) > width-4 {
info = info[:max(width-5, 0)] + "..."
}
if v.ctx.CIMode {
apiCreditLastConsumptionInfo = info
} else {
apiCreditLastConsumptionInfo = v.printer.Color(info, ColorLightYellow)
}

apiCreditLastConsumptionInfo = v.printer.Color(info, ColorLightYellow)
return apiCreditLastConsumptionInfo
}
49 changes: 31 additions & 18 deletions view/infinite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ func Test_OutputInfinite_SingleProbe_InProgress(t *testing.T) {
timeMock.EXPECT().Now().Return(defaultCurrentTime.Add(500 * time.Millisecond)).Times(3)

ctx := createDefaultContext("ping")
ctx.CIMode = true
hm := ctx.History.Find(measurementID1)
w := new(bytes.Buffer)
viewer := NewViewer(ctx, NewPrinter(nil, w, w), timeMock, nil)
printer := NewPrinter(nil, w, w)
printer.DisableStyling()
viewer := NewViewer(ctx, printer, timeMock, nil)

measurement := createPingMeasurement(measurementID1)
measurement.Status = globalping.StatusInProgress
Expand Down Expand Up @@ -141,9 +142,10 @@ func Test_OutputInfinite_SingleProbe_Failed(t *testing.T) {
measurement.Results[0].Result.RawOutput = `ping: cdn.jsdelivr.net.xc: Name or service not known`

ctx := createDefaultContext("ping")
ctx.CIMode = true
w := new(bytes.Buffer)
viewer := NewViewer(ctx, NewPrinter(nil, w, w), nil, nil)
printer := NewPrinter(nil, w, w)
printer.DisableStyling()
viewer := NewViewer(ctx, printer, nil, nil)
err := viewer.OutputInfinite(measurement)
assert.Equal(t, "all probes failed", err.Error())

Expand All @@ -170,9 +172,10 @@ func Test_OutputInfinite_MultipleProbes_MultipleCalls(t *testing.T) {
measurement.Results[0].Result.RawOutput = `PING (146.75.73.229) 56(84) bytes of data.`

ctx := createDefaultContext("ping")
ctx.CIMode = true
w := new(bytes.Buffer)
viewer := NewViewer(ctx, NewPrinter(nil, w, w), timeMock, nil)
printer := NewPrinter(nil, w, w)
printer.DisableStyling()
viewer := NewViewer(ctx, printer, timeMock, nil)

// Call 1
expectedOutput := `Location | Sent | Loss | Last | Min | Avg | Max
Expand Down Expand Up @@ -292,9 +295,10 @@ func Test_OutputInfinite_MultipleProbes_MultipleConcurrentCalls(t *testing.T) {
ctx := createDefaultContext("ping")
hm1 := ctx.History.Find(measurementID1)
hm1.Status = globalping.StatusInProgress
ctx.CIMode = true
w := new(bytes.Buffer)
viewer := NewViewer(ctx, NewPrinter(nil, w, w), timeMock, nil)
printer := NewPrinter(nil, w, w)
printer.DisableStyling()
viewer := NewViewer(ctx, printer, timeMock, nil)

expectedOutput := `Location | Sent | Loss | Last | Min | Avg | Max
London, GB, EU, OVH SAS (AS0) | 1 | 0.00% | 10.0 ms | 10.0 ms | 10.0 ms | 10.0 ms
Expand Down Expand Up @@ -445,9 +449,10 @@ func Test_OutputInfinite_MultipleProbes_All_Failed(t *testing.T) {
}

ctx := createDefaultContext("ping")
ctx.CIMode = true
w := new(bytes.Buffer)
v := NewViewer(ctx, NewPrinter(nil, w, w), nil, nil)
printer := NewPrinter(nil, w, w)
printer.DisableStyling()
v := NewViewer(ctx, printer, nil, nil)
err := v.OutputInfinite(measurement)

assert.Equal(t, "all probes failed", err.Error())
Expand Down Expand Up @@ -479,7 +484,9 @@ func Test_GenerateTable_Full(t *testing.T) {
NewMeasurementStats(),
}
hm := ctx.History.Find(measurementID1)
viewer := &viewer{ctx: ctx}
w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
viewer := &viewer{ctx: ctx, printer: printer}
measurement := createPingMeasurement_MultipleProbes(measurementID1)
table, _, stats := viewer.generateTable(hm, measurement, 500)

Expand All @@ -488,15 +495,15 @@ func Test_GenerateTable_Full(t *testing.T) {
"Falkenstein, DE, EU, Hetzner Online GmbH (AS0) | 1 | 0.00% | 5.46 ms | 5.46 ms | 5.46 ms | 5.46 ms\n" +
"Nuremberg, DE, EU, Hetzner Online GmbH (AS0) | 1 | 0.00% | 4.07 ms | 4.07 ms | 4.07 ms | 4.07 ms\n"
assert.Equal(t, expectedTable, *table)

assert.Equal(t, "", w.String())
assert.Equal(t, []*MeasurementStats{
{Sent: 1, Rcv: 1, Lost: 0, Loss: 0, Last: 0.77, Min: 0.77, Avg: 0.77, Max: 0.77, Time: 100, Tsum: 0.77, Tsum2: 0.5929},
{Sent: 1, Rcv: 1, Lost: 0, Loss: 0, Last: 5.46, Min: 5.46, Avg: 5.46, Max: 5.46, Time: 200, Tsum: 5.46, Tsum2: 29.8116},
{Sent: 1, Rcv: 1, Lost: 0, Loss: 0, Last: 4.07, Min: 4.07, Avg: 4.07, Max: 4.07, Time: 300, Tsum: 4.07, Tsum2: 16.5649},
}, stats)
}

func Test_GenerateTable_CIMode(t *testing.T) {
func Test_GenerateTable_StylingDisabled(t *testing.T) {
ctx := createDefaultContext("ping")
ctx.CIMode = true
ctx.AggregatedStats = []*MeasurementStats{
Expand All @@ -505,7 +512,10 @@ func Test_GenerateTable_CIMode(t *testing.T) {
NewMeasurementStats(),
}
hm := ctx.History.Find(measurementID1)
viewer := &viewer{ctx: ctx}
w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
printer.DisableStyling()
viewer := &viewer{ctx: ctx, printer: printer}

measurement := createPingMeasurement_MultipleProbes(measurementID1)
table, _, stats := viewer.generateTable(hm, measurement, 500)
Expand All @@ -516,7 +526,7 @@ Falkenstein, DE, EU, Hetzner Online GmbH (AS0) | 1 | 0.00% | 5.46 ms | 5.
Nuremberg, DE, EU, Hetzner Online GmbH (AS0) | 1 | 0.00% | 4.07 ms | 4.07 ms | 4.07 ms | 4.07 ms
`
assert.Equal(t, expectedTable, *table)

assert.Equal(t, "", w.String())
assert.Equal(t, []*MeasurementStats{
{Sent: 1, Rcv: 1, Lost: 0, Loss: 0, Last: 0.77, Min: 0.77, Avg: 0.77, Max: 0.77, Time: 100, Tsum: 0.77, Tsum2: 0.5929},
{Sent: 1, Rcv: 1, Lost: 0, Loss: 0, Last: 5.46, Min: 5.46, Avg: 5.46, Max: 5.46, Time: 200, Tsum: 5.46, Tsum2: 29.8116},
Expand All @@ -532,7 +542,8 @@ func Test_GenerateTable_OneRow_Truncated(t *testing.T) {
NewMeasurementStats(),
}
hm := ctx.History.Find(measurementID1)
viewer := &viewer{ctx: ctx}
printer := NewPrinter(nil, nil, nil)
viewer := &viewer{ctx: ctx, printer: printer}

measurement := createPingMeasurement_MultipleProbes(measurementID1)
measurement.Results[1].Probe.Network = "作者聚集的原创内容平台于201 1年1月正式上线让人们更"
Expand All @@ -559,7 +570,8 @@ func Test_GenerateTable_MultiLine_Truncated(t *testing.T) {
NewMeasurementStats(),
}
hm := ctx.History.Find(measurementID1)
viewer := &viewer{ctx: ctx}
printer := NewPrinter(nil, nil, nil)
viewer := &viewer{ctx: ctx, printer: printer}

measurement := createPingMeasurement_MultipleProbes(measurementID1)
measurement.Results[1].Probe.Network = "Hetzner Online GmbH\nLorem ipsum\nLorem ipsum dolor sit amet"
Expand Down Expand Up @@ -588,7 +600,8 @@ func Test_GenerateTable_MaxTruncated(t *testing.T) {
NewMeasurementStats(),
}
hm := ctx.History.Find(measurementID1)
viewer := &viewer{ctx: ctx}
printer := NewPrinter(nil, nil, nil)
viewer := &viewer{ctx: ctx, printer: printer}

measurement := createPingMeasurement_MultipleProbes(measurementID1)
table, _, stats := viewer.generateTable(hm, measurement, 0)
Expand Down
5 changes: 3 additions & 2 deletions view/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ func Test_Output_Json(t *testing.T) {
gbMock.EXPECT().GetMeasurementRaw(measurementID1).Times(1).Return(b, nil)

w := new(bytes.Buffer)
printer := NewPrinter(nil, w, w)
printer.DisableStyling()
viewer := NewViewer(
&Context{
ToJSON: true,
Share: true,
CIMode: true,
},
NewPrinter(nil, w, w),
printer,
nil,
gbMock,
)
Expand Down
6 changes: 1 addition & 5 deletions view/latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,5 @@ func (v *viewer) OutputLatency(id string, data *globalping.Measurement) error {
}

func (v *viewer) latencyStatHeader(title string) string {
text := fmt.Sprintf("%s: ", title)
if v.ctx.CIMode {
return text
}
return v.printer.Bold(text)
return v.printer.Bold(fmt.Sprintf("%s: ", title))
}
Loading

0 comments on commit d2423b3

Please sign in to comment.