Localize text wrapping - toot - Unnamed repository; edit this file 'description… | |
Log | |
Files | |
Refs | |
LICENSE | |
--- | |
commit 431475161052bc6d368ad5566f42ce8f7ebf1e0d | |
parent fba3b78ff6d5f1fc9f8f8304e7ef1f4a100282d9 | |
Author: Ivan Habunek <[email protected]> | |
Date: Sat, 20 Jan 2018 13:43:21 +0100 | |
Localize text wrapping | |
Diffstat: | |
toot/ui/app.py | 24 +++++++----------------- | |
toot/ui/utils.py | 49 +++++++++++++++++++++---------- | |
2 files changed, 40 insertions(+), 33 deletions(-) | |
--- | |
diff --git a/toot/ui/app.py b/toot/ui/app.py | |
@@ -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() | |
diff --git a/toot/ui/utils.py b/toot/ui/utils.py | |
@@ -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, start_y, padding, default_color): | |
+ height, width = window.getmaxyx() | |
+ text_width = width - 2 * padding | |
-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) | |
+ 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 + dy + 1 | |
+ return y + 1 |