Skip to content

Commit 60ccdbd

Browse files
committed
feat(theme): integrate Theme with Rich's Theme system (Phase 0 task 4)
- Add Theme.to_rich_theme() method to convert StyledConsole themes to Rich themes - Pass Rich theme to RichConsole for markup support - Users can now use Rich markup like [success], [error], [primary] with themes - Fix: Change default muted color from "gray" to "bright_black" (Rich compatibility) Example: console = Console(theme=THEMES.DARK) console.print("[success]Done![/]") # Now works with theme colors!
1 parent f86c73c commit 60ccdbd

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/styledconsole/console.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,18 @@ def __init__(
162162
# This ensures colors work in non-TTY environments when policy.color=True
163163
force_terminal = self._terminal.should_force_terminal() or self._policy.color
164164

165+
# Convert our theme to Rich theme for markup support
166+
# This enables Rich markup like [success], [error], [primary] to work
167+
rich_theme = self._theme.to_rich_theme()
168+
165169
# Initialize Rich console with terminal settings
166170
self._rich_console = RichConsole(
167171
record=record,
168172
width=width,
169173
file=file or sys.stdout,
170174
force_terminal=force_terminal,
171175
color_system=color_system,
176+
theme=rich_theme,
172177
)
173178

174179
# Initialize rendering engine (handles frame, banner, text, rule, newline)

src/styledconsole/core/theme.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
from __future__ import annotations
1717

1818
from dataclasses import dataclass
19+
from typing import TYPE_CHECKING
1920

2021
from styledconsole.core.registry import Registry
2122

23+
if TYPE_CHECKING:
24+
from rich.theme import Theme as RichTheme
25+
2226

2327
@dataclass(frozen=True)
2428
class GradientSpec:
@@ -89,7 +93,7 @@ class Theme:
8993
info: str = "blue"
9094
border: str = "white"
9195
text: str = "white"
92-
muted: str = "gray"
96+
muted: str = "bright_black" # Rich uses "grey" not "gray", bright_black is safe
9397
background: str = "black"
9498

9599
# Gradient specifications (optional)
@@ -136,6 +140,36 @@ def has_gradients(self) -> bool:
136140
"""Check if this theme has any gradient definitions."""
137141
return any([self.border_gradient, self.text_gradient, self.banner_gradient])
138142

143+
def to_rich_theme(self) -> RichTheme:
144+
"""Convert this Theme to a Rich Theme for markup support.
145+
146+
This enables Rich markup like [success], [error], [primary] to work
147+
with StyledConsole theme colors.
148+
149+
Returns:
150+
A Rich Theme instance with style definitions for semantic colors.
151+
152+
Example:
153+
>>> from styledconsole import Console, THEMES
154+
>>> console = Console(theme=THEMES.DARK)
155+
>>> console.print("[success]Operation completed![/]") # Now works!
156+
"""
157+
from rich.theme import Theme as RichTheme
158+
159+
return RichTheme(
160+
{
161+
"primary": self.primary,
162+
"secondary": self.secondary,
163+
"success": f"bold {self.success}",
164+
"warning": f"bold {self.warning}",
165+
"error": f"bold {self.error}",
166+
"info": self.info,
167+
"muted": f"dim {self.muted}",
168+
"border": self.border,
169+
"text": self.text,
170+
}
171+
)
172+
139173

140174
class ThemeRegistry(Registry[Theme]):
141175
"""Registry for color themes."""

0 commit comments

Comments
 (0)