only strip \n in paginator

release-1.0.0
DustyP 8 years ago
parent ab14f9da3c
commit 59a7bb37c6

@ -1,4 +1,4 @@
from typing import Dict, List from typing import Dict
import discord import discord
from discord.ext import commands from discord.ext import commands
@ -8,7 +8,7 @@ import json
import aiohttp import aiohttp
from googleapiclient.discovery import build from googleapiclient.discovery import build
from concurrent import futures from concurrent import futures
from shared_libs import database from src.shared_libs import database
log_format = '{asctime}.{msecs:03.0f}|{levelname:<8}|{name}::{message}' log_format = '{asctime}.{msecs:03.0f}|{levelname:<8}|{name}::{message}'
@ -52,19 +52,10 @@ class Geeksbot(commands.Bot):
self.bot_config = json.load(file) self.bot_config = json.load(file)
with open(f'{config_dir}{secrets_file}') as file: with open(f'{config_dir}{secrets_file}') as file:
self.bot_secrets = json.load(file) self.bot_secrets = json.load(file)
# with open(f'{config_dir}{profane_words_file}') as file:
# self.profane_words = file.readlines()
self.guild_config = {} self.guild_config = {}
self.infected = {} self.infected = {}
self.TOKEN = self.bot_secrets['token'] self.TOKEN = self.bot_secrets['token']
self.embed_color = discord.Colour.from_rgb(49, 107, 111) self.embed_color = discord.Colour.from_rgb(49, 107, 111)
# async def connect_db():
# return await asyncpg.create_pool(host=self.bot_secrets['db_con']['host'],
# database=self.bot_secrets['db_con']['db_name'],
# user=self.bot_secrets['db_con']['user'],
# password=self.bot_secrets['db_con']['password'],
# loop=asyncio.get_event_loop())
del self.bot_secrets['token'] del self.bot_secrets['token']
self.db_con = database.DatabaseConnection(**self.bot_secrets['db_con']) self.db_con = database.DatabaseConnection(**self.bot_secrets['db_con'])
self.default_prefix = 'g~' self.default_prefix = 'g~'

@ -99,8 +99,11 @@ class Paginator:
prefix: str='```md', prefix: str='```md',
suffix: str='```', suffix: str='```',
page_break: str='\uFFF8', page_break: str='\uFFF8',
max_line_length: int=100): field_break: str='\uFFF7',
_max_len = 1980 field_name_char: str='\uFFF6',
max_line_length: int=100,
embed=False):
_max_len = 6000 if embed else 1980
assert 0 < max_lines <= max_chars assert 0 < max_lines <= max_chars
assert 0 < max_line_length < 120 assert 0 < max_line_length < 120
@ -113,38 +116,77 @@ class Paginator:
self._page_break = page_break self._page_break = page_break
self._max_line_length = max_line_length self._max_line_length = max_line_length
self._pages = list() self._pages = list()
self._max_field_chars = 1014
self._max_field_name = 256
self._max_description = 2048
self._embed = embed
self._field_break = field_break
self._field_name_char = field_name_char
self._embed_title = ''
self._embed_description = ''
def set_embed_meta(self, title: str='\uFFF0', description: str='\uFFF0'):
if len(title) <= self._max_field_name:
self._embed_title = title
else:
raise RuntimeError('Provided Title is too long')
if len(description) <= self._max_description:
self._embed_description = description
else:
raise RuntimeError('Provided Description is too long')
def pages(self) -> typing.List[str]: def pages(self) -> typing.List[str]:
pages = list() pages = list()
page = '' _fields = list()
lines = 0 _page = ''
_lines = 0
_field_name = ''
_field_value = ''
def open_page(): def open_page():
nonlocal page, lines nonlocal _page, _lines
page = self._prefix _page = self._prefix
lines = 0 _lines = 0
def close_page(): def close_page():
nonlocal page, lines nonlocal _page, _lines
page += self._suffix _page += self._suffix
pages.append(page) pages.append(_page)
open_page() open_page()
open_page() open_page()
if not self._embed:
for part in [str(p) for p in self._parts]: for part in [str(p) for p in self._parts]:
if part == self._page_break: if part == self._page_break:
close_page() close_page()
new_chars = len(page) + len(part) new_chars = len(_page) + len(part)
if new_chars > self._max_chars: if new_chars > self._max_chars:
close_page() close_page()
elif (lines + (part.count('\n') + 1 or 1)) > self._max_lines: elif (_lines + (part.count('\n') + 1 or 1)) > self._max_lines:
close_page() close_page()
lines += (part.count('\n') + 1 or 1) _lines += (part.count('\n') + 1 or 1)
page += '\n' + part _page += '\n' + part
else:
def open_field(name: str):
_field_name = name
_field_value = '\uFFF0'
def close_field():
pass
for part in [str(p) for p in self._parts]:
if part == self._page_break:
close_page()
elif part == self._field_break:
close_field()
close_page() close_page()
self._pages = pages self._pages = pages
@ -173,7 +215,7 @@ class Paginator:
item = str(item) item = str(item)
i = 0 i = 0
if not keep_intact and not item == self._page_break: if not keep_intact and not item == self._page_break:
item_parts = item.strip().split('\n') item_parts = item.strip('\n').split('\n')
for part in item_parts: for part in item_parts:
if len(part) > self._max_line_length: if len(part) > self._max_line_length:
length = 0 length = 0

Loading…
Cancel
Save