Skip to content

Commit

Permalink
fix: incorrect color on macOS terminal (jsdelivr#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
radulucut authored Jul 23, 2024
1 parent d2423b3 commit 34abab9
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +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
r.printer.Printf(r.printer.Color("> "+e.Message, view.ColorLightYellow) + "\n")
r.printer.Printf(r.printer.Color("> "+e.Message, view.FGBrightYellow) + "\n")
}
}
r.viewer.OutputShare()
Expand Down
6 changes: 3 additions & 3 deletions view/infinite.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (v *viewer) generateTable(hm *HistoryItem, m *globalping.Measurement, areaW
lines := strings.Split(table[i][0], "\n") // Split first column into lines
color := ColorNone // No color
if i == 0 {
color = ColorLightCyan
color = FGBrightCyan
}
for k := range lines {
width := runewidth.StringWidth(lines[k])
Expand Down Expand Up @@ -391,7 +391,7 @@ func computeMdev(tsum float64, tsum2 float64, rcv int, avg float64) float64 {
}

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

func (v *viewer) getAPICreditConsumptionInfo(width int) string {
Expand All @@ -409,6 +409,6 @@ func (v *viewer) getAPICreditConsumptionInfo(width int) string {
info = info[:max(width-5, 0)] + "..."
}

apiCreditLastConsumptionInfo = v.printer.Color(info, ColorLightYellow)
apiCreditLastConsumptionInfo = v.printer.Color(info, FGBrightYellow)
return apiCreditLastConsumptionInfo
}
8 changes: 4 additions & 4 deletions view/latency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ func Test_Output_Latency_Ping(t *testing.T) {
err := viewer.Output(measurementID1, &globalping.MeasurementCreate{})
assert.NoError(t, err)

assert.Equal(t, "\033[1;38;2;23;212;167m> City (State), Country, Continent, Network (AS12345) (tag-1)\033[0m\n"+
assert.Equal(t, "\033[1;38;5;43m> City (State), Country, Continent, Network (AS12345) (tag-1)\033[0m\n"+
"\033[1mMin: \033[0m8.00 ms\n"+
"\033[1mMax: \033[0m20.00 ms\n"+
"\033[1mAvg: \033[0m12.00 ms\n\n"+
"\033[1;38;2;23;212;167m> City B (State B), Country B, Continent B, Network B (AS12349)\033[0m\n"+
"\033[1;38;5;43m> City B (State B), Country B, Continent B, Network B (AS12349)\033[0m\n"+
"\033[1mMin: \033[0m9.00 ms\n"+
"\033[1mMax: \033[0m22.00 ms\n"+
"\033[1mAvg: \033[0m15.00 ms\n\n", w.String())
Expand Down Expand Up @@ -165,7 +165,7 @@ func Test_Output_Latency_DNS(t *testing.T) {
err := viewer.Output(measurementID1, &globalping.MeasurementCreate{})
assert.NoError(t, err)

assert.Equal(t, "\033[1;38;2;23;212;167m> City (State), Country, Continent, Network (AS12345)\033[0m\n"+
assert.Equal(t, "\033[1;38;5;43m> City (State), Country, Continent, Network (AS12345)\033[0m\n"+
"\033[1mTotal: \033[0m44 ms\n\n", w.String())
}

Expand Down Expand Up @@ -257,7 +257,7 @@ func Test_Output_Latency_Http(t *testing.T) {
err := viewer.Output(measurementID1, &globalping.MeasurementCreate{})
assert.NoError(t, err)

assert.Equal(t, "\033[1;38;2;23;212;167m> City (State), Country, Continent, Network (AS12345)\033[0m\n"+
assert.Equal(t, "\033[1;38;5;43m> City (State), Country, Continent, Network (AS12345)\033[0m\n"+
"\033[1mTotal: \033[0m44 ms\n"+
"\033[1mDownload: \033[0m11 ms\n"+
"\033[1mFirst byte: \033[0m20 ms\n"+
Expand Down
4 changes: 2 additions & 2 deletions view/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ func (v *viewer) getProbeInfo(result *globalping.ProbeMeasurement) string {
}
}
}
return v.printer.BoldWithColor(output.String(), ColorHighlight)
return v.printer.BoldForeground(output.String(), BGYellow)
}

func (v *viewer) getShareMessage(id string) string {
return v.printer.BoldWithColor(fmt.Sprintf("> View the results online: %s%s", ShareURL, id), ColorHighlight)
return v.printer.BoldForeground(fmt.Sprintf("> View the results online: %s%s", ShareURL, id), BGYellow)
}

func (v *viewer) isBodyOnlyHttpGet(m *globalping.MeasurementCreate) bool {
Expand Down
2 changes: 1 addition & 1 deletion view/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_HeadersBase(t *testing.T) {
ctx: &Context{},
printer: NewPrinter(nil, nil, nil),
}
assert.Equal(t, "\033[1;38;2;23;212;167m> City (State), Country, Continent, Network (AS12345)\033[0m", v.getProbeInfo(&testResult))
assert.Equal(t, "\033[1;38;5;43m> City (State), Country, Continent, Network (AS12345)\033[0m", v.getProbeInfo(&testResult))
}

func Test_HeadersTags(t *testing.T) {
Expand Down
67 changes: 62 additions & 5 deletions view/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,39 @@ import (
type Color string

const (
ColorNone Color = ""
ColorLightYellow Color = "93"
ColorLightCyan Color = "96"
ColorHighlight Color = "38;2;23;212;167"
ColorNone Color = ""
FGBlack Color = "30"
FGRed Color = "31"
FGGreen Color = "32"
FGYellow Color = "33"
FGBlue Color = "34"
FGMagenta Color = "35"
FGCyan Color = "36"
FGWhite Color = "37"
FGBrightBlack Color = "90"
FGBrightRed Color = "91"
FGBrightGreen Color = "92"
FGBrightYellow Color = "93"
FGBrightBlue Color = "94"
FGBrightMagenta Color = "95"
FGBrightCyan Color = "96"
FGBrightWhite Color = "97"
BGBlack Color = "40"
BGRed Color = "41"
BGGreen Color = "42"
BGYellow Color = "43"
BGBlue Color = "44"
BGMagenta Color = "45"
BGCyan Color = "46"
BGWhite Color = "47"
BGBrightBlack Color = "100"
BGBrightRed Color = "101"
BGBrightGreen Color = "102"
BGBrightYellow Color = "103"
BGBrightBlue Color = "104"
BGBrightMagenta Color = "105"
BGBrightCyan Color = "106"
BGBrightWhite Color = "107"
)

type Printer struct {
Expand Down Expand Up @@ -93,20 +122,48 @@ func (p *Printer) Color(s string, color Color) string {
return fmt.Sprintf("\033[%sm%s\033[0m", color, s)
}

func (p *Printer) ColorForeground(s string, color Color) string {
if p.disableStyling {
return s
}
return fmt.Sprintf("\033[38;5;%sm%s\033[0m", color, s)
}

func (p *Printer) ColorBackground(s string, color Color) string {
if p.disableStyling {
return s
}
return fmt.Sprintf("\033[48;5;%sm%s\033[0m", color, s)
}

func (p *Printer) Bold(s string) string {
if p.disableStyling {
return s
}
return fmt.Sprintf("\033[1m%s\033[0m", s)
}

func (p *Printer) BoldWithColor(s string, color Color) string {
func (p *Printer) BoldColor(s string, color Color) string {
if p.disableStyling {
return s
}
return fmt.Sprintf("\033[1;%sm%s\033[0m", color, s)
}

func (p *Printer) BoldForeground(s string, color Color) string {
if p.disableStyling {
return s
}
return fmt.Sprintf("\033[1;38;5;%sm%s\033[0m", color, s)
}

func (p *Printer) BoldBackground(s string, color Color) string {
if p.disableStyling {
return s
}
return fmt.Sprintf("\033[1;48;5;%sm%s\033[0m", color, s)
}

func (p *Printer) GetSize() (width, height int) {
f, ok := p.OutWriter.(*os.File)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion view/share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Test_OutputShare(t *testing.T) {
viewer := NewViewer(ctx, NewPrinter(nil, w, w), nil, nil)
viewer.OutputShare()

expectedOutput := fmt.Sprintf("\033[1;38;2;23;212;167m> View the results online: https://www.jsdelivr.com/globalping?measurement=%s\033[0m\n", measurementID1)
expectedOutput := fmt.Sprintf("\033[1;38;5;43m> View the results online: https://www.jsdelivr.com/globalping?measurement=%s\033[0m\n", measurementID1)

assert.Equal(t, expectedOutput, w.String())
})
Expand Down

0 comments on commit 34abab9

Please sign in to comment.