Skip to content

Commit

Permalink
feat: add user info into the location output (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
radulucut authored Feb 8, 2025
1 parent b066ac7 commit 5f1be53
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
40 changes: 35 additions & 5 deletions view/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,26 @@ func (v *viewer) getProbeInfo(result *globalping.ProbeMeasurement) string {
var output strings.Builder
output.WriteString("> ")
output.WriteString(getLocationText(result))
// Check tags to see if there's a region code
if len(result.Probe.Tags) > 0 {
regionCode := ""
userInfoTags := []string{}
for _, tag := range result.Probe.Tags {
// If tag ends in a number, it's likely a region code and should be displayed
if _, err := strconv.Atoi(tag[len(tag)-1:]); err == nil {
output.WriteString(" (" + tag + ")")
break
if strings.HasPrefix(tag, "u-") && !strings.Contains(tag, ":") {
userInfoTags = append(userInfoTags, tag)
} else if regionCode == "" {
// If tag ends in a number, it's likely a region code and should be displayed
if _, err := strconv.Atoi(tag[len(tag)-1:]); err == nil {
regionCode = tag
}
}
}
userInfo := largestCommonPrefix(userInfoTags)
if userInfo != "" {
output.WriteString(", " + userInfo)
}
if regionCode != "" {
output.WriteString(" (" + regionCode + ")")
}
}
return v.printer.BoldForeground(output.String(), BGYellow)
}
Expand All @@ -157,3 +168,22 @@ func getLocationText(m *globalping.ProbeMeasurement) string {
m.Probe.Network + " " +
"(AS" + fmt.Sprint(m.Probe.ASN) + ")"
}

func largestCommonPrefix(items []string) string {
if len(items) == 0 {
return ""
}
if len(items) == 1 {
return items[0]
}
prefix := items[0]
for i := 1; i < len(items); i++ {
for j := 0; j < len(prefix); j++ {
if j >= len(items[i]) || prefix[j] != items[i][j] {
prefix = prefix[:j]
break
}
}
}
return prefix
}
4 changes: 2 additions & 2 deletions view/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func Test_HeadersTags(t *testing.T) {

assert.Equal(t, "> City (State), Country, Continent, Network (AS12345) (tag1)", v.getProbeInfo(&newResult))

newResult.Probe.Tags = []string{"tag", "tag2"}
assert.Equal(t, "> City (State), Country, Continent, Network (AS12345) (tag2)", v.getProbeInfo(&newResult))
newResult.Probe.Tags = []string{"u-JohnDoe1", "tag2", "u-JohnDoe2"}
assert.Equal(t, "> City (State), Country, Continent, Network (AS12345), u-JohnDoe (tag2)", v.getProbeInfo(&newResult))
}

func Test_TrimOutput(t *testing.T) {
Expand Down

0 comments on commit 5f1be53

Please sign in to comment.