Skip to content

Commit

Permalink
✨ feat: Add TrimAnsi method to remove possible Ansi encoding in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaha committed Apr 9, 2024
1 parent ade56e8 commit 31c80f6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions zlog/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,8 @@ func init() {
func isSupportColor() bool {
return !DisableColor && IsSupportColor()
}

func TrimAnsi(str string) string {
str, _ = zstring.RegexReplace(`\x1b\[[0-9;]*[a-zA-Z]`, str, "")
return str
}
10 changes: 10 additions & 0 deletions zlog/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ func TestColor(t *testing.T) {
OutAllColor()
T.Equal(testText, ColorTextWrap(ColorGreen, testText))
}

func TestTrimAnsi(t *testing.T) {
tt := zls.NewTest(t)

testText := "ok\x1b[31m"
tt.Equal("ok", TrimAnsi(testText))

testText = ColorTextWrap(ColorGreen, "ok")
tt.Equal("ok", TrimAnsi(testText))
}

0 comments on commit 31c80f6

Please sign in to comment.