Browse Source

[commands] Add linesep kwarg to Paginator

pull/6325/head
PikalaxALT 4 years ago
committed by GitHub
parent
commit
b7c7200f4d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      discord/ext/commands/help.py

26
discord/ext/commands/help.py

@ -79,18 +79,22 @@ class Paginator:
The suffix appended at the end of every page. e.g. three backticks. The suffix appended at the end of every page. e.g. three backticks.
max_size: :class:`int` max_size: :class:`int`
The maximum amount of codepoints allowed in a page. 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.prefix = prefix
self.suffix = suffix self.suffix = suffix
self.max_size = max_size self.max_size = max_size
self.linesep = linesep
self.clear() self.clear()
def clear(self): def clear(self):
"""Clears the paginator to have no pages.""" """Clears the paginator to have no pages."""
if self.prefix is not None: if self.prefix is not None:
self._current_page = [self.prefix] self._current_page = [self.prefix]
self._count = len(self.prefix) + 1 # prefix + newline self._count = len(self.prefix) + self._linesep_len # prefix + newline
else: else:
self._current_page = [] self._current_page = []
self._count = 0 self._count = 0
@ -104,6 +108,10 @@ class Paginator:
def _suffix_len(self): def _suffix_len(self):
return len(self.suffix) if self.suffix else 0 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): def add_line(self, line='', *, empty=False):
"""Adds a line to the current page. """Adds a line to the current page.
@ -122,29 +130,29 @@ class Paginator:
RuntimeError RuntimeError
The line was too big for the current :attr:`max_size`. 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: if len(line) > max_page_size:
raise RuntimeError('Line exceeds maximum page size %s' % (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.close_page()
self._count += len(line) + 1 self._count += len(line) + self._linesep_len
self._current_page.append(line) self._current_page.append(line)
if empty: if empty:
self._current_page.append('') self._current_page.append('')
self._count += 1 self._count += self._linesep_len
def close_page(self): def close_page(self):
"""Prematurely terminate a page.""" """Prematurely terminate a page."""
if self.suffix is not None: if self.suffix is not None:
self._current_page.append(self.suffix) 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: if self.prefix is not None:
self._current_page = [self.prefix] self._current_page = [self.prefix]
self._count = len(self.prefix) + 1 # prefix + newline self._count = len(self.prefix) + self._linesep_len # prefix + linesep
else: else:
self._current_page = [] self._current_page = []
self._count = 0 self._count = 0
@ -162,7 +170,7 @@ class Paginator:
return self._pages return self._pages
def __repr__(self): def __repr__(self):
fmt = '<Paginator prefix: {0.prefix} suffix: {0.suffix} max_size: {0.max_size} count: {0._count}>' fmt = '<Paginator prefix: {0.prefix!r} suffix: {0.suffix!r} linesep: {0.linesep!r} max_size: {0.max_size} count: {0._count}>'
return fmt.format(self) return fmt.format(self)
def _not_overriden(f): def _not_overriden(f):

Loading…
Cancel
Save