Refactoring the monochrome stuff

This commit is contained in:
Michael Lazar
2017-09-11 00:30:18 -04:00
parent a208a6764a
commit 9af4b8c709
11 changed files with 110 additions and 185 deletions

View File

@@ -51,14 +51,12 @@ class Terminal(object):
RETURN = 10
SPACE = 32
def __init__(self, stdscr, config, theme=None):
def __init__(self, stdscr, config):
self.stdscr = stdscr
self.config = config
self.loader = LoadScreen(self)
self.theme = None
self.set_theme(theme)
self.theme = None # Initialized by term.set_theme()
self._display = None
self._mailcap_dict = mailcap.getcaps()
@@ -831,7 +829,7 @@ class Terminal(object):
return self.theme.get(element)
def set_theme(self, theme=None):
def set_theme(self, theme=None, monochrome=False):
"""
Check that the terminal supports the provided theme, and applies
the theme to the terminal if possible.
@@ -839,11 +837,25 @@ class Terminal(object):
If the terminal doesn't support the theme, this falls back to the
default theme. The default theme only requires 8 colors so it
should be compatible with any terminal that supports basic colors.
Using ``monochrome=True`` will force loading the current theme
without any color support. The intention is that this be used as
a fallback for the default theme to support the old --monochrome
command line flag.
"""
monochrome = (not curses.has_colors())
if not monochrome and curses.has_colors():
terminal_colors = curses.COLORS
else:
terminal_colors = 0
if theme is None:
theme = Theme(monochrome=monochrome)
theme = Theme()
elif monochrome:
# No need to display a warning message if the user has
# explicitly turned off support for colors
pass
elif theme.required_color_pairs > curses.COLOR_PAIRS:
_logger.warning(
@@ -851,19 +863,19 @@ class Terminal(object):
'supports %s color pairs, switching to default theme',
theme.name, theme.required_color_pairs, self._term,
curses.COLOR_PAIRS)
theme = Theme(monochrome=monochrome)
theme = Theme()
elif theme.required_colors > curses.COLORS:
elif theme.required_colors > terminal_colors:
_logger.warning(
'Theme %s requires %s colors, but TERM %s only '
'supports %s colors, switching to default theme',
theme.name, theme.required_colors, self._term,
curses.COLORS)
theme = Theme(monochrome=monochrome)
theme = Theme()
theme.bind_curses()
theme.bind_curses(use_color=bool(terminal_colors))
# Apply the default color to the whole screen
self.stdscr.bkgd(str(' '), theme.get('@normal'))
self.theme = theme
self.theme = theme