Skip to content

Commit 54db93d

Browse files
committed
Convert ANSI dim to RGB
1 parent 71548ca commit 54db93d

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616

1717
- Collapsible title now accepts str, Text, or Content https://github.com/Textualize/textual/pull/5697
1818
- Rich Text objects will be converted to Content in OptionList and other widgets https://github.com/Textualize/textual/pull/5712
19+
- Textual will always convert dim attributes to RGB by default
20+
21+
### Added
22+
23+
- Added `TEXTUAL_DIM_FACTOR` env var to set the opacity of the 'dim' ANSI attribute
1924

2025
## [3.0.1] - 2025-04-01
2126

src/textual/constants.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def _get_environ_bool(name: str) -> bool:
2727
return has_environ
2828

2929

30-
def _get_environ_int(name: str, default: int, minimum: int | None = None) -> int:
30+
def _get_environ_int(
31+
name: str, default: int, minimum: int | None = None, maximum: int | None = None
32+
) -> int:
3133
"""Retrieves an integer environment variable.
3234
3335
Args:
@@ -48,6 +50,8 @@ def _get_environ_int(name: str, default: int, minimum: int | None = None) -> int
4850
return default
4951
if minimum is not None:
5052
return max(minimum, value)
53+
if maximum is not None:
54+
return min(maximum, value)
5155
return value
5256

5357

@@ -159,5 +163,10 @@ def _get_textual_animations() -> AnimationLevel:
159163
"""
160164

161165
SMOOTH_SCROLL: Final[bool] = _get_environ_int("TEXTUAL_SMOOTH_SCROLL", 1) == 1
162-
"""Should smooth scrolling be enabled? set `TEXTUAL_SMOOTH_SCROLL=0` to disable smooth
166+
"""Should smooth scrolling be enabled? set `TEXTUAL_SMOOTH_SCROLL=0` to disable smooth scrolling.
163167
"""
168+
169+
DIM_FACTOR: Final[float] = (
170+
_get_environ_int("TEXTUAL_DIM_FACTOR", 50, minimum=0, maximum=100) / 100
171+
)
172+
"""Percentage to use as opacity when converting ANSI 'dim' attribute to RGB."""

src/textual/filter.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from rich.terminal_theme import TerminalTheme
2323

2424
from textual.color import Color
25+
from textual.constants import DIM_FACTOR
2526

2627

2728
class LineFilter(ABC):
@@ -125,7 +126,9 @@ def apply(self, segments: list[Segment], background: Color) -> list[Segment]:
125126

126127

127128
@lru_cache(1024)
128-
def dim_color(background: RichColor, color: RichColor, factor: float) -> RichColor:
129+
def dim_color(
130+
background: RichColor, color: RichColor, factor: float = DIM_FACTOR
131+
) -> RichColor:
129132
"""Dim a color by blending towards the background
130133
131134
Args:
@@ -249,7 +252,7 @@ def truecolor_style(self, style: Style, background: RichColor) -> Style:
249252
)
250253
# Convert dim style to RGB
251254
if style.dim and color is not None:
252-
color = dim_color(background, color, 0.5)
255+
color = dim_color(background, color)
253256

254257
return Style.from_color(color, bgcolor)
255258

0 commit comments

Comments
 (0)