From 00df45bd34b027a7f5021374db54657de55dbd88 Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Sat, 4 Nov 2017 14:10:50 -0400 Subject: [PATCH] Fix crash on startup when terminal doesnt support colors --- rtv/objects.py | 4 +--- rtv/terminal.py | 2 +- tests/test_terminal.py | 13 +++++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/rtv/objects.py b/rtv/objects.py index 40274ac..cec6add 100644 --- a/rtv/objects.py +++ b/rtv/objects.py @@ -83,6 +83,7 @@ def curses_session(): # return from C start_color() is ignorable. try: curses.start_color() + curses.use_default_colors() except: _logger.warning('Curses failed to initialize color support') @@ -92,9 +93,6 @@ def curses_session(): except: _logger.warning('Curses failed to initialize the cursor mode') - # Assign the terminal's default (background) color to code -1 - curses.use_default_colors() - yield stdscr finally: diff --git a/rtv/terminal.py b/rtv/terminal.py index 98d2af2..4a7b8d4 100644 --- a/rtv/terminal.py +++ b/rtv/terminal.py @@ -849,7 +849,7 @@ class Terminal(object): """ monochrome = (not curses.has_colors()) - if theme is None: + if theme is None or monochrome: theme = Theme(monochrome=monochrome) theme.bind_curses() diff --git a/tests/test_terminal.py b/tests/test_terminal.py index a548ae0..52950e7 100644 --- a/tests/test_terminal.py +++ b/tests/test_terminal.py @@ -596,3 +596,16 @@ def test_set_theme(terminal, stdscr): terminal.set_theme(theme=theme) assert terminal.theme.monochrome stdscr.bkgd.assert_called_once_with(' ', 0) + + +def test_set_theme_no_colors(terminal, stdscr): + + with mock.patch('curses.has_colors') as has_colors: + has_colors.return_value = False + + terminal.set_theme() + assert terminal.theme.monochrome + + theme = Theme(monochrome=False) + terminal.set_theme(theme=theme) + assert terminal.theme.monochrome