log icon indicating copy to clipboard operation
log copied to clipboard

AdaptiveColor not working

Open blob42 opened this issue 8 months ago • 2 comments

Describe the bug

AdaptiveColor always returns the Dark color. Bug only occurs with the log library. Tea and other charm packages respect the adaptive rule.

In my TUI I use both bubbles and log and the log appears always with dark theme.

lipgloss v1.1.0 log v0.4.2 (same issue on v0.4.0) bubbles v0.21.0 bubbletea v1.3.4 termenv v0.16.0

Setup

  • OS Arch
  • Shell fish or bash
  • Terminal Emulator: both Alacritty or Kitty
  • Terminal Multiplexer With or without tmux

To Reproduce

Using log library with tea does not respect lipgloss dark/light rule.

Source Code

var (
	logTextStyle = lipgloss.NewStyle().Foreground(
		lipgloss.AdaptiveColor{Light: "71", Dark: "196"},
	)
)

// Sets the logging into TUI mode.
func SetTUI(output io.Writer) {
	tuiLogStyles := log.DefaultStyles()
	tuiLogStyles.Levels = logLevelStyles

	tuiLogStyles.Prefix = lipgloss.NewStyle().
		Foreground(lipgloss.Color("250"))

	tuiLogStyles.Message = logTextStyle

	tuiLogStyles.Key = lipgloss.NewStyle().
		Foreground(lipgloss.Color("250"))

	tuiLogStyles.Separator = lipgloss.NewStyle().
		Foreground(lipgloss.Color("250"))

	tuiLogStyles.Value = lipgloss.NewStyle().
		Foreground(lipgloss.Color("252"))

	for _, logger := range loggers {
		logger.SetOutput(output)
		//TODO: set custom theme
		// see https://github.com/charmbracelet/log?tab=readme-ov-file#styles
		logger.SetStyles(tuiLogStyles)
		logger.SetColorProfile(termenv.ANSI256)
		logger.SetReportCaller(false)
		logger.SetReportTimestamp(false)
		if logger.GetLevel() < log.InfoLevel {
			logger.SetLevel(log.Level(log.InfoLevel))
		}

	}
}

Expected behavior

  • In this example code I set the dark foreground for the Message part to be in red.
  • When I set the terminal (any of them) to a light background, log messages are only displayed in red color.
  • Expected : Green foreground on white background

Screenshots

Image

Additional context

I am using the log library within a bubbletea TUI. The TUI respects adaptive colors except the log lib

blob42 avatar May 15 '25 13:05 blob42

Hi @blob42, I couldn't reproduce this here on my end. I'm using the following example based on your code.

package main

import (
	"io"
	"os"

	"github.com/charmbracelet/lipgloss"
	"github.com/charmbracelet/log"
	"github.com/muesli/termenv"
)

var logTextStyle = lipgloss.NewStyle().Foreground(
	lipgloss.AdaptiveColor{Light: "71", Dark: "196"},
)

var (
	logLevelStyles = log.DefaultStyles().Levels
	loggers        = []*log.Logger{
		log.Default(),
	}
)

// Sets the logging into TUI mode.
func SetTUI(output io.Writer) {
	tuiLogStyles := log.DefaultStyles()
	tuiLogStyles.Levels = logLevelStyles

	tuiLogStyles.Prefix = lipgloss.NewStyle().
		Foreground(lipgloss.Color("250"))

	tuiLogStyles.Message = logTextStyle

	tuiLogStyles.Key = lipgloss.NewStyle().
		Foreground(lipgloss.Color("250"))

	tuiLogStyles.Separator = lipgloss.NewStyle().
		Foreground(lipgloss.Color("250"))

	tuiLogStyles.Value = lipgloss.NewStyle().
		Foreground(lipgloss.Color("252"))

	for _, logger := range loggers {
		logger.SetOutput(output)
		// TODO: set custom theme
		// see https://github.com/charmbracelet/log?tab=readme-ov-file#styles
		logger.SetStyles(tuiLogStyles)
		logger.SetColorProfile(termenv.ANSI256)
		logger.SetReportCaller(false)
		logger.SetReportTimestamp(false)
		if logger.GetLevel() < log.InfoLevel {
			logger.SetLevel(log.Level(log.InfoLevel))
		}

	}
}

func main() {
	// Example usage
	SetTUI(os.Stderr) // Replace io.Discard with your actual output writer

	// Log messages
	loggers[0].Info("This is an info message")
	loggers[0].Error("This is an error message")
	loggers[0].Debug("This is a debug message") // This won't be shown due to log level
}

Image

aymanbagabas avatar May 16 '25 16:05 aymanbagabas

@aymanbagabas Maybe you misunderstood my case. The Logs are emnedded in a tea app. example screen cast . In this recording I am hard coding the color of logs for the dark background, the light one is ignored.

blob42 avatar May 17 '25 11:05 blob42