1
0
mirror of https://github.com/ihabunek/toot.git synced 2024-06-30 06:35:24 +00:00

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

View File

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