-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change alert parts calculations (#1084)
- Loading branch information
1 parent
6541929
commit ae27338
Showing
10 changed files
with
738 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package msgformat | ||
|
||
import "unicode/utf8" | ||
|
||
// DefaultDescriptionCutter cuts description, so len(newDesc) <= maxSize. Ensure that len(desc) >= maxSize and | ||
// maxSize >= len("...\n"). | ||
func DefaultDescriptionCutter(desc string, maxSize int) string { | ||
suffix := "...\n" | ||
return desc[:maxSize-len(suffix)] + suffix | ||
} | ||
|
||
var bracketsLen = utf8.RuneCountInString("[]") | ||
|
||
// DefaultTagsLimiter cuts and formats tags to fit maxSize. There will be no tag parts, for example: | ||
// | ||
// if we have | ||
// | ||
// tags = []string{"tag1", "tag2} | ||
// maxSize = 8 | ||
// | ||
// so call DefaultTagsLimiter(tags, maxSize) will return " [tag1]". | ||
func DefaultTagsLimiter(tags []string, maxSize int) string { | ||
tagsStr := " " | ||
lenTagsStr := utf8.RuneCountInString(tagsStr) | ||
|
||
for i := range tags { | ||
lenTag := utf8.RuneCountInString(tags[i]) + bracketsLen | ||
|
||
if lenTagsStr+lenTag > maxSize { | ||
break | ||
} | ||
|
||
tagsStr += "[" + tags[i] + "]" | ||
lenTagsStr += lenTag | ||
} | ||
|
||
if tagsStr == " " { | ||
return "" | ||
} | ||
|
||
return tagsStr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package msgformat | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestDefaultTagsLimiter(t *testing.T) { | ||
Convey("Test default tags limiter", t, func() { | ||
tags := []string{"tag1", "tag2"} | ||
|
||
Convey("with maxSize < 0", func() { | ||
tagsStr := DefaultTagsLimiter(tags, -1) | ||
|
||
So(tagsStr, ShouldResemble, "") | ||
}) | ||
|
||
Convey("with maxSize > total characters in tags string", func() { | ||
tagsStr := DefaultTagsLimiter(tags, 30) | ||
|
||
So(tagsStr, ShouldResemble, " [tag1][tag2]") | ||
}) | ||
|
||
Convey("with maxSize not enough for all tags", func() { | ||
tagsStr := DefaultTagsLimiter(tags, 8) | ||
|
||
So(tagsStr, ShouldResemble, " [tag1]") | ||
}) | ||
|
||
Convey("with one long tag > maxSize", func() { | ||
tagsStr := DefaultTagsLimiter([]string{"long_tag"}, 4) | ||
|
||
So(tagsStr, ShouldResemble, "") | ||
}) | ||
|
||
Convey("with no tags", func() { | ||
tagsStr := DefaultTagsLimiter([]string{}, 0) | ||
|
||
So(tagsStr, ShouldResemble, "") | ||
}) | ||
}) | ||
} |
Oops, something went wrong.