Create format_list once per SubredditPage

This commit is contained in:
John Helmert III
2020-04-24 20:15:19 -05:00
parent 790f505d2f
commit 4e00011c78
2 changed files with 24 additions and 12 deletions

View File

@@ -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. # 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()): 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.config['subreddit_format'] = fmt
subreddit_page.FORMAT_LIST = subreddit_page._create_format_list()
subreddit_page._draw_item_format(win, data, 0, 0) subreddit_page._draw_item_format(win, data, 0, 0)
try: 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 # Ensure spaces aren't printed consecutively if data is absent
data['gold'] = 0 data['gold'] = 0
subreddit_page.config['subreddit_format'] = ' %g ' subreddit_page.config['subreddit_format'] = ' %g '
subreddit_page.FORMAT_LIST = subreddit_page._create_format_list()
subreddit_page._draw_item_format(win, data, 0, 0) subreddit_page._draw_item_format(win, data, 0, 0)
assert terminal.add_line.call_count == 1 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 # Test for correct handling of separators
subreddit_page.config['subreddit_format'] = ' | ' subreddit_page.config['subreddit_format'] = ' | '
subreddit_page.FORMAT_LIST = subreddit_page._create_format_list()
subreddit_page._draw_item_format(win, data, 0, 0) subreddit_page._draw_item_format(win, data, 0, 0)
# Should be called thrice - ' ', '|', ' ' # Should be called thrice - ' ', '|', ' '

View File

@@ -26,8 +26,9 @@ class SubredditPage(Page):
# Format separators, used by _create_format and _draw_item_format for # Format separators, used by _create_format and _draw_item_format for
# attribute handling logic # attribute handling logic
FORMAT_SEP = r"<>/{}[]()|_-~" FORMAT_SEP = r'<>/{}[]()|_-~'
FORMAT_LIST = ''
name = 'subreddit' name = 'subreddit'
def __init__(self, reddit, term, config, oauth, name): def __init__(self, reddit, term, config, oauth, name):
@@ -42,6 +43,23 @@ class SubredditPage(Page):
self.nav = Navigator(self.content.get) self.nav = Navigator(self.content.get)
self.toggled_subreddit = None 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): def handle_selected_page(self):
""" """
Open all selected pages in subwindows except other subreddit pages. 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): def _draw_item_format(self, win, data, valid_rows, offset):
first = True 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 # Remember whether or not the last character printed was a space. If it
# was, printing more spaces should be avoided. # was, printing more spaces should be avoided.
last_was_space = False last_was_space = False
for item in format_list: for item in self.FORMAT_LIST:
col = 1 if first else None col = 1 if first else None
# We don't want to print consecutive spaces, so check if a space # 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) valid_rows = range(0, n_rows)
offset = 0 if not inverted else - (data['n_rows'] - 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 # future it should, and when it does it will break this if
if self.config['subreddit_format']: if self.config['subreddit_format']:
self._draw_item_format(win, data, valid_rows, offset) self._draw_item_format(win, data, valid_rows, offset)