diff options
| author | 2020-06-26 03:22:30 -0400 | |
|---|---|---|
| committer | 2020-06-26 03:34:38 -0400 | |
| commit | 77ce4c88695ca748059a7076de88d5b42b37d5f5 (patch) | |
| tree | a98ca1ad4a2159a33407024d3a316c30f0a42df7 | |
| parent | Correctly pass scale_to_size in LinePaginator.paginate() (diff) | |
In LinePaginator, truncate words that exceed scale_to_size
| -rw-r--r-- | bot/pagination.py | 11 | ||||
| -rw-r--r-- | tests/bot/test_pagination.py | 12 | 
2 files changed, 11 insertions, 12 deletions
| diff --git a/bot/pagination.py b/bot/pagination.py index 746ec3696..cd602c715 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -88,11 +88,9 @@ class LinePaginator(Paginator):          if len(line) > (max_chars := self.max_size - len(self.prefix) - 2):              if len(line) > self.scale_to_size:                  line, remaining_words = self._split_remaining_words(line, max_chars) -                # If line still exceeds scale_to_size, we were unable to split into a second -                # page without truncating.                  if len(line) > self.scale_to_size: -                    raise RuntimeError(f'Line exceeds maximum scale_to_size {self.scale_to_size}' -                                       ' and could not be split.') +                    log.debug("Could not continue to next page, truncating line.") +                    line = line[:self.scale_to_size]          if self.max_lines is not None and self._linecount >= self.max_lines:              log.debug("max_lines exceeded, creating new page.") @@ -144,11 +142,14 @@ class LinePaginator(Paginator):                      reduced_words.append(word)                      reduced_char_count += len(word) + 1                  else: +                    # If reduced_words is empty, we were unable to split the words across pages +                    if not reduced_words: +                        return line, None                      is_full = True                      remaining_words.append(word)              else:                  remaining_words.append(word) -         +          return (              " ".join(reduced_words),              continuation_header + " ".join(remaining_words) if remaining_words else None diff --git a/tests/bot/test_pagination.py b/tests/bot/test_pagination.py index 74896f010..ce880d457 100644 --- a/tests/bot/test_pagination.py +++ b/tests/bot/test_pagination.py @@ -39,13 +39,11 @@ class LinePaginatorTests(TestCase):          self.paginator.add_line('z')          self.assertEqual(len(self.paginator._pages), 1) -    def test_add_line_raises_on_very_long_words(self): -        """`add_line` should raise if a single long word is added that exceeds `scale_to_size`. - -        Note: truncation is also a potential option, but this should not occur from normal usage. -        """ -        with self.assertRaises(RuntimeError): -            self.paginator.add_line('x' * (self.paginator.scale_to_size + 1)) +    def test_add_line_truncates_very_long_words(self): +        """`add_line` should truncate if a single long word exceeds `scale_to_size`.""" +        self.paginator.add_line('x' * (self.paginator.scale_to_size + 1)) +        # Note: item at index 1 is the truncated line, index 0 is prefix +        self.assertEqual(self.paginator._current_page[1], 'x' * self.paginator.scale_to_size)  class ImagePaginatorTests(TestCase): | 
