From b7c7200f4d791bc6bdc74c0f731cd644b60c55b5 Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Sun, 17 Jan 2021 00:15:36 -0500 Subject: [PATCH] [commands] Add linesep kwarg to Paginator --- discord/ext/commands/help.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/discord/ext/commands/help.py b/discord/ext/commands/help.py index 82708571d..2dd94fcd6 100644 --- a/discord/ext/commands/help.py +++ b/discord/ext/commands/help.py @@ -79,18 +79,22 @@ class Paginator: The suffix appended at the end of every page. e.g. three backticks. max_size: :class:`int` The maximum amount of codepoints allowed in a page. + linesep: :class:`str` + The character string inserted between lines. e.g. a newline character. + .. versionadded:: 1.7 """ - def __init__(self, prefix='```', suffix='```', max_size=2000): + def __init__(self, prefix='```', suffix='```', max_size=2000, linesep='\n'): self.prefix = prefix self.suffix = suffix self.max_size = max_size + self.linesep = linesep self.clear() def clear(self): """Clears the paginator to have no pages.""" if self.prefix is not None: self._current_page = [self.prefix] - self._count = len(self.prefix) + 1 # prefix + newline + self._count = len(self.prefix) + self._linesep_len # prefix + newline else: self._current_page = [] self._count = 0 @@ -104,6 +108,10 @@ class Paginator: def _suffix_len(self): return len(self.suffix) if self.suffix else 0 + @property + def _linesep_len(self): + return len(self.linesep) + def add_line(self, line='', *, empty=False): """Adds a line to the current page. @@ -122,29 +130,29 @@ class Paginator: RuntimeError The line was too big for the current :attr:`max_size`. """ - max_page_size = self.max_size - self._prefix_len - self._suffix_len - 2 + max_page_size = self.max_size - self._prefix_len - self._suffix_len - 2 * self._linesep_len if len(line) > max_page_size: raise RuntimeError('Line exceeds maximum page size %s' % (max_page_size)) - if self._count + len(line) + 1 > self.max_size - self._suffix_len: + if self._count + len(line) + self._linesep_len > self.max_size - self._suffix_len: self.close_page() - self._count += len(line) + 1 + self._count += len(line) + self._linesep_len self._current_page.append(line) if empty: self._current_page.append('') - self._count += 1 + self._count += self._linesep_len def close_page(self): """Prematurely terminate a page.""" if self.suffix is not None: self._current_page.append(self.suffix) - self._pages.append('\n'.join(self._current_page)) + self._pages.append(self.linesep.join(self._current_page)) if self.prefix is not None: self._current_page = [self.prefix] - self._count = len(self.prefix) + 1 # prefix + newline + self._count = len(self.prefix) + self._linesep_len # prefix + linesep else: self._current_page = [] self._count = 0 @@ -162,7 +170,7 @@ class Paginator: return self._pages def __repr__(self): - fmt = '' + fmt = '' return fmt.format(self) def _not_overriden(f):