Pass config object to terminal.

This commit is contained in:
Michael Lazar
2016-07-08 20:48:42 -07:00
parent e30d53c49e
commit cd9617b0d8
4 changed files with 56 additions and 56 deletions

View File

@@ -93,7 +93,7 @@ def main():
if not config['monochrome']: if not config['monochrome']:
Color.init() Color.init()
term = Terminal(stdscr, config['ascii']) term = Terminal(stdscr, config)
with term.loader('Initializing', catch_exception=False): with term.loader('Initializing', catch_exception=False):
reddit = praw.Reddit(user_agent=user_agent, reddit = praw.Reddit(user_agent=user_agent,
decode_html_entities=False, decode_html_entities=False,

View File

@@ -42,28 +42,28 @@ class Terminal(object):
RETURN = 10 RETURN = 10
SPACE = 32 SPACE = 32
def __init__(self, stdscr, ascii=False): def __init__(self, stdscr, config):
self.stdscr = stdscr self.stdscr = stdscr
self.ascii = ascii self.config = config
self.loader = LoadScreen(self) self.loader = LoadScreen(self)
self._display = None self._display = None
@property @property
def up_arrow(self): def up_arrow(self):
symbol = '^' if self.ascii else '' symbol = '^' if self.config['ascii'] else ''
attr = curses.A_BOLD | Color.GREEN attr = curses.A_BOLD | Color.GREEN
return symbol, attr return symbol, attr
@property @property
def down_arrow(self): def down_arrow(self):
symbol = 'v' if self.ascii else '' symbol = 'v' if self.config['ascii'] else ''
attr = curses.A_BOLD | Color.RED attr = curses.A_BOLD | Color.RED
return symbol, attr return symbol, attr
@property @property
def neutral_arrow(self): def neutral_arrow(self):
symbol = 'o' if self.ascii else '' symbol = 'o' if self.config['ascii'] else ''
attr = curses.A_BOLD attr = curses.A_BOLD
return symbol, attr return symbol, attr
@@ -75,7 +75,7 @@ class Terminal(object):
@property @property
def guilded(self): def guilded(self):
symbol = '*' if self.ascii else '' symbol = '*' if self.config['ascii'] else ''
attr = curses.A_BOLD | Color.YELLOW attr = curses.A_BOLD | Color.YELLOW
return symbol, attr return symbol, attr
@@ -215,7 +215,7 @@ class Terminal(object):
if isinstance(string, six.text_type): if isinstance(string, six.text_type):
string = unescape(string) string = unescape(string)
if self.ascii: if self.config['ascii']:
if isinstance(string, six.binary_type): if isinstance(string, six.binary_type):
string = string.decode('utf-8') string = string.decode('utf-8')
string = string.encode('ascii', 'replace') string = string.encode('ascii', 'replace')

View File

@@ -18,9 +18,9 @@ except ImportError:
import mock import mock
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_objects_load_screen(terminal, stdscr, ascii): def test_objects_load_screen(terminal, stdscr, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
# Ensure the thread is properly started/stopped # Ensure the thread is properly started/stopped
with terminal.loader(delay=0, message=u'Hello', trail=u'...'): with terminal.loader(delay=0, message=u'Hello', trail=u'...'):
@@ -32,9 +32,9 @@ def test_objects_load_screen(terminal, stdscr, ascii):
assert stdscr.subwin.nlines == 3 assert stdscr.subwin.nlines == 3
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_objects_load_screen_exception_unhandled(terminal, stdscr, ascii): def test_objects_load_screen_exception_unhandled(terminal, stdscr, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
# Raising an exception should clean up the loader properly # Raising an exception should clean up the loader properly
with pytest.raises(Exception): with pytest.raises(Exception):
@@ -45,9 +45,9 @@ def test_objects_load_screen_exception_unhandled(terminal, stdscr, ascii):
assert not terminal.loader._animator.is_alive() assert not terminal.loader._animator.is_alive()
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_objects_load_screen_exception_handled(terminal, stdscr, ascii): def test_objects_load_screen_exception_handled(terminal, stdscr, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
# Raising a handled exception should get stored on the loaders # Raising a handled exception should get stored on the loaders
with terminal.loader(delay=0): with terminal.loader(delay=0):
@@ -60,9 +60,9 @@ def test_objects_load_screen_exception_handled(terminal, stdscr, ascii):
stdscr.subwin.addstr.assert_called_with(1, 1, error_message) stdscr.subwin.addstr.assert_called_with(1, 1, error_message)
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_objects_load_screen_exception_not_caught(terminal, stdscr, ascii): def test_objects_load_screen_exception_not_caught(terminal, stdscr, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
with pytest.raises(KeyboardInterrupt): with pytest.raises(KeyboardInterrupt):
with terminal.loader(delay=0, catch_exception=False): with terminal.loader(delay=0, catch_exception=False):
@@ -73,9 +73,9 @@ def test_objects_load_screen_exception_not_caught(terminal, stdscr, ascii):
assert terminal.loader.exception is None assert terminal.loader.exception is None
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_objects_load_screen_keyboard_interrupt(terminal, stdscr, ascii): def test_objects_load_screen_keyboard_interrupt(terminal, stdscr, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
# Raising a KeyboardInterrupt should be also be stored # Raising a KeyboardInterrupt should be also be stored
with terminal.loader(delay=0): with terminal.loader(delay=0):
@@ -86,9 +86,9 @@ def test_objects_load_screen_keyboard_interrupt(terminal, stdscr, ascii):
assert isinstance(terminal.loader.exception, KeyboardInterrupt) assert isinstance(terminal.loader.exception, KeyboardInterrupt)
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_objects_load_screen_escape(terminal, stdscr, ascii): def test_objects_load_screen_escape(terminal, stdscr, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
stdscr.getch.return_value = terminal.ESCAPE stdscr.getch.return_value = terminal.ESCAPE
@@ -109,9 +109,9 @@ def test_objects_load_screen_escape(terminal, stdscr, ascii):
assert kill.called assert kill.called
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_objects_load_screen_initial_delay(terminal, stdscr, ascii): def test_objects_load_screen_initial_delay(terminal, stdscr, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
# If we don't reach the initial delay nothing should be drawn # If we don't reach the initial delay nothing should be drawn
with terminal.loader(delay=0.1): with terminal.loader(delay=0.1):
@@ -119,9 +119,9 @@ def test_objects_load_screen_initial_delay(terminal, stdscr, ascii):
assert not stdscr.subwin.addstr.called assert not stdscr.subwin.addstr.called
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_objects_load_screen_nested(terminal, ascii): def test_objects_load_screen_nested(terminal, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
with terminal.loader(message='Outer'): with terminal.loader(message='Outer'):
with terminal.loader(message='Inner'): with terminal.loader(message='Inner'):
@@ -134,9 +134,9 @@ def test_objects_load_screen_nested(terminal, ascii):
assert not terminal.loader._animator.is_alive() assert not terminal.loader._animator.is_alive()
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_objects_load_screen_nested_complex(terminal, stdscr, ascii): def test_objects_load_screen_nested_complex(terminal, stdscr, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
with terminal.loader(message='Outer') as outer_loader: with terminal.loader(message='Outer') as outer_loader:
assert outer_loader.depth == 1 assert outer_loader.depth == 1

View File

@@ -51,7 +51,7 @@ def test_terminal_properties(terminal, config):
assert terminal.get_arrow(None) is not None assert terminal.get_arrow(None) is not None
assert terminal.get_arrow(True) is not None assert terminal.get_arrow(True) is not None
assert terminal.get_arrow(False) is not None assert terminal.get_arrow(False) is not None
assert terminal.ascii == config['ascii'] assert terminal.config == config
assert terminal.loader is not None assert terminal.loader is not None
assert terminal.MIN_HEIGHT is not None assert terminal.MIN_HEIGHT is not None
@@ -93,7 +93,7 @@ def test_terminal_functions(terminal):
def test_terminal_clean_ascii(terminal): def test_terminal_clean_ascii(terminal):
terminal.ascii = True terminal.config['ascii'] = True
# unicode returns ascii # unicode returns ascii
text = terminal.clean('hello ❤') text = terminal.clean('hello ❤')
@@ -113,7 +113,7 @@ def test_terminal_clean_ascii(terminal):
def test_terminal_clean_unicode(terminal): def test_terminal_clean_unicode(terminal):
terminal.ascii = False terminal.config['ascii'] = False
# unicode returns utf-8 # unicode returns utf-8
text = terminal.clean('hello ❤') text = terminal.clean('hello ❤')
@@ -146,20 +146,20 @@ def test_terminal_clean_ncols(terminal):
assert text.decode('utf-8') == '' assert text.decode('utf-8') == ''
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_terminal_clean_unescape_html(terminal, ascii): def test_terminal_clean_unescape_html(terminal, use_ascii):
# HTML characters get decoded # HTML characters get decoded
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
text = terminal.clean('<') text = terminal.clean('<')
assert isinstance(text, six.binary_type) assert isinstance(text, six.binary_type)
assert text.decode('ascii' if ascii else 'utf-8') == '<' assert text.decode('ascii' if ascii else 'utf-8') == '<'
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_terminal_add_line(terminal, stdscr, ascii): def test_terminal_add_line(terminal, stdscr, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
terminal.add_line(stdscr, 'hello') terminal.add_line(stdscr, 'hello')
assert stdscr.addstr.called_with(0, 0, 'hello'.encode('ascii')) assert stdscr.addstr.called_with(0, 0, 'hello'.encode('ascii'))
@@ -176,10 +176,10 @@ def test_terminal_add_line(terminal, stdscr, ascii):
stdscr.reset_mock() stdscr.reset_mock()
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_show_notification(terminal, stdscr, ascii): def test_show_notification(terminal, stdscr, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
# The whole message should fit in 40x80 # The whole message should fit in 40x80
text = HELP.strip().splitlines() text = HELP.strip().splitlines()
@@ -198,10 +198,10 @@ def test_show_notification(terminal, stdscr, ascii):
assert stdscr.subwin.addstr.call_count == 13 assert stdscr.subwin.addstr.call_count == 13
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_text_input(terminal, stdscr, ascii): def test_text_input(terminal, stdscr, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
stdscr.nlines = 1 stdscr.nlines = 1
# Text will be wrong because stdscr.inch() is not implemented # Text will be wrong because stdscr.inch() is not implemented
@@ -219,10 +219,10 @@ def test_text_input(terminal, stdscr, ascii):
assert terminal.text_input(stdscr, allow_resize=False) is None assert terminal.text_input(stdscr, allow_resize=False) is None
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_prompt_input(terminal, stdscr, ascii): def test_prompt_input(terminal, stdscr, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
window = stdscr.derwin() window = stdscr.derwin()
window.getch.side_effect = [ord('h'), ord('i'), terminal.RETURN] window.getch.side_effect = [ord('h'), ord('i'), terminal.RETURN]
@@ -270,10 +270,10 @@ def test_prompt_y_or_n(terminal, stdscr):
assert curses.flash.called assert curses.flash.called
@pytest.mark.parametrize('ascii', [True, False]) @pytest.mark.parametrize('use_ascii', [True, False])
def test_open_editor(terminal, ascii): def test_open_editor(terminal, use_ascii):
terminal.ascii = ascii terminal.config['ascii'] = use_ascii
comment = COMMENT_EDIT_FILE.format(content='#| This is a comment! ❤') comment = COMMENT_EDIT_FILE.format(content='#| This is a comment! ❤')
data = {'filename': None} data = {'filename': None}