diff --git a/rtv/__main__.py b/rtv/__main__.py index 40ed843..e19bb15 100644 --- a/rtv/__main__.py +++ b/rtv/__main__.py @@ -12,7 +12,7 @@ from . import docs from .config import Config, copy_default_config from .oauth import OAuthHelper from .terminal import Terminal -from .objects import curses_session +from .objects import curses_session, Color from .subreddit import SubredditPage from .exceptions import ConfigError from .__version__ import __version__ @@ -83,6 +83,11 @@ def main(): try: with curses_session() as stdscr: + + # Initialize global color-pairs with curses + if not config['monochrome']: + Color.init() + term = Terminal(stdscr, config['ascii']) with term.loader('Initializing', catch_exception=False): reddit = praw.Reddit(user_agent=user_agent, diff --git a/rtv/config.py b/rtv/config.py index 7519653..4ed475d 100644 --- a/rtv/config.py +++ b/rtv/config.py @@ -46,6 +46,9 @@ def build_parser(): parser.add_argument( '--ascii', action='store_const', const=True, help='enable ascii-only mode') + parser.add_argument( + '--monochrome', action='store_const', const=True, + help='enable monochrome mode and force text to black & white') parser.add_argument( '--non-persistent', dest='persistent', action='store_const', const=False, @@ -79,6 +82,7 @@ def copy_default_config(filename=CONFIG): print('Copying default settings to %s' % filename) shutil.copy(DEFAULT_CONFIG, filename) + os.chmod(filename, 0o664) class OrderedSet(object): @@ -208,6 +212,7 @@ class Config(object): params = { 'ascii': partial(config.getboolean, 'rtv'), + 'monochrome': partial(config.getboolean, 'rtv'), 'clear_auth': partial(config.getboolean, 'rtv'), 'persistent': partial(config.getboolean, 'rtv'), 'history_size': partial(config.getint, 'rtv'), diff --git a/rtv/objects.py b/rtv/objects.py index f64e3cb..7e3fab7 100644 --- a/rtv/objects.py +++ b/rtv/objects.py @@ -62,7 +62,8 @@ def curses_session(): # Hide the blinking cursor curses.curs_set(0) - Color.init() + # Assign the terminal's default (background) color to code -1 + curses.use_default_colors() yield stdscr @@ -254,7 +255,6 @@ class LoadScreen(object): class Color(object): - """ Color attributes for curses. """ @@ -286,9 +286,6 @@ class Color(object): curses color pairs can be accessed directly through class attributes. """ - # Assign the terminal's default (background) color to code -1 - curses.use_default_colors() - for index, (attr, code) in enumerate(cls._colors.items(), start=1): curses.init_pair(index, code[0], code[1]) setattr(cls, attr, curses.color_pair(index)) diff --git a/rtv/rtv.cfg b/rtv/rtv.cfg index 034d4a0..4d89afe 100644 --- a/rtv/rtv.cfg +++ b/rtv/rtv.cfg @@ -13,6 +13,9 @@ ; This may be necessary for compatibility with some terminal browsers. ascii = False +; Turn on monochrome mode to force text to black and white. +monochrome = False + ; Enable debugging by logging all HTTP requests and errors to the given file. ;log = /tmp/rtv.log diff --git a/tests/test_config.py b/tests/test_config.py index 1f409c4..eb1bcf4 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -53,9 +53,10 @@ def test_config_get_args(): '--log', 'logfile.log', '--config', 'configfile.cfg', '--ascii', + '--monochrome', '--non-persistent', '--clear-auth', - '--copy-config'] + '--copy-config',] with mock.patch('sys.argv', ['rtv']): config_dict = Config.get_args() @@ -67,6 +68,7 @@ def test_config_get_args(): config = Config(**config_dict) assert config['ascii'] is True + assert config['monochrome'] is True assert config['subreddit'] == 'cfb' assert config['log'] == 'logfile.log' assert config['ascii'] is True @@ -82,6 +84,7 @@ def test_config_from_file(): args = { 'ascii': True, + 'monochrome': True, 'persistent': False, 'clear_auth': True, 'log': 'logfile.log', diff --git a/tests/test_objects.py b/tests/test_objects.py index d9592f6..84bb016 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -168,7 +168,6 @@ def test_objects_color(stdscr): assert getattr(Color, color) == curses.A_NORMAL Color.init() - assert curses.use_default_colors.called # Check that all colors are populated for color in colors: @@ -182,6 +181,7 @@ def test_objects_curses_session(stdscr): pass assert curses.initscr.called assert curses.endwin.called + assert curses.use_default_colors.called curses.initscr.reset_mock() curses.endwin.reset_mock()