Employ wcstring utils to improve rendering

This commit is contained in:
Ivan Habunek 2019-02-14 15:47:40 +01:00
parent 8a3ff94e47
commit 996228d224
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
3 changed files with 12 additions and 16 deletions

View File

@ -9,7 +9,9 @@ from itertools import chain
from itertools import zip_longest from itertools import zip_longest
from textwrap import wrap, TextWrapper from textwrap import wrap, TextWrapper
from toot.utils import format_content, get_text, pad from toot.utils import format_content, get_text
from toot.wcstring import pad
START_CODES = { START_CODES = {
'red': '\033[31m', 'red': '\033[31m',

View File

@ -7,13 +7,13 @@ from toot import __version__
from toot.exceptions import ConsoleError from toot.exceptions import ConsoleError
from toot.ui.parsers import parse_status from toot.ui.parsers import parse_status
from toot.ui.utils import draw_horizontal_divider, draw_lines from toot.ui.utils import draw_horizontal_divider, draw_lines
from toot.utils import trunc from toot.wcstring import fit_text
# Attempt to load curses, which is not available on windows # Attempt to load curses, which is not available on windows
try: try:
import curses import curses
import curses.panel import curses.panel
except ImportError as e: except ImportError:
raise ConsoleError("Curses is not available on this platform") raise ConsoleError("Curses is not available on this platform")
@ -64,12 +64,12 @@ class FooterWindow:
def draw_status(self, selected, count): def draw_status(self, selected, count):
text = "Showing toot {} of {}".format(selected + 1, count) text = "Showing toot {} of {}".format(selected + 1, count)
text = trunc(text, self.width - 1).ljust(self.width - 1) text = fit_text(text, self.width)
self.window.addstr(0, 0, text, Color.WHITE_ON_BLUE | curses.A_BOLD) self.window.addstr(0, 0, text, Color.WHITE_ON_BLUE | curses.A_BOLD)
self.window.refresh() self.window.refresh()
def draw_message(self, text, color): def draw_message(self, text, color):
text = trunc(text, self.width - 1).ljust(self.width - 1) text = fit_text(text, self.width - 1)
self.window.addstr(1, 0, text, color) self.window.addstr(1, 0, text, color)
self.window.refresh() self.window.refresh()
@ -121,8 +121,8 @@ class StatusListWindow:
color = Color.GREEN if highlight else Color.WHITE color = Color.GREEN if highlight else Color.WHITE
trunc_width = width - 15 trunc_width = width - 15
acct = trunc("@" + status['account']['acct'], trunc_width).ljust(trunc_width) acct = fit_text("@" + status['account']['acct'], trunc_width)
display_name = trunc(status['account']['display_name'], trunc_width).ljust(trunc_width) display_name = fit_text(status['account']['display_name'], trunc_width)
if status['account']['display_name']: if status['account']['display_name']:
self.pad.addstr(offset + 1, 14, display_name, color) self.pad.addstr(offset + 1, 14, display_name, color)
@ -134,12 +134,6 @@ class StatusListWindow:
self.pad.addstr(offset + 1, 1, " " + date.ljust(12), color) self.pad.addstr(offset + 1, 1, " " + date.ljust(12), color)
self.pad.addstr(offset + 2, 1, " " + time.ljust(12), color) self.pad.addstr(offset + 2, 1, " " + time.ljust(12), color)
# Redraw box borders to mitigate unicode overflow issues
self.pad.addch(offset + 1, 0, "")
self.pad.addch(offset + 2, 0, "")
self.pad.addch(offset + 1, width - 1, "")
self.pad.addch(offset + 2, width - 1, "")
if draw_divider: if draw_divider:
draw_horizontal_divider(self.pad, offset + 3) draw_horizontal_divider(self.pad, offset + 3)

View File

@ -1,6 +1,6 @@
import re import re
from textwrap import wrap from toot.wcstring import fit_text, wc_wrap
def draw_horizontal_divider(window, y): def draw_horizontal_divider(window, y):
@ -27,7 +27,7 @@ def enumerate_lines(lines, text_width, default_color):
for line in lines: for line in lines:
line, color = parse_line(line) line, color = parse_line(line)
if line: if line:
for wrapped in wrap(line, text_width): for wrapped in wc_wrap(line, text_width):
yield wrapped, color yield wrapped, color
else: else:
yield "", color yield "", color
@ -53,7 +53,7 @@ def draw_lines(window, lines, start_y, padding, default_color):
for dy, (line, color) in enumerate_lines(lines, text_width, default_color): for dy, (line, color) in enumerate_lines(lines, text_width, default_color):
y = start_y + dy y = start_y + dy
if y < height - 1: if y < height - 1:
window.addstr(y, padding, line.ljust(text_width), color) window.addstr(y, padding, fit_text(line, text_width), color)
highlight_hashtags(window, y, padding, line) highlight_hashtags(window, y, padding, line)
return y + 1 return y + 1