From 4e00011c78cf684ac60e1b7ddf1011bc36ec5436 Mon Sep 17 00:00:00 2001 From: John Helmert III Date: Fri, 24 Apr 2020 20:15:19 -0500 Subject: [PATCH] Create format_list once per SubredditPage --- tests/test_subreddit.py | 3 +++ tuir/subreddit_page.py | 33 +++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/tests/test_subreddit.py b/tests/test_subreddit.py index a5ca56d..f168c40 100644 --- a/tests/test_subreddit.py +++ b/tests/test_subreddit.py @@ -781,6 +781,7 @@ def test_subreddit_page__draw_item_format(terminal, subreddit_page): # Loop through each individual format specifier and check for the desired behavior. for fmt in filter(None, '%i %t %s %v %c %r %e %a %S %u %U %A %h %T %g %n %f %F'.split()): subreddit_page.config['subreddit_format'] = fmt + subreddit_page.FORMAT_LIST = subreddit_page._create_format_list() subreddit_page._draw_item_format(win, data, 0, 0) try: @@ -829,6 +830,7 @@ def test_subreddit_page__draw_item_format(terminal, subreddit_page): # Ensure spaces aren't printed consecutively if data is absent data['gold'] = 0 subreddit_page.config['subreddit_format'] = ' %g ' + subreddit_page.FORMAT_LIST = subreddit_page._create_format_list() subreddit_page._draw_item_format(win, data, 0, 0) assert terminal.add_line.call_count == 1 @@ -837,6 +839,7 @@ def test_subreddit_page__draw_item_format(terminal, subreddit_page): # Test for correct handling of separators subreddit_page.config['subreddit_format'] = ' | ' + subreddit_page.FORMAT_LIST = subreddit_page._create_format_list() subreddit_page._draw_item_format(win, data, 0, 0) # Should be called thrice - ' ', '|', ' ' diff --git a/tuir/subreddit_page.py b/tuir/subreddit_page.py index c8936d1..687a96d 100644 --- a/tuir/subreddit_page.py +++ b/tuir/subreddit_page.py @@ -26,8 +26,9 @@ class SubredditPage(Page): # Format separators, used by _create_format and _draw_item_format for # attribute handling logic - FORMAT_SEP = r"<>/{}[]()|_-~" + FORMAT_SEP = r'<>/{}[]()|_-~' + FORMAT_LIST = '' name = 'subreddit' def __init__(self, reddit, term, config, oauth, name): @@ -42,6 +43,23 @@ class SubredditPage(Page): self.nav = Navigator(self.content.get) self.toggled_subreddit = None + self.FORMAT_LIST = self._create_format_list() + + # Split this out to a function mostly to simplify testing + def _create_format_list(self): + if self.config['subreddit_format']: + # Split the list between %., newlines, and separator characters to + # treat them separately + format_list = re.split(r'(%.|[\n' + re.escape(self.FORMAT_SEP) + '])', + self.config['subreddit_format'], re.DOTALL) + + # Clean the list of null items. We don't need to join this list + # together again, so this is safe. + # https://stackoverflow.com/q/2197451 + return [item for item in format_list if item != ''] + else: + return None + def handle_selected_page(self): """ Open all selected pages in subwindows except other subreddit pages. @@ -278,20 +296,11 @@ class SubredditPage(Page): def _draw_item_format(self, win, data, valid_rows, offset): first = True - # Split the list between %., newlines, and separator characters to - # treat them separately - format_list = re.split(r'(%.|[\n' + re.escape(self.FORMAT_SEP) + '])', self.config['subreddit_format'], re.DOTALL) - - # Clean the list of null items. We don't need to join this list - # together again, so this is safe. - # https://stackoverflow.com/q/2197451 - format_list = [item for item in format_list if item != ''] - # Remember whether or not the last character printed was a space. If it # was, printing more spaces should be avoided. last_was_space = False - for item in format_list: + for item in self.FORMAT_LIST: col = 1 if first else None # We don't want to print consecutive spaces, so check if a space @@ -598,7 +607,7 @@ class SubredditPage(Page): valid_rows = range(0, n_rows) offset = 0 if not inverted else - (data['n_rows'] - n_rows) - # FIXME - this assumes default doesn't have a subreddit_format. In the + # TODO - this assumes default doesn't have a subreddit_format. In the # future it should, and when it does it will break this if if self.config['subreddit_format']: self._draw_item_format(win, data, valid_rows, offset)