Skip to content

Commit c51218c

Browse files
committed
themes/run: Enable transparency via generate_theme & parse_themefile.
This is connected through to run.py, but is disabled pending config-file/CLI infrastructure. Tests updated.
1 parent 2091e8e commit c51218c

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

tests/config/test_themes.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ class FakeTheme:
202202

203203
mocker.patch(MODULE + ".THEMES", {fake_theme_name: FakeTheme})
204204

205-
generated_theme = generate_theme(fake_theme_name, depth)
205+
generated_theme = generate_theme(
206+
fake_theme_name, color_depth=depth, transparent_background=False
207+
)
206208

207209
assert len(generated_theme) == len(theme_styles) + expected_pygments_length
208210
assert (single_style, "", "", "", "a", "b") in generated_theme
@@ -219,9 +221,11 @@ class FakeTheme:
219221

220222
mocker.patch(MODULE + ".THEMES", {fake_theme_name: FakeTheme})
221223

224+
kwargs: Dict[str, Any] = dict(color_depth=depth, transparent_background=False)
225+
222226
# No attributes (STYLES or META) - flag missing Color
223227
with pytest.raises(MissingThemeAttributeError) as e:
224-
generate_theme(fake_theme_name, depth)
228+
generate_theme(fake_theme_name, **kwargs)
225229
assert str(e.value) == "Theme is missing required attribute 'Color'"
226230

227231
# Color but missing STYLES - flag missing STYLES
@@ -232,7 +236,7 @@ class FakeColor(Enum):
232236
FakeTheme.Color = FakeColor # type: ignore [attr-defined]
233237

234238
with pytest.raises(MissingThemeAttributeError) as e:
235-
generate_theme(fake_theme_name, depth)
239+
generate_theme(fake_theme_name, **kwargs)
236240
assert str(e.value) == "Theme is missing required attribute 'STYLES'"
237241

238242
# Color, STYLES and META, but no pygments data in META
@@ -241,7 +245,7 @@ class FakeColor(Enum):
241245
FakeTheme.META = {} # type: ignore [attr-defined]
242246

243247
with pytest.raises(MissingThemeAttributeError) as e:
244-
generate_theme(fake_theme_name, depth)
248+
generate_theme(fake_theme_name, **kwargs)
245249
assert str(e.value) == """Theme is missing required attribute 'META["pygments"]'"""
246250

247251
# Color, STYLES and META, but incomplete pygments in META
@@ -250,7 +254,7 @@ class FakeColor(Enum):
250254
}
251255

252256
with pytest.raises(MissingThemeAttributeError) as e:
253-
generate_theme(fake_theme_name, depth)
257+
generate_theme(fake_theme_name, **kwargs)
254258
assert (
255259
str(e.value)
256260
== """Theme is missing required attribute 'META["pygments"]["overrides"]'"""
@@ -306,7 +310,7 @@ class Color(Enum):
306310
req_styles = {"s1": "", "s2": "bold"}
307311
mocker.patch.dict("zulipterminal.config.themes.REQUIRED_STYLES", req_styles)
308312
assert (
309-
parse_themefile(theme_styles, color_depth, Color.DARK_MAGENTA)
313+
parse_themefile(theme_styles, color_depth, Color.DARK_MAGENTA, False)
310314
== expected_urwid_theme
311315
)
312316

tests/core/test_core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def controller(self, mocker: MockerFixture) -> Controller:
4444

4545
self.config_file = "path/to/zuliprc"
4646
self.theme_name = "zt_dark"
47-
self.theme = generate_theme("zt_dark", 256)
47+
self.theme = generate_theme(
48+
"zt_dark", color_depth=256, transparent_background=False
49+
)
4850
self.in_explore_mode = False
4951
self.autohide = True # FIXME Add tests for no-autohide
5052
self.notify_enabled = False

zulipterminal/cli/run.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,11 @@ def print_setting(setting: str, data: SettingData, suffix: str = "") -> None:
559559
color_depth_str = zterm["color-depth"].value
560560
color_depth = COLOR_DEPTH_ARGS_TO_DEPTHS[color_depth_str]
561561

562-
theme_data = generate_theme(theme_to_use.value, color_depth)
562+
theme_data = generate_theme(
563+
theme_to_use.value,
564+
color_depth=color_depth,
565+
transparent_background=False,
566+
)
563567

564568
# Translate valid strings for boolean values into True/False
565569
boolean_settings: Dict[str, bool] = dict()

zulipterminal/config/themes.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,13 @@ def complete_and_incomplete_themes() -> Tuple[List[str], List[str]]:
173173
return sorted(complete), sorted(incomplete)
174174

175175

176-
def generate_theme(theme_name: str, color_depth: int) -> ThemeSpec:
177-
theme_module = THEMES[theme_name]
176+
def generate_theme(
177+
name: str,
178+
*,
179+
color_depth: int,
180+
transparent_background: bool,
181+
) -> ThemeSpec:
182+
theme_module = THEMES[name]
178183

179184
try:
180185
theme_colors = theme_module.Color
@@ -205,7 +210,12 @@ def generate_theme(theme_name: str, color_depth: int) -> ThemeSpec:
205210
background_color = Background.COLOR
206211
pygments_styles = []
207212

208-
urwid_theme = parse_themefile(theme_styles, color_depth, background_color)
213+
urwid_theme = parse_themefile(
214+
theme_styles,
215+
color_depth,
216+
background_color,
217+
transparent_background,
218+
)
209219
urwid_theme.extend(pygments_styles)
210220

211221
return urwid_theme
@@ -240,13 +250,15 @@ def parse_themefile(
240250
theme_styles: Dict[Optional[str], Tuple[Any, Any]],
241251
color_depth: int,
242252
background_color: Any,
253+
transparent_background: bool,
243254
) -> ThemeSpec:
244255
urwid_theme = []
245256
for style_name, (fg_name, bg_name) in theme_styles.items():
246257
fg_code16, fg_code256, fg_code24, *fg_props = fg_name.value.split()
247258

248-
# If background (bg) is specific enum, use specified background_color instead
249-
if bg_name == Background.COLOR:
259+
# Background.COLOR is transparent
260+
# => Replace with background_color, unless transparency is requested
261+
if bg_name == Background.COLOR and not transparent_background:
250262
bg_name = background_color # noqa: PLW2901 # overwrite loop variable
251263

252264
bg_code16, bg_code256, bg_code24, *bg_props = bg_name.value.split()

0 commit comments

Comments
 (0)