Localize text wrapping

This commit is contained in:
Ivan Habunek 2018-01-20 13:43:21 +01:00
parent fba3b78ff6
commit 4314751610
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
2 changed files with 40 additions and 33 deletions

View File

@ -2,8 +2,6 @@
import webbrowser
from textwrap import wrap
from toot.exceptions import ConsoleError
from toot.ui.utils import draw_horizontal_divider, draw_lines
from toot.utils import format_content, trunc
@ -181,8 +179,7 @@ class StatusDetailWindow:
if status['sensitive']:
for line in status['spoiler_text']:
for wrapped in wrap(line, text_width):
yield wrapped
yield line
yield
if status['sensitive'] and not status['show_sensitive']:
@ -190,28 +187,21 @@ class StatusDetailWindow:
return
for line in status['content']:
wrapped_lines = wrap(line, text_width) if line else ['']
for wrapped_line in wrapped_lines:
yield wrapped_line.ljust(text_width)
yield line
if status['media_attachments']:
yield
yield "Media:"
for attachment in status['media_attachments']:
url = attachment['text_url'] or attachment['url']
for line in wrap(url, text_width):
yield line
yield attachment['text_url'] or attachment['url']
def footer_lines(self, status):
text_width = self.width - 4
if status['url'] is not None:
for line in wrap(status['url'], text_width):
yield line
yield status['url']
if status['boosted_by']:
acct = status['boosted_by']['acct']
yield "Boosted by @{}".format(acct), Color.BLUE
yield "Boosted by @{}".format(acct), Color.GREEN
def draw(self, status):
self.window.erase()
@ -223,9 +213,9 @@ class StatusDetailWindow:
content = self.content_lines(status)
footer = self.footer_lines(status)
y = draw_lines(self.window, content, 2, 1, Color.WHITE)
y = draw_lines(self.window, content, 1, 2, Color.WHITE)
draw_horizontal_divider(self.window, y)
draw_lines(self.window, footer, 2, y + 1, Color.WHITE)
draw_lines(self.window, footer, y + 1, 2, Color.WHITE)
self.window.refresh()

View File

@ -1,3 +1,6 @@
from textwrap import wrap
def draw_horizontal_divider(window, y):
height, width = window.getmaxyx()
@ -7,22 +10,36 @@ def draw_horizontal_divider(window, y):
window.addstr(y, 0, line)
def enumerate_lines(generator, default_color):
for y, item in enumerate(generator):
if isinstance(item, tuple) and len(item) == 2:
yield y, item[0], item[1]
elif isinstance(item, str):
yield y, item, default_color
elif item is None:
yield y, "", default_color
else:
raise ValueError("Wrong yield in generator")
def enumerate_lines(lines, text_width, default_color):
def parse_line(line):
if isinstance(line, tuple) and len(line) == 2:
return line[0], line[1]
elif isinstance(line, str):
return line, default_color
elif line is None:
return "", default_color
raise ValueError("Wrong yield in generator")
def wrap_lines(lines):
for line in lines:
line, color = parse_line(line)
if line:
for wrapped in wrap(line, text_width):
yield wrapped, color
else:
yield "", color
return enumerate(wrap_lines(lines))
def draw_lines(window, lines, x, y, default_color):
height, _ = window.getmaxyx()
for dy, line, color in enumerate_lines(lines, default_color):
if y + dy < height - 1:
window.addstr(y + dy, x, line, color)
def draw_lines(window, lines, start_y, padding, default_color):
height, width = window.getmaxyx()
text_width = width - 2 * padding
return y + dy + 1
for dy, (line, color) in enumerate_lines(lines, text_width, default_color):
y = start_y + dy
if y < height - 1:
window.addstr(y, padding, line.ljust(text_width), color)
return y + 1