From b23b3bc3a87c6199f2f8922ee49eeb2aa128e1c9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 15 May 2026 12:04:43 +0000 Subject: [PATCH 1/4] Initial plan From fef51dac1da048bbab549d856dc953dca476867b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 15 May 2026 12:24:22 +0000 Subject: [PATCH 2/4] Refactor logger namespace colors to use Lipgloss styles Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> --- pkg/logger/logger.go | 63 ++++++++++++++++----------------------- pkg/logger/logger_test.go | 52 +++++++++++--------------------- 2 files changed, 43 insertions(+), 72 deletions(-) diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 540d98fbedd..922e3d0884b 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -8,8 +8,9 @@ import ( "sync" "time" + lipgloss "charm.land/lipgloss/v2" + "github.com/github/gh-aw/pkg/styles" "github.com/github/gh-aw/pkg/timeutil" - "github.com/github/gh-aw/pkg/tty" ) // Logger represents a debug logger for a specific namespace. @@ -29,27 +30,21 @@ var ( // DEBUG_COLORS environment variable to control color output. debugColors = os.Getenv("DEBUG_COLORS") != "0" - // Check if stderr is a terminal (for color support). - isTTY = tty.IsStderrTerminal() - - // Color palette - chosen to be readable on both light and dark backgrounds. - // Using ANSI 256-color codes for better compatibility. - colorPalette = []string{ - "\033[38;5;33m", // Blue - "\033[38;5;35m", // Green - "\033[38;5;166m", // Orange - "\033[38;5;125m", // Purple - "\033[38;5;37m", // Cyan - "\033[38;5;161m", // Magenta - "\033[38;5;136m", // Yellow - "\033[38;5;124m", // Red - "\033[38;5;28m", // Dark green - "\033[38;5;63m", // Light blue - "\033[38;5;95m", // Brown - "\033[38;5;21m", // Dark blue + // Color palette for namespace coloring, using adaptive styles. + colorPalette = []lipgloss.Style{ + lipgloss.NewStyle().Foreground(styles.ColorInfo), + lipgloss.NewStyle().Foreground(styles.ColorSuccess), + lipgloss.NewStyle().Foreground(styles.ColorWarning), + lipgloss.NewStyle().Foreground(styles.ColorPurple), + lipgloss.NewStyle().Foreground(styles.ColorInfo), + lipgloss.NewStyle().Foreground(styles.ColorPurple), + lipgloss.NewStyle().Foreground(styles.ColorYellow), + lipgloss.NewStyle().Foreground(styles.ColorError), + lipgloss.NewStyle().Foreground(styles.ColorSuccess), + lipgloss.NewStyle().Foreground(styles.ColorInfo), + lipgloss.NewStyle().Foreground(styles.ColorWarning), + lipgloss.NewStyle().Foreground(styles.ColorInfo), } - - colorReset = "\033[0m" ) // initDebugEnv resolves the effective debug pattern. @@ -74,7 +69,7 @@ func initDebugEnv() string { // DEBUG=ns1,ns2 - enables specific namespaces // DEBUG=ns:*,-ns:skip - enables namespace but excludes specific patterns // -// Colors are automatically assigned to each namespace if DEBUG_COLORS != "0" and stderr is a TTY. +// Colors are automatically assigned to each namespace if DEBUG_COLORS != "0". func New(namespace string) *Logger { enabled := computeEnabled(namespace) color := selectColor(namespace) @@ -88,21 +83,21 @@ func New(namespace string) *Logger { // selectColor selects a color for the namespace based on its hash. func selectColor(namespace string) string { - if !debugColors || !isTTY { - return "" + if !debugColors { + return namespace } // Use FNV-1a hash for consistent color assignment h := fnv.New32a() // hash.Hash.Write never returns an error in practice, but check to satisfy gosec G104 if _, err := h.Write([]byte(namespace)); err != nil { - // Return empty string (no color) if write somehow fails - return "" + // Return plain namespace (no color) if write somehow fails + return namespace } hash := h.Sum32() - // Select color from palette based on hash - return colorPalette[hash%uint32(len(colorPalette))] + // Select color from palette based on hash and render namespace with it. + return colorPalette[hash%uint32(len(colorPalette))].Render(namespace) } // Enabled returns whether this logger is enabled @@ -124,11 +119,7 @@ func (l *Logger) Printf(format string, args ...any) { l.mu.Unlock() message := fmt.Sprintf(format, args...) - if l.color != "" { - fmt.Fprintf(os.Stderr, "%s%s%s %s +%s\n", l.color, l.namespace, colorReset, message, timeutil.FormatDuration(diff)) - } else { - fmt.Fprintf(os.Stderr, "%s %s +%s\n", l.namespace, message, timeutil.FormatDuration(diff)) - } + lipgloss.Fprintf(os.Stderr, "%s %s +%s\n", l.color, message, timeutil.FormatDuration(diff)) } // Print prints a message if the logger is enabled. @@ -145,11 +136,7 @@ func (l *Logger) Print(args ...any) { l.mu.Unlock() message := fmt.Sprint(args...) - if l.color != "" { - fmt.Fprintf(os.Stderr, "%s%s%s %s +%s\n", l.color, l.namespace, colorReset, message, timeutil.FormatDuration(diff)) - } else { - fmt.Fprintf(os.Stderr, "%s %s +%s\n", l.namespace, message, timeutil.FormatDuration(diff)) - } + lipgloss.Fprintf(os.Stderr, "%s %s +%s\n", l.color, message, timeutil.FormatDuration(diff)) } // computeEnabled computes whether a namespace matches the DEBUG patterns diff --git a/pkg/logger/logger_test.go b/pkg/logger/logger_test.go index daed2a62fc9..e7f29d4fdeb 100644 --- a/pkg/logger/logger_test.go +++ b/pkg/logger/logger_test.go @@ -5,7 +5,6 @@ package logger import ( "bytes" "os" - "slices" "strings" "testing" "time" @@ -284,51 +283,36 @@ func TestColorSelection(t *testing.T) { if color1 != color2 { t.Errorf("selectColor should return same color for same namespace") } - - // Test that different namespaces can get different colors - // (not guaranteed but likely with our hash function) - color3 := selectColor("other:namespace") - // Just verify it's a valid color from palette or empty - found := color3 == "" - if slices.Contains(colorPalette, color3) { - found = true + if color1 == "" { + t.Error("selectColor should return non-empty namespace label") } - if !found { - t.Errorf("selectColor returned invalid color: %q", color3) + + // When colors are disabled, selectColor should return plain namespace text. + origDebugColors := debugColors + debugColors = false + t.Cleanup(func() { debugColors = origDebugColors }) + if got := selectColor("plain:namespace"); got != "plain:namespace" { + t.Errorf("selectColor should return plain namespace when debugColors=false, got %q", got) } } -func TestColorDisabling(t *testing.T) { +func TestNoColorEnvironment(t *testing.T) { // Save original values origDebugColors := debugColors - origIsTTY := isTTY defer func() { debugColors = origDebugColors - isTTY = origIsTTY }() - // Test with colors disabled via DEBUG_COLORS - debugColors = false - isTTY = true - color := selectColor("test:namespace") - if color != "" { - t.Errorf("selectColor should return empty when debugColors=false, got %q", color) - } - - // Test with TTY disabled + debugEnv = "*" debugColors = true - isTTY = false - color = selectColor("test:namespace") - if color != "" { - t.Errorf("selectColor should return empty when isTTY=false, got %q", color) - } + t.Setenv("NO_COLOR", "1") - // Test with both enabled - debugColors = true - isTTY = true - color = selectColor("test:namespace") - if color == "" { - t.Error("selectColor should return color when both enabled") + logger := New("test:namespace") + output := captureStderr(func() { + logger.Printf("hello") + }) + if strings.Contains(output, "\x1b[") { + t.Errorf("logger output should not contain ANSI color escapes when NO_COLOR is set, got %q", output) } } From e58d5d107ffe92d55dda9b371e819a2484fadd95 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 15 May 2026 12:34:04 +0000 Subject: [PATCH 3/4] Improve logger color palette diversity and test assertions Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> --- pkg/logger/logger.go | 12 ++++++------ pkg/logger/logger_test.go | 6 ++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 922e3d0884b..b38a26e38b6 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -36,14 +36,14 @@ var ( lipgloss.NewStyle().Foreground(styles.ColorSuccess), lipgloss.NewStyle().Foreground(styles.ColorWarning), lipgloss.NewStyle().Foreground(styles.ColorPurple), - lipgloss.NewStyle().Foreground(styles.ColorInfo), - lipgloss.NewStyle().Foreground(styles.ColorPurple), lipgloss.NewStyle().Foreground(styles.ColorYellow), lipgloss.NewStyle().Foreground(styles.ColorError), - lipgloss.NewStyle().Foreground(styles.ColorSuccess), - lipgloss.NewStyle().Foreground(styles.ColorInfo), - lipgloss.NewStyle().Foreground(styles.ColorWarning), - lipgloss.NewStyle().Foreground(styles.ColorInfo), + lipgloss.NewStyle().Foreground(styles.ColorComment), + lipgloss.NewStyle().Foreground(styles.ColorForeground), + lipgloss.NewStyle().Foreground(styles.ColorBorder), + lipgloss.NewStyle().Foreground(styles.ColorBackground), + lipgloss.NewStyle().Foreground(styles.ColorTableAltRow), + lipgloss.NewStyle().Foreground(lipgloss.Color("33")), } ) diff --git a/pkg/logger/logger_test.go b/pkg/logger/logger_test.go index e7f29d4fdeb..5db37a0365b 100644 --- a/pkg/logger/logger_test.go +++ b/pkg/logger/logger_test.go @@ -286,6 +286,12 @@ func TestColorSelection(t *testing.T) { if color1 == "" { t.Error("selectColor should return non-empty namespace label") } + if !strings.Contains(color1, "test:namespace") { + t.Errorf("selectColor should include namespace text, got %q", color1) + } + if !strings.Contains(color1, "\x1b[") { + t.Errorf("selectColor should include ANSI styling when colors are enabled, got %q", color1) + } // When colors are disabled, selectColor should return plain namespace text. origDebugColors := debugColors From 1b3f80df02d7ccca3a21479b3bc8880c07ae6c42 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 15 May 2026 13:27:44 +0000 Subject: [PATCH 4/4] Address logger review feedback on palette and naming Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> --- pkg/logger/logger.go | 20 ++++++++++---------- pkg/logger/logger_test.go | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index b38a26e38b6..d34cd584bb0 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -19,7 +19,7 @@ type Logger struct { enabled bool lastLog time.Time mu sync.Mutex - color string + label string } var ( @@ -41,9 +41,9 @@ var ( lipgloss.NewStyle().Foreground(styles.ColorComment), lipgloss.NewStyle().Foreground(styles.ColorForeground), lipgloss.NewStyle().Foreground(styles.ColorBorder), - lipgloss.NewStyle().Foreground(styles.ColorBackground), - lipgloss.NewStyle().Foreground(styles.ColorTableAltRow), - lipgloss.NewStyle().Foreground(lipgloss.Color("33")), + lipgloss.NewStyle().Foreground(styles.ColorInfo), + lipgloss.NewStyle().Foreground(styles.ColorSuccess), + lipgloss.NewStyle().Foreground(styles.ColorPurple), } ) @@ -72,17 +72,17 @@ func initDebugEnv() string { // Colors are automatically assigned to each namespace if DEBUG_COLORS != "0". func New(namespace string) *Logger { enabled := computeEnabled(namespace) - color := selectColor(namespace) + label := selectNamespaceLabel(namespace) return &Logger{ namespace: namespace, enabled: enabled, lastLog: time.Now(), - color: color, + label: label, } } -// selectColor selects a color for the namespace based on its hash. -func selectColor(namespace string) string { +// selectNamespaceLabel renders the namespace label with a hash-selected style. +func selectNamespaceLabel(namespace string) string { if !debugColors { return namespace } @@ -119,7 +119,7 @@ func (l *Logger) Printf(format string, args ...any) { l.mu.Unlock() message := fmt.Sprintf(format, args...) - lipgloss.Fprintf(os.Stderr, "%s %s +%s\n", l.color, message, timeutil.FormatDuration(diff)) + lipgloss.Fprintf(os.Stderr, "%s %s +%s\n", l.label, message, timeutil.FormatDuration(diff)) } // Print prints a message if the logger is enabled. @@ -136,7 +136,7 @@ func (l *Logger) Print(args ...any) { l.mu.Unlock() message := fmt.Sprint(args...) - lipgloss.Fprintf(os.Stderr, "%s %s +%s\n", l.color, message, timeutil.FormatDuration(diff)) + lipgloss.Fprintf(os.Stderr, "%s %s +%s\n", l.label, message, timeutil.FormatDuration(diff)) } // computeEnabled computes whether a namespace matches the DEBUG patterns diff --git a/pkg/logger/logger_test.go b/pkg/logger/logger_test.go index 5db37a0365b..3a6f1d47386 100644 --- a/pkg/logger/logger_test.go +++ b/pkg/logger/logger_test.go @@ -277,28 +277,28 @@ func TestLogger_TimeDiff(t *testing.T) { } func TestColorSelection(t *testing.T) { - // Test that selectColor returns consistent colors for the same namespace - color1 := selectColor("test:namespace") - color2 := selectColor("test:namespace") + // Test that selectNamespaceLabel returns consistent colors for the same namespace + color1 := selectNamespaceLabel("test:namespace") + color2 := selectNamespaceLabel("test:namespace") if color1 != color2 { - t.Errorf("selectColor should return same color for same namespace") + t.Errorf("selectNamespaceLabel should return same color for same namespace") } if color1 == "" { - t.Error("selectColor should return non-empty namespace label") + t.Error("selectNamespaceLabel should return non-empty namespace label") } if !strings.Contains(color1, "test:namespace") { - t.Errorf("selectColor should include namespace text, got %q", color1) + t.Errorf("selectNamespaceLabel should include namespace text, got %q", color1) } if !strings.Contains(color1, "\x1b[") { - t.Errorf("selectColor should include ANSI styling when colors are enabled, got %q", color1) + t.Errorf("selectNamespaceLabel should include ANSI styling when colors are enabled, got %q", color1) } - // When colors are disabled, selectColor should return plain namespace text. + // When colors are disabled, selectNamespaceLabel should return plain namespace text. origDebugColors := debugColors debugColors = false t.Cleanup(func() { debugColors = origDebugColors }) - if got := selectColor("plain:namespace"); got != "plain:namespace" { - t.Errorf("selectColor should return plain namespace when debugColors=false, got %q", got) + if got := selectNamespaceLabel("plain:namespace"); got != "plain:namespace" { + t.Errorf("selectNamespaceLabel should return plain namespace when debugColors=false, got %q", got) } }