bat icon indicating copy to clipboard operation
bat copied to clipboard

Theme based on terminal colors doesn't work on Tmux

Open YaFou opened this issue 9 months ago • 7 comments
trafficstars

What steps will reproduce the bug?

  1. Use the following configuration:
--theme-dark="[dark theme]"
--theme-light="[light theme]"

(I use Catppuccin)

  1. Launch a Tmux session
  2. Run bat file. The theme should be the correct one.
  3. Change your terminal theme.
  4. Run once again bat file. The theme should be wrong.

What happens? The theme doesn't switch properly.

What did you expect to happen instead? The theme should switch when I switch to dark or light mode.

How did you install bat?

Using the Arch package repository


bat version and environment

Software version

bat 0.25.0 (v0.25.0-modified)

Operating system

Linux 6.12.10-arch1-1

Command-line

bat .zshrc --diagnostic

Environment variables

BAT_CACHE_PATH=<not set>
BAT_CONFIG_PATH=<not set>
BAT_OPTS=<not set>
BAT_PAGER=<not set>
BAT_PAGING=<not set>
BAT_STYLE=<not set>
BAT_TABS=<not set>
BAT_THEME=<not set>
COLORTERM=truecolor
LANG=fr_FR.UTF-8
LC_ALL=<not set>
LESS=<not set>
MANPAGER='sh -c '\''col -bx | bat -l man -p'\'''
NO_COLOR=<not set>
PAGER=<not set>
SHELL=/usr/bin/zsh
TERM=tmux-256color
XDG_CACHE_HOME=<not set>
XDG_CONFIG_HOME=<not set>

System Config file

Could not read contents of '/etc/bat/config': No such file or directory (os error 2).

Config file

--theme-dark="Catppuccin Mocha"
--theme-light="Catppuccin Latte"

Custom assets metadata

bat_version: 0.25.0
creation_time:
  secs_since_epoch: 1737367908
  nanos_since_epoch: 307702877

Custom assets

  • metadata.yaml, 97 bytes
  • syntaxes.bin, 969578 bytes
  • themes.bin, 49134 bytes

Compile time information

  • Profile: release
  • Target triple: x86_64-unknown-linux-gnu
  • Family: unix
  • OS: linux
  • Architecture: x86_64
  • Pointer width: 64
  • Endian: little
  • CPU features: fxsr,sse,sse2
  • Host: x86_64-unknown-linux-gnu

Less version

> less --version
less 668 (PCRE2 regular expressions)
Copyright (C) 1984-2024  Mark Nudelman

less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Home page: https://greenwoodsoftware.com/less

YaFou avatar Feb 10 '25 19:02 YaFou

If you want the colors to change after bat has already passed it's output to the pager, I guess we would need a built-in pager like mentioned at #1053

keith-hall avatar Feb 10 '25 19:02 keith-hall

If you want the colors to change after bat has already passed it's output to the pager, I guess we would need a built-in pager like mentioned at #1053

I should have been more precise. I have updated the steps to reproduce the bug.

YaFou avatar Feb 10 '25 21:02 YaFou

What tmux version are you using, and do you have any pane styles set?

If I recall correctly, the older versions didn't support background color querying at all, while the newer tmux versions respond directly to the query instead of going through the terminal.

eth-p avatar Feb 10 '25 21:02 eth-p

What tmux version are you using, and do you have any pane styles set?

If I recall correctly, the older versions didn't support background color querying at all, while the newer tmux versions respond directly to the query instead of going through the terminal.

My Tmux version is 3.5a.

My theme is:

source-file ~/.local/share/tmux/theme.conf
set -g default-terminal "tmux-256color"

# Status
set -g status-left ""
set -g status-right "#[fg=#{@theme_mauve}]◖#[fg=#{@theme_base},bg=#{@theme_mauve}] #[fg=#{@theme_text},bg=#{@theme_surface_0}] #S#[fg=#{@theme_surface_0},bg=#{@theme_base}]◗"
set -g status-style "bg=#{@theme_base},fg=#{@theme_text}"

# Window
set -g window-status-format "#[fg=#{@theme_rosewater}]◖#[fg=#{@theme_base},bg=#{@theme_rosewater}]#I #[fg=#{@theme_text},bg=#{@theme_surface_0}] #W#[fg=#{@theme_surface_0},bg=#{@theme_base}]◗"
set -g window-status-current-format "#[fg=#{@theme_green}]◖#[fg=#{@theme_base},bg=#{@theme_green}]#I #[fg=#{@theme_text},bg=#{@theme_surface_0}] #W#[fg=#{@theme_surface_0},bg=#{@theme_base}]◗"

# Message
set -g message-style "fg=#{@theme_text},bg=#{@theme_surface_0}"
set -g message-command-style "fg=#{@theme_text},bg=#{@theme_surface_2}"

# Menu
set -g menu-selected-style "bg=#{@theme_green},fg=#{@theme_base}"
set -g menu-style "bg=#{@theme_green},fg=#{@theme_base}"

where ~/.local/share/tmux/theme.conf is updated according to the system theme. I'm using catppuccin-latte et catppuccin-mocha from this repo.

YaFou avatar Feb 12 '25 22:02 YaFou

I can reproduce this problem as well, on the same bat 0.25.0 (with no configuration), and tmux 3.5a (with oh-my-tmux config) installed with homebrew on macOS 15.3.

bat works fine in my terminal (ghostty), but is stuck on the active theme (light or dark) when the tmux session is launched.

shanscendent avatar Feb 13 '25 08:02 shanscendent

This might be related to this issue.

YaFou avatar Feb 13 '25 09:02 YaFou

I was able to reproduce the behavior, and the upstream tmux issue you mentioned appears to be the underlying cause. Using tmux 3.5a, it will return the original terminal background color when queried, but only until I resize the terminal window. As the linked issue describes, the fix they implemented works by invalidating the cached color when a SIGWINCH signal is sent by the terminal. Unfortunately, it seems that not all of the terminal emulators will do that when the background color is changed, though.

Ideally, one of two things would fix this:

  • tmux adds another way to invalidate the cached background color; or
  • more terminal emulators are updated to send SIGWINCH when the background color is changed.

Another option would be to make a pull request for terminal-colorsaurus to handle tmux as a special case. If the TMUX environment variable is set, the OSC 11 query could use the tmux passthrough escape sequence to query the terminal directly. This comes with the downside that it would no longer detect the right background color when tmux has the window-style bg=#...... option set, though.

In the meantime, I can think of a couple of workarounds that can be used until a better solution is implemented:

  • Resize the terminal window to force tmux to re-check the background color.
  • Detach and reattach to the tmux session.
  • Figure out a way to also run tmux set-option window-style bg=#rrggbb when you change the terminal background color.

eth-p avatar Feb 14 '25 05:02 eth-p