From 186fb54e5d9732475697cb94e5634e9c36d23700 Mon Sep 17 00:00:00 2001 From: Maksym Kryvchun Date: Tue, 25 Jul 2023 16:05:55 +0300 Subject: [PATCH 1/2] fix: color styles after filtering (#22) --- internal/pkg/source/entry.go | 22 +++++++++++++++++++--- internal/pkg/source/helper.go | 4 ++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/internal/pkg/source/entry.go b/internal/pkg/source/entry.go index 8844443..8d1d333 100644 --- a/internal/pkg/source/entry.go +++ b/internal/pkg/source/entry.go @@ -3,6 +3,8 @@ package source import ( "bytes" "encoding/json" + "strings" + "unicode" "github.com/charmbracelet/bubbles/table" "github.com/valyala/fastjson" @@ -79,15 +81,29 @@ func ParseLogEntry(line json.RawMessage) LogEntry { return LogEntry{ Line: line, Time: "-", - Message: string(line), + Message: formatMessage(string(line)), Level: LevelUnknown, } } return LogEntry{ Line: line, - Time: extractTime(value), - Message: extractMessage(value), + Time: formatMessage(extractTime(value)), + Message: formatMessage(extractMessage(value)), Level: extractLevel(value), } } + +func formatMessage(msg string) string { + msg = strings.NewReplacer("\n", "\\n", "\t", "\\t").Replace(msg) + + msg = strings.Map(func(r rune) rune { + if unicode.IsPrint(r) { + return r + } + + return -1 + }, msg) + + return msg +} diff --git a/internal/pkg/source/helper.go b/internal/pkg/source/helper.go index bbfc78e..a7fa8ea 100644 --- a/internal/pkg/source/helper.go +++ b/internal/pkg/source/helper.go @@ -10,7 +10,7 @@ import ( func extractTime(value *fastjson.Value) string { timeValue := extractValue(value, "timestamp", "time", "t") if timeValue != "" { - return strings.TrimSpace(timeValue) + return formatMessage(strings.TrimSpace(timeValue)) } return "-" @@ -19,7 +19,7 @@ func extractTime(value *fastjson.Value) string { func extractLevel(value *fastjson.Value) Level { level := extractValue(value, "level", "lvl") - return ParseLevel(level) + return ParseLevel(formatMessage(level)) } func extractValue(value *fastjson.Value, keys ...string) string { From de6adc807e394eb3ddff407bf39a29e84462f359 Mon Sep 17 00:00:00 2001 From: hedhyw Date: Sat, 5 Aug 2023 07:06:55 +0300 Subject: [PATCH 2/2] test: message_special_rune --- internal/pkg/source/entry_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/pkg/source/entry_test.go b/internal/pkg/source/entry_test.go index 1da1a93..dd8b2ae 100644 --- a/internal/pkg/source/entry_test.go +++ b/internal/pkg/source/entry_test.go @@ -75,6 +75,14 @@ func TestParseLogEntry(t *testing.T) { assert.Equal(t, "msg", logEntry.Message) }, + }, { + Name: "message_special_rune", + JSON: `{"message":"mes` + string(rune(1)) + `sage"}`, + Assert: func(tb testing.TB, logEntry source.LogEntry) { + tb.Helper() + + assert.Equal(t, "message", logEntry.Message) + }, }, { Name: "error", JSON: `{"error":"error"}`,