From 4d41b093ca2808a9bb7870db67971f1d222c1feb Mon Sep 17 00:00:00 2001 From: resolvicomai Date: Thu, 21 May 2026 22:17:14 -0300 Subject: [PATCH] fix(tui): log image debug (#1316) ## What? - route image protocol debug messages through the standard logger instead of writing directly to stdout - add coverage that verifies debug output goes to the logger and leaves stdout untouched ## Why? Debug output should respect the application logging path instead of bypassing logger formatting and destinations. Fixes #701 --- view/html.go | 3 ++- view/html_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/view/html.go b/view/html.go index 7d76f64c357790ec1da1479320b3a9d51ff020c7..fce56661d361ad530d467a9cc0093effdfafadce 100644 --- a/view/html.go +++ b/view/html.go @@ -14,6 +14,7 @@ import ( "charm.land/lipgloss/v2" "github.com/floatpane/matcha/clib" "github.com/floatpane/matcha/internal/httpclient" + "github.com/floatpane/matcha/internal/loglevel" "github.com/floatpane/matcha/theme" lru "github.com/hashicorp/golang-lru/v2" ) @@ -273,7 +274,7 @@ func debugImageProtocol(format string, args ...interface{}) { return } msg := fmt.Sprintf("[img-protocol] "+format+"\n", args...) - fmt.Print(msg) + loglevel.Infof("%s", strings.TrimSuffix(msg, "\n")) if path := os.Getenv("DEBUG_IMAGE_PROTOCOL_LOG"); path != "" { if f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil { _, _ = f.WriteString(msg) diff --git a/view/html_test.go b/view/html_test.go index f9630f035056395154132979f88399926366e2f0..080e59f046a3977d9bbdf3a2c399b8ea5ef6bdc5 100644 --- a/view/html_test.go +++ b/view/html_test.go @@ -1,7 +1,9 @@ package view import ( + "bytes" "fmt" + "log" "os" "regexp" "strings" @@ -68,6 +70,30 @@ func TestDecodeQuotedPrintable(t *testing.T) { } } +func TestDebugImageProtocolUsesLogger(t *testing.T) { + t.Setenv("DEBUG_IMAGE_PROTOCOL", "1") + t.Setenv("DEBUG_IMAGE_PROTOCOL_LOG", "") + t.Setenv("DEBUG_KITTY_IMAGES", "") + t.Setenv("DEBUG_KITTY_LOG", "") + + var logBuf bytes.Buffer + originalLogOutput := log.Writer() + originalLogFlags := log.Flags() + log.SetOutput(&logBuf) + log.SetFlags(0) + t.Cleanup(func() { + log.SetOutput(originalLogOutput) + log.SetFlags(originalLogFlags) + }) + + debugImageProtocol("hello %s", "world") + + want := "[img-protocol] hello world\n" + if got := logBuf.String(); got != want { + t.Fatalf("debugImageProtocol log output = %q, want %q", got, want) + } +} + func TestMarkdownToHTML(t *testing.T) { testCases := []struct { name string