Pass config object to terminal.
This commit is contained in:
@@ -93,7 +93,7 @@ def main():
|
||||
if not config['monochrome']:
|
||||
Color.init()
|
||||
|
||||
term = Terminal(stdscr, config['ascii'])
|
||||
term = Terminal(stdscr, config)
|
||||
with term.loader('Initializing', catch_exception=False):
|
||||
reddit = praw.Reddit(user_agent=user_agent,
|
||||
decode_html_entities=False,
|
||||
|
||||
@@ -42,28 +42,28 @@ class Terminal(object):
|
||||
RETURN = 10
|
||||
SPACE = 32
|
||||
|
||||
def __init__(self, stdscr, ascii=False):
|
||||
def __init__(self, stdscr, config):
|
||||
|
||||
self.stdscr = stdscr
|
||||
self.ascii = ascii
|
||||
self.config = config
|
||||
self.loader = LoadScreen(self)
|
||||
self._display = None
|
||||
|
||||
@property
|
||||
def up_arrow(self):
|
||||
symbol = '^' if self.ascii else '▲'
|
||||
symbol = '^' if self.config['ascii'] else '▲'
|
||||
attr = curses.A_BOLD | Color.GREEN
|
||||
return symbol, attr
|
||||
|
||||
@property
|
||||
def down_arrow(self):
|
||||
symbol = 'v' if self.ascii else '▼'
|
||||
symbol = 'v' if self.config['ascii'] else '▼'
|
||||
attr = curses.A_BOLD | Color.RED
|
||||
return symbol, attr
|
||||
|
||||
@property
|
||||
def neutral_arrow(self):
|
||||
symbol = 'o' if self.ascii else '•'
|
||||
symbol = 'o' if self.config['ascii'] else '•'
|
||||
attr = curses.A_BOLD
|
||||
return symbol, attr
|
||||
|
||||
@@ -75,7 +75,7 @@ class Terminal(object):
|
||||
|
||||
@property
|
||||
def guilded(self):
|
||||
symbol = '*' if self.ascii else '✪'
|
||||
symbol = '*' if self.config['ascii'] else '✪'
|
||||
attr = curses.A_BOLD | Color.YELLOW
|
||||
return symbol, attr
|
||||
|
||||
@@ -215,7 +215,7 @@ class Terminal(object):
|
||||
if isinstance(string, six.text_type):
|
||||
string = unescape(string)
|
||||
|
||||
if self.ascii:
|
||||
if self.config['ascii']:
|
||||
if isinstance(string, six.binary_type):
|
||||
string = string.decode('utf-8')
|
||||
string = string.encode('ascii', 'replace')
|
||||
|
||||
@@ -18,9 +18,9 @@ except ImportError:
|
||||
import mock
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_objects_load_screen(terminal, stdscr, ascii):
|
||||
terminal.ascii = ascii
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_objects_load_screen(terminal, stdscr, use_ascii):
|
||||
terminal.config['ascii'] = use_ascii
|
||||
|
||||
# Ensure the thread is properly started/stopped
|
||||
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
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_objects_load_screen_exception_unhandled(terminal, stdscr, ascii):
|
||||
terminal.ascii = ascii
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_objects_load_screen_exception_unhandled(terminal, stdscr, use_ascii):
|
||||
terminal.config['ascii'] = use_ascii
|
||||
|
||||
# Raising an exception should clean up the loader properly
|
||||
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()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_objects_load_screen_exception_handled(terminal, stdscr, ascii):
|
||||
terminal.ascii = ascii
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_objects_load_screen_exception_handled(terminal, stdscr, use_ascii):
|
||||
terminal.config['ascii'] = use_ascii
|
||||
|
||||
# Raising a handled exception should get stored on the loaders
|
||||
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)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_objects_load_screen_exception_not_caught(terminal, stdscr, ascii):
|
||||
terminal.ascii = ascii
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_objects_load_screen_exception_not_caught(terminal, stdscr, use_ascii):
|
||||
terminal.config['ascii'] = use_ascii
|
||||
|
||||
with pytest.raises(KeyboardInterrupt):
|
||||
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
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_objects_load_screen_keyboard_interrupt(terminal, stdscr, ascii):
|
||||
terminal.ascii = ascii
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_objects_load_screen_keyboard_interrupt(terminal, stdscr, use_ascii):
|
||||
terminal.config['ascii'] = use_ascii
|
||||
|
||||
# Raising a KeyboardInterrupt should be also be stored
|
||||
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)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_objects_load_screen_escape(terminal, stdscr, ascii):
|
||||
terminal.ascii = ascii
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_objects_load_screen_escape(terminal, stdscr, use_ascii):
|
||||
terminal.config['ascii'] = use_ascii
|
||||
|
||||
stdscr.getch.return_value = terminal.ESCAPE
|
||||
|
||||
@@ -109,9 +109,9 @@ def test_objects_load_screen_escape(terminal, stdscr, ascii):
|
||||
assert kill.called
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_objects_load_screen_initial_delay(terminal, stdscr, ascii):
|
||||
terminal.ascii = ascii
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_objects_load_screen_initial_delay(terminal, stdscr, use_ascii):
|
||||
terminal.config['ascii'] = use_ascii
|
||||
|
||||
# If we don't reach the initial delay nothing should be drawn
|
||||
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
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_objects_load_screen_nested(terminal, ascii):
|
||||
terminal.ascii = ascii
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_objects_load_screen_nested(terminal, use_ascii):
|
||||
terminal.config['ascii'] = use_ascii
|
||||
|
||||
with terminal.loader(message='Outer'):
|
||||
with terminal.loader(message='Inner'):
|
||||
@@ -134,9 +134,9 @@ def test_objects_load_screen_nested(terminal, ascii):
|
||||
assert not terminal.loader._animator.is_alive()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_objects_load_screen_nested_complex(terminal, stdscr, ascii):
|
||||
terminal.ascii = ascii
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_objects_load_screen_nested_complex(terminal, stdscr, use_ascii):
|
||||
terminal.config['ascii'] = use_ascii
|
||||
|
||||
with terminal.loader(message='Outer') as outer_loader:
|
||||
assert outer_loader.depth == 1
|
||||
|
||||
@@ -51,7 +51,7 @@ def test_terminal_properties(terminal, config):
|
||||
assert terminal.get_arrow(None) is not None
|
||||
assert terminal.get_arrow(True) 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.MIN_HEIGHT is not None
|
||||
@@ -93,7 +93,7 @@ def test_terminal_functions(terminal):
|
||||
|
||||
def test_terminal_clean_ascii(terminal):
|
||||
|
||||
terminal.ascii = True
|
||||
terminal.config['ascii'] = True
|
||||
|
||||
# unicode returns ascii
|
||||
text = terminal.clean('hello ❤')
|
||||
@@ -113,7 +113,7 @@ def test_terminal_clean_ascii(terminal):
|
||||
|
||||
def test_terminal_clean_unicode(terminal):
|
||||
|
||||
terminal.ascii = False
|
||||
terminal.config['ascii'] = False
|
||||
|
||||
# unicode returns utf-8
|
||||
text = terminal.clean('hello ❤')
|
||||
@@ -146,20 +146,20 @@ def test_terminal_clean_ncols(terminal):
|
||||
assert text.decode('utf-8') == 'hell'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_terminal_clean_unescape_html(terminal, ascii):
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_terminal_clean_unescape_html(terminal, use_ascii):
|
||||
|
||||
# HTML characters get decoded
|
||||
terminal.ascii = ascii
|
||||
terminal.config['ascii'] = use_ascii
|
||||
text = terminal.clean('<')
|
||||
assert isinstance(text, six.binary_type)
|
||||
assert text.decode('ascii' if ascii else 'utf-8') == '<'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_terminal_add_line(terminal, stdscr, ascii):
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_terminal_add_line(terminal, stdscr, use_ascii):
|
||||
|
||||
terminal.ascii = ascii
|
||||
terminal.config['ascii'] = use_ascii
|
||||
|
||||
terminal.add_line(stdscr, 'hello')
|
||||
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()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_show_notification(terminal, stdscr, ascii):
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_show_notification(terminal, stdscr, use_ascii):
|
||||
|
||||
terminal.ascii = ascii
|
||||
terminal.config['ascii'] = use_ascii
|
||||
|
||||
# The whole message should fit in 40x80
|
||||
text = HELP.strip().splitlines()
|
||||
@@ -198,10 +198,10 @@ def test_show_notification(terminal, stdscr, ascii):
|
||||
assert stdscr.subwin.addstr.call_count == 13
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_text_input(terminal, stdscr, ascii):
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_text_input(terminal, stdscr, use_ascii):
|
||||
|
||||
terminal.ascii = ascii
|
||||
terminal.config['ascii'] = use_ascii
|
||||
stdscr.nlines = 1
|
||||
|
||||
# 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
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_prompt_input(terminal, stdscr, ascii):
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
def test_prompt_input(terminal, stdscr, use_ascii):
|
||||
|
||||
terminal.ascii = ascii
|
||||
terminal.config['ascii'] = use_ascii
|
||||
window = stdscr.derwin()
|
||||
|
||||
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
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ascii', [True, False])
|
||||
def test_open_editor(terminal, ascii):
|
||||
@pytest.mark.parametrize('use_ascii', [True, False])
|
||||
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! ❤')
|
||||
data = {'filename': None}
|
||||
|
||||
Reference in New Issue
Block a user