Skip to content

Commit 7f9e85a

Browse files
committed
color/themes/zt_dark: Enable marking common background color in styles.
This allows easily updating the background color to a theme by updating the color in only one location, as well as making it clear upon reading, if a background to a style is distinct from the background. To apply this to a theme with some common background color `Color.BG`: - import `Background` from `zulipterminal.config.color` - replace `Color.BG` values with `Background.COLOR` in styles (STYLES) - add a "background" entry to the META dictionary, with value `Color.BG` This commit applies this to only the 'zt_dark' theme. Note that in the absence of a globally reasonable fallback value, if the "background" entry is not specified in META, the background will be transparent or the terminal default background color. Tests updated.
1 parent 726123f commit 7f9e85a

File tree

4 files changed

+63
-38
lines changed

4 files changed

+63
-38
lines changed

tests/config/test_themes.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pytest import param as case
1010
from pytest_mock import MockerFixture
1111

12+
from zulipterminal.config.color import Background
1213
from zulipterminal.config.regexes import REGEX_COLOR_VALID_FORMATS
1314
from zulipterminal.config.themes import (
1415
REQUIRED_STYLES,
@@ -86,7 +87,8 @@ def test_builtin_theme_completeness(theme_name: str) -> None:
8687
# Check if color used in STYLE exists in Color.
8788
for style_name, style_conf in theme_styles.items():
8889
fg, bg = style_conf
89-
assert fg in theme_colors and bg in theme_colors
90+
assert fg in theme_colors
91+
assert bg in theme_colors or bg in Background
9092
# Check completeness of META
9193
expected_META = {"pygments": ["styles", "background", "overrides"]}
9294
for metadata, config in expected_META.items():
@@ -303,7 +305,10 @@ class Color(Enum):
303305

304306
req_styles = {"s1": "", "s2": "bold"}
305307
mocker.patch.dict("zulipterminal.config.themes.REQUIRED_STYLES", req_styles)
306-
assert parse_themefile(theme_styles, color_depth) == expected_urwid_theme
308+
assert (
309+
parse_themefile(theme_styles, color_depth, Color.DARK_MAGENTA)
310+
== expected_urwid_theme
311+
)
307312

308313

309314
@pytest.mark.parametrize(

zulipterminal/config/color.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525

2626

2727
# fmt: off
28+
# Background is treated as transparent by default
29+
# This avoids the need for a dummy default color
30+
class Background(Enum):
31+
COLOR = 'default default default'
32+
33+
2834
# NOTE: The 24bit color codes use 256 color which can be
2935
# enhanced to be truly 24bit.
3036
# NOTE: The 256code format can be moved to h0-255 to

zulipterminal/config/themes.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from pygments.token import STANDARD_TYPES, _TokenType
77

8-
from zulipterminal.config.color import term16
8+
from zulipterminal.config.color import Background, term16
99
from zulipterminal.themes import gruvbox_dark, gruvbox_light, zt_blue, zt_dark, zt_light
1010

1111

@@ -186,11 +186,14 @@ def generate_theme(theme_name: str, color_depth: int) -> ThemeSpec:
186186
theme_styles = theme_module.STYLES
187187
except AttributeError:
188188
raise MissingThemeAttributeError("STYLES") from None
189-
urwid_theme = parse_themefile(theme_styles, color_depth)
190189

191190
# META is not required, but if present should contain pygments data
192191
theme_meta = getattr(theme_module, "META", None)
193192
if theme_meta is not None:
193+
# FIXME: Is META now required? Or only if Background.COLOR present?
194+
# If used in styles and background is not specified, transparent by default!
195+
background_color = theme_meta.get("background", Background.COLOR)
196+
194197
pygments_data = theme_meta.get("pygments", None)
195198
if pygments_data is None:
196199
raise MissingThemeAttributeError('META["pygments"]') from None
@@ -199,8 +202,10 @@ def generate_theme(theme_name: str, color_depth: int) -> ThemeSpec:
199202
raise MissingThemeAttributeError(f'META["pygments"]["{key}"]') from None
200203
pygments_styles = generate_pygments_styles(pygments_data)
201204
else:
205+
background_color = Background.COLOR
202206
pygments_styles = []
203207

208+
urwid_theme = parse_themefile(theme_styles, color_depth, background_color)
204209
urwid_theme.extend(pygments_styles)
205210

206211
return urwid_theme
@@ -232,11 +237,18 @@ def validate_colors(color_enum: Any, color_depth: int) -> None:
232237

233238

234239
def parse_themefile(
235-
theme_styles: Dict[Optional[str], Tuple[Any, Any]], color_depth: int
240+
theme_styles: Dict[Optional[str], Tuple[Any, Any]],
241+
color_depth: int,
242+
background_color: Any,
236243
) -> ThemeSpec:
237244
urwid_theme = []
238245
for style_name, (fg_name, bg_name) in theme_styles.items():
239246
fg_code16, fg_code256, fg_code24, *fg_props = fg_name.value.split()
247+
248+
# If background (bg) is specific enum, use specified background_color instead
249+
if bg_name == Background.COLOR:
250+
bg_name = background_color # noqa: PLW2901 # overwrite loop variable
251+
240252
bg_code16, bg_code256, bg_code24, *bg_props = bg_name.value.split()
241253

242254
new_style: StyleSpec

zulipterminal/themes/zt_dark.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,71 +6,73 @@
66
"""
77
from pygments.styles.material import MaterialStyle
88

9+
from zulipterminal.config.color import Background
910
from zulipterminal.config.color import DefaultBoldColor as Color
1011

1112

1213
# fmt: off
1314
STYLES = {
1415
# style_name : foreground background
15-
None : (Color.WHITE, Color.BLACK),
16+
None : (Color.WHITE, Background.COLOR),
1617
'selected' : (Color.WHITE, Color.DARK_BLUE),
1718
'msg_selected' : (Color.WHITE, Color.DARK_BLUE),
1819
'header' : (Color.DARK_CYAN, Color.DARK_BLUE),
1920
'general_narrow' : (Color.WHITE, Color.DARK_BLUE),
20-
'general_bar' : (Color.WHITE, Color.BLACK),
21-
'msg_sender' : (Color.YELLOW__BOLD, Color.BLACK),
22-
'unread' : (Color.DARK_BLUE, Color.BLACK),
23-
'user_active' : (Color.LIGHT_GREEN, Color.BLACK),
24-
'user_idle' : (Color.YELLOW, Color.BLACK),
25-
'user_offline' : (Color.WHITE, Color.BLACK),
26-
'user_inactive' : (Color.WHITE, Color.BLACK),
27-
'user_bot' : (Color.WHITE, Color.BLACK),
28-
'title' : (Color.WHITE__BOLD, Color.BLACK),
29-
'column_title' : (Color.WHITE__BOLD, Color.BLACK),
30-
'time' : (Color.LIGHT_BLUE, Color.BLACK),
21+
'general_bar' : (Color.WHITE, Background.COLOR),
22+
'msg_sender' : (Color.YELLOW__BOLD, Background.COLOR),
23+
'unread' : (Color.DARK_BLUE, Background.COLOR),
24+
'user_active' : (Color.LIGHT_GREEN, Background.COLOR),
25+
'user_idle' : (Color.YELLOW, Background.COLOR),
26+
'user_offline' : (Color.WHITE, Background.COLOR),
27+
'user_inactive' : (Color.WHITE, Background.COLOR),
28+
'user_bot' : (Color.WHITE, Background.COLOR),
29+
'title' : (Color.WHITE__BOLD, Background.COLOR),
30+
'column_title' : (Color.WHITE__BOLD, Background.COLOR),
31+
'time' : (Color.LIGHT_BLUE, Background.COLOR),
3132
'bar' : (Color.WHITE, Color.DARK_GRAY),
32-
'msg_emoji' : (Color.LIGHT_MAGENTA, Color.BLACK),
33-
'reaction' : (Color.LIGHT_MAGENTA__BOLD, Color.BLACK),
33+
'msg_emoji' : (Color.LIGHT_MAGENTA, Background.COLOR),
34+
'reaction' : (Color.LIGHT_MAGENTA__BOLD, Background.COLOR),
3435
'reaction_mine' : (Color.BLACK, Color.LIGHT_MAGENTA),
3536
'msg_heading' : (Color.LIGHT_CYAN__BOLD, Color.DARK_MAGENTA),
3637
'msg_math' : (Color.LIGHT_GRAY, Color.DARK_GRAY),
37-
'msg_mention' : (Color.LIGHT_RED__BOLD, Color.BLACK),
38-
'msg_link' : (Color.LIGHT_BLUE, Color.BLACK),
39-
'msg_link_index' : (Color.LIGHT_BLUE__BOLD, Color.BLACK),
40-
'msg_quote' : (Color.BROWN, Color.BLACK),
41-
'msg_bold' : (Color.WHITE__BOLD, Color.BLACK),
38+
'msg_mention' : (Color.LIGHT_RED__BOLD, Background.COLOR),
39+
'msg_link' : (Color.LIGHT_BLUE, Background.COLOR),
40+
'msg_link_index' : (Color.LIGHT_BLUE__BOLD, Background.COLOR),
41+
'msg_quote' : (Color.BROWN, Background.COLOR),
42+
'msg_bold' : (Color.WHITE__BOLD, Background.COLOR),
4243
'msg_time' : (Color.BLACK, Color.WHITE),
4344
'footer' : (Color.BLACK, Color.LIGHT_GRAY),
44-
'footer_contrast' : (Color.WHITE, Color.BLACK),
45-
'starred' : (Color.LIGHT_RED__BOLD, Color.BLACK),
46-
'unread_count' : (Color.YELLOW, Color.BLACK),
47-
'starred_count' : (Color.LIGHT_GRAY, Color.BLACK),
48-
'table_head' : (Color.WHITE__BOLD, Color.BLACK),
45+
'footer_contrast' : (Color.WHITE, Background.COLOR),
46+
'starred' : (Color.LIGHT_RED__BOLD, Background.COLOR),
47+
'unread_count' : (Color.YELLOW, Background.COLOR),
48+
'starred_count' : (Color.LIGHT_GRAY, Background.COLOR),
49+
'table_head' : (Color.WHITE__BOLD, Background.COLOR),
4950
'filter_results' : (Color.WHITE, Color.DARK_GREEN),
5051
'edit_topic' : (Color.WHITE, Color.DARK_GRAY),
5152
'edit_tag' : (Color.WHITE, Color.DARK_GRAY),
52-
'edit_author' : (Color.YELLOW, Color.BLACK),
53-
'edit_time' : (Color.LIGHT_BLUE, Color.BLACK),
54-
'current_user' : (Color.WHITE, Color.BLACK),
55-
'muted' : (Color.LIGHT_BLUE, Color.BLACK),
56-
'popup_border' : (Color.WHITE, Color.BLACK),
57-
'popup_category' : (Color.LIGHT_BLUE__BOLD, Color.BLACK),
53+
'edit_author' : (Color.YELLOW, Background.COLOR),
54+
'edit_time' : (Color.LIGHT_BLUE, Background.COLOR),
55+
'current_user' : (Color.WHITE, Background.COLOR),
56+
'muted' : (Color.LIGHT_BLUE, Background.COLOR),
57+
'popup_border' : (Color.WHITE, Background.COLOR),
58+
'popup_category' : (Color.LIGHT_BLUE__BOLD, Background.COLOR),
5859
'popup_contrast' : (Color.WHITE, Color.DARK_GRAY),
59-
'popup_important' : (Color.LIGHT_RED__BOLD, Color.BLACK),
60-
'widget_disabled' : (Color.DARK_GRAY, Color.BLACK),
60+
'popup_important' : (Color.LIGHT_RED__BOLD, Background.COLOR),
61+
'widget_disabled' : (Color.DARK_GRAY, Background.COLOR),
6162
'area:help' : (Color.WHITE, Color.DARK_GREEN),
6263
'area:msg' : (Color.WHITE, Color.BROWN),
6364
'area:stream' : (Color.WHITE, Color.DARK_CYAN),
6465
'area:error' : (Color.WHITE, Color.DARK_RED),
6566
'area:user' : (Color.WHITE, Color.DARK_BLUE),
66-
'search_error' : (Color.LIGHT_RED, Color.BLACK),
67+
'search_error' : (Color.LIGHT_RED, Background.COLOR),
6768
'task:success' : (Color.WHITE, Color.DARK_GREEN),
6869
'task:error' : (Color.WHITE, Color.DARK_RED),
6970
'task:warning' : (Color.WHITE, Color.BROWN),
7071
'ui_code' : (Color.BLACK, Color.WHITE),
7172
}
7273

7374
META = {
75+
'background': Color.BLACK,
7476
'pygments': {
7577
'styles' : MaterialStyle().styles,
7678
'background': 'h235',

0 commit comments

Comments
 (0)