Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for different color depths (16, 1) #563

Merged
merged 2 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tests/cli/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def test_main_help(capsys, options):
'--config-file CONFIG_FILE, -c CONFIG_FILE',
'--autohide',
'--no-autohide',
'-v, --version'
'-v, --version',
'--color-depth'
}
optional_argument_lines = {line[2:] for line in lines
if len(line) > 2 and line[2] == '-'}
Expand Down
23 changes: 23 additions & 0 deletions tests/config/test_themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from zulipterminal.config.themes import (
THEMES, all_themes, complete_and_incomplete_themes, required_styles,
theme_with_monochrome_added,
)


Expand Down Expand Up @@ -34,3 +35,25 @@ def test_complete_and_incomplete_themes():
result = (sorted(list(expected_complete_themes)),
sorted(list(set(THEMES)-expected_complete_themes)))
assert result == complete_and_incomplete_themes()


@pytest.mark.parametrize('theme, expected_new_theme, req_styles', [
([('a', 'another')], [], {}),
([('a', 'another')], [('a', 'another')], {'a': ''}),
([('a', 'fg', 'bg')], [('a', 'fg', 'bg', 'x')], {'a': 'x'}),
([('a', 'fg', 'bg', 'bold')], [('a', 'fg', 'bg', 'x')], {'a': 'x'}),
([('a', 'fg', 'bg', 'bold', 'h1', 'h2')],
[('a', 'fg', 'bg', 'x', 'h1', 'h2')],
{'a': 'x'}),
], ids=[
'incomplete_theme',
'one_to_one',
'16_color_add_mono',
'16_color_mono_overwrite',
'256_color',
])
def test_theme_with_monochrome_added(mocker,
theme, expected_new_theme, req_styles):
mocker.patch.dict('zulipterminal.config.themes.required_styles',
req_styles)
assert theme_with_monochrome_added(theme) == expected_new_theme
2 changes: 1 addition & 1 deletion tests/core/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def controller(self, mocker) -> None:
self.theme = 'default'
self.autohide = True # FIXME Add tests for no-autohide
self.notify_enabled = False
return Controller(self.config_file, self.theme, self.autohide,
return Controller(self.config_file, self.theme, 256, self.autohide,
self.notify_enabled)

def test_initialize_controller(self, controller, mocker) -> None:
Expand Down
14 changes: 13 additions & 1 deletion zulipterminal/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from zulipterminal.config.themes import (
THEMES, all_themes, complete_and_incomplete_themes,
theme_with_monochrome_added,
)
from zulipterminal.core import Controller
from zulipterminal.model import ServerConnectionFailure
Expand Down Expand Up @@ -48,6 +49,9 @@ def parse_args(argv: List[str]) -> argparse.Namespace:
organization.(e.g. ~/zuliprc)')
parser.add_argument('--theme', '-t',
help='choose color theme. (e.g. blue, light)')
parser.add_argument('--color-depth',
choices=['1', '16', '256'], default=256,
help="Force the color depth (default 256).")
neiljp marked this conversation as resolved.
Show resolved Hide resolved
# debug mode
parser.add_argument("-d",
"--debug",
Expand Down Expand Up @@ -268,8 +272,16 @@ def main(options: Optional[List[str]]=None) -> None:
print("Specify the {} option in zuliprc file.".format(setting))
sys.exit(1)
boolean_settings[setting] = (zterm[setting][0] == valid_values[0])

color_depth = int(args.color_depth)
if color_depth == 1:
theme_data = theme_with_monochrome_added(THEMES[theme_to_use[0]])
else:
theme_data = THEMES[theme_to_use[0]]

Controller(zuliprc_path,
THEMES[theme_to_use[0]],
theme_data,
int(args.color_depth),
**boolean_settings).main()
except ServerConnectionFailure as e:
print(in_color('red',
Expand Down
107 changes: 65 additions & 42 deletions zulipterminal/config/themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,38 @@

ThemeSpec = List[Tuple[Optional[str], ...]]

required_styles = {
None,
'selected',
'msg_selected',
'header',
'custom',
'name',
'unread',
'user_active',
'user_idle',
'user_offline',
'user_inactive',
'title',
'time',
'bar',
'popup_contrast',
'msg_emoji',
'reaction',
'msg_mention',
'msg_link',
'msg_quote',
'msg_code',
'msg_bold',
'footer',
'starred',
'popup_category',
'unread_count',
'filter_results',
# The keys in required_styles specify what styles are necessary for a theme to
# be complete, while the values are those used to style each element in
# monochrome (1-bit) mode - independently of the specified theme

required_styles = { # style-name: monochrome-bit-depth-style
None: '',
neiljp marked this conversation as resolved.
Show resolved Hide resolved
'selected': 'standout',
'msg_selected': 'standout',
'header': 'bold',
'custom': 'standout',
'name': '',
'unread': 'strikethrough',
'user_active': 'bold',
'user_idle': '',
'user_offline': '',
'user_inactive': '',
'title': 'standout',
'time': '',
'bar': 'standout',
'popup_contrast': 'standout',
'msg_emoji': 'bold',
'reaction': 'bold',
'msg_mention': 'bold',
'msg_link': '',
'msg_quote': 'underline',
'msg_code': 'bold',
'msg_bold': 'bold',
'footer': 'standout',
'starred': 'bold',
'popup_category': 'bold',
'unread_count': '',
'filter_results': 'bold',
}

# Colors used in gruvbox-256
Expand All @@ -55,8 +59,8 @@
(None, 'white', ''),
('selected', 'white', 'dark blue'),
('msg_selected', 'white', 'dark blue'),
('header', 'dark cyan', 'dark blue', 'bold'),
('custom', 'white', 'dark blue', 'underline'),
('header', 'dark cyan', 'dark blue'),
('custom', 'white', 'dark blue'),
('name', 'yellow, bold', ''),
('unread', 'dark blue', ''),
('user_active', 'light green', ''),
Expand All @@ -74,7 +78,7 @@
('msg_quote', 'brown', ''),
('msg_code', 'black', 'white'),
('msg_bold', 'white, bold', ''),
('footer', 'white', 'dark red', 'bold'),
('footer', 'white', 'dark red'),
('starred', 'light red, bold', ''),
('popup_category', 'light blue, bold', ''),
('unread_count', 'yellow', ''),
Expand All @@ -91,9 +95,9 @@
('msg_selected', 'black', 'white',
None, BLACK, WHITE),
('header', 'dark cyan', 'dark blue',
'bold', 'dark cyan', DARKBLUE),
None, 'dark cyan', DARKBLUE),
('custom', 'white', 'dark blue',
'underline', WHITE, DARKBLUE),
None, WHITE, DARKBLUE),
('name', 'yellow, bold', 'black',
None, YELLOWBOLD, BLACK),
('unread', 'light magenta', 'black',
Expand Down Expand Up @@ -129,7 +133,7 @@
('msg_bold', 'white, bold', 'black',
None, WHITEBOLD, BLACK),
('footer', 'white', 'dark red',
'bold', WHITE, DARKRED),
None, WHITE, DARKRED),
('starred', 'light red, bold', 'black',
None, LIGHTREDBOLD, BLACK),
('popup_category', 'light blue, bold', 'black',
Expand All @@ -145,9 +149,9 @@
(None, 'black', 'white'),
('selected', 'black', 'light green'),
('msg_selected', 'black', 'light green'),
('header', 'white', 'dark blue', 'bold'),
('custom', 'white', 'dark blue', 'underline'),
('name', 'dark green', 'white', 'bold'),
('header', 'white', 'dark blue'),
('custom', 'white', 'dark blue'),
('name', 'dark green', 'white'),
('unread', 'dark gray', 'light gray'),
('user_active', 'dark green', 'white'),
('user_idle', 'dark blue', 'white'),
Expand All @@ -164,7 +168,7 @@
('msg_quote', 'black', 'brown'),
('msg_code', 'black', 'light gray'),
('msg_bold', 'white, bold', 'dark gray'),
('footer', 'white', 'dark red', 'bold'),
('footer', 'white', 'dark red'),
('starred', 'light red, bold', 'white'),
('popup_category', 'dark gray, bold', 'light gray'),
('unread_count', 'dark blue, bold', 'white'),
Expand All @@ -175,9 +179,9 @@
(None, 'black', 'light blue'),
('selected', 'black', 'light gray'),
('msg_selected', 'black', 'light gray'),
('header', 'black', 'dark blue', 'bold'),
('custom', 'white', 'dark blue', 'underline'),
('name', 'dark red', 'light blue', 'bold'),
('header', 'black', 'dark blue'),
('custom', 'white', 'dark blue'),
('name', 'dark red', 'light blue'),
('unread', 'light gray', 'light blue'),
('user_active', 'light green, bold', 'light blue'),
('user_idle', 'dark gray', 'light blue'),
Expand All @@ -194,7 +198,7 @@
('msg_quote', 'brown', 'dark blue'),
('msg_code', 'dark blue', 'white'),
('msg_bold', 'white, bold', 'dark blue'),
('footer', 'white', 'dark red', 'bold'),
('footer', 'white', 'dark red'),
('starred', 'light red, bold', 'light blue'),
('popup_category', 'light gray, bold', 'light blue'),
('unread_count', 'yellow', 'light blue'),
Expand All @@ -213,3 +217,22 @@ def complete_and_incomplete_themes() -> Tuple[List[str], List[str]]:
if set(s[0] for s in styles).issuperset(required_styles)}
incomplete = list(set(THEMES) - complete)
return sorted(list(complete)), sorted(incomplete)


def theme_with_monochrome_added(theme: ThemeSpec) -> ThemeSpec:
updated_theme = []
for style in theme:
style_name = style[0]
if style_name not in required_styles: # incomplete theme
continue
mono_style = required_styles[style_name]
if len(style) > 4: # 256 colors+
new_style = style[:3] + (mono_style,) + style[4:]
elif len(style) == 4: # 16 colors + mono (overwrite mono)
new_style = style[:3] + (mono_style,)
elif len(style) == 3: # 16 colors only
new_style = style[:3] + (mono_style,)
else: # 1-to-1 mapping (same as other style)
new_style = style
updated_theme.append(new_style)
return updated_theme
4 changes: 3 additions & 1 deletion zulipterminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ class Controller:
"""

def __init__(self, config_file: str, theme: ThemeSpec,
color_depth: int,
autohide: bool, notify: bool) -> None:
self.theme = theme
self.color_depth = color_depth
self.autohide = autohide
self.notify_enabled = notify
self.editor_mode = False # type: bool
Expand Down Expand Up @@ -262,7 +264,7 @@ def exit_handler(self, signum: int, frame: Any) -> None:

def main(self) -> None:
screen = Screen()
screen.set_terminal_properties(colors=256)
screen.set_terminal_properties(colors=self.color_depth)
self.loop = urwid.MainLoop(self.view,
self.theme,
screen=screen)
Expand Down