diff --git a/view/html.go b/view/html.go index 7d76f64c..fce56661 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 f9630f03..080e59f0 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