Skip to content

Commit 9df60ac

Browse files
authored
allow TrueColor Pygments formatters (#102)
when the COLORTERM environment variable contains "truecolor"
1 parent 5f9090e commit 9df60ac

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Version 2.10.0
4+
5+
(released on 2026-02-09)
6+
7+
- Allow TrueColor Pygments formatters, based on the `COLORTERM` env variable.
8+
39
## Version 2.9.0
410

511
(released on 2026-01-26)

cli_helpers/compat.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@
3636
HAS_PYGMENTS = True
3737
try:
3838
from pygments.token import Token
39-
from pygments.formatters.terminal256 import Terminal256Formatter
39+
from pygments.formatters.terminal256 import (
40+
TerminalTrueColorFormatter,
41+
Terminal256Formatter,
42+
)
4043
except ImportError:
4144
HAS_PYGMENTS = False
4245
Terminal256Formatter = None
46+
TerminalTrueColorFormatter = None
4347
Token = SimpleNamespace()
4448
Token.Output = SimpleNamespace()
4549
Token.Output.Header = None

cli_helpers/tabular_output/tabulate_adapter.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33

44
from __future__ import unicode_literals
55

6+
import os
7+
68
from cli_helpers.utils import filter_dict_by_key
7-
from cli_helpers.compat import Terminal256Formatter, Token, StringIO
9+
from cli_helpers.compat import (
10+
Terminal256Formatter,
11+
TerminalTrueColorFormatter,
12+
Token,
13+
StringIO,
14+
)
815
from .preprocessors import (
916
convert_to_string,
1017
truncate_string,
@@ -193,7 +200,10 @@ class YourStyle(Style):
193200
194201
"""
195202
if style and HAS_PYGMENTS and format_name in supported_table_formats:
196-
formatter = Terminal256Formatter(style=style)
203+
if "truecolor" in os.getenv("COLORTERM", "").lower():
204+
formatter = TerminalTrueColorFormatter(style=style)
205+
else:
206+
formatter = Terminal256Formatter(style=style)
197207

198208
def style_field(token, field):
199209
"""Get the styled text for a *field* using *token* type."""

cli_helpers/utils.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""Various utility functions and helpers."""
33

44
import binascii
5+
import os
56
import re
67
from functools import lru_cache
78
from typing import Dict
@@ -11,7 +12,13 @@
1112
if TYPE_CHECKING:
1213
from pygments.style import StyleMeta
1314

14-
from cli_helpers.compat import binary_type, text_type, Terminal256Formatter, StringIO
15+
from cli_helpers.compat import (
16+
binary_type,
17+
text_type,
18+
Terminal256Formatter,
19+
TerminalTrueColorFormatter,
20+
StringIO,
21+
)
1522

1623

1724
def bytes_to_string(b):
@@ -112,8 +119,11 @@ def replace(s, replace):
112119

113120

114121
@lru_cache()
115-
def _get_formatter(style) -> Terminal256Formatter:
116-
return Terminal256Formatter(style=style)
122+
def _get_formatter(style) -> Terminal256Formatter | TerminalTrueColorFormatter:
123+
if "truecolor" in os.getenv("COLORTERM", "").lower():
124+
return TerminalTrueColorFormatter(style=style)
125+
else:
126+
return Terminal256Formatter(style=style)
117127

118128

119129
def style_field(token, field, style):

tests/tabular_output/test_preprocessors.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from __future__ import unicode_literals
55
from decimal import Decimal
6+
import os
67

78
import pytest
89

@@ -29,6 +30,12 @@
2930
import types
3031

3132

33+
@pytest.fixture
34+
def TwoFiftySixColor():
35+
# todo: finalize this fixture by resetting the env value
36+
os.environ["COLORTERM"] = ""
37+
38+
3239
def test_convert_to_string():
3340
"""Test the convert_to_string() function."""
3441
data = [[1, "John"], [2, "Jill"]]
@@ -60,7 +67,7 @@ def test_override_missing_values():
6067

6168

6269
@pytest.mark.skipif(not HAS_PYGMENTS, reason="requires the Pygments library")
63-
def test_override_missing_value_with_style():
70+
def test_override_missing_value_with_style(TwoFiftySixColor):
6471
"""Test that *override_missing_value()* styles output."""
6572

6673
class NullStyle(Style):
@@ -185,7 +192,7 @@ def test_style_output_no_pygments():
185192

186193

187194
@pytest.mark.skipif(not HAS_PYGMENTS, reason="requires the Pygments library")
188-
def test_style_output():
195+
def test_style_output(TwoFiftySixColor):
189196
"""Test that *style_output()* styles output."""
190197

191198
class CliStyle(Style):
@@ -210,7 +217,7 @@ class CliStyle(Style):
210217

211218

212219
@pytest.mark.skipif(not HAS_PYGMENTS, reason="requires the Pygments library")
213-
def test_style_output_with_newlines():
220+
def test_style_output_with_newlines(TwoFiftySixColor):
214221
"""Test that *style_output()* styles output with newlines in it."""
215222

216223
class CliStyle(Style):
@@ -238,7 +245,7 @@ class CliStyle(Style):
238245

239246

240247
@pytest.mark.skipif(not HAS_PYGMENTS, reason="requires the Pygments library")
241-
def test_style_output_custom_tokens():
248+
def test_style_output_custom_tokens(TwoFiftySixColor):
242249
"""Test that *style_output()* styles output with custom token names."""
243250

244251
class CliStyle(Style):

0 commit comments

Comments
 (0)