From f7ab2afa1fcdb9d3b2cda4bd1f4a8c8c7035f296 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Wed, 5 Aug 2020 09:20:00 +0000 Subject: [PATCH] attempt at supporting emoji --- rich/rule.py | 17 ++++++++++++----- tests/test_rule.py | 1 + 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/rich/rule.py b/rich/rule.py index f7a18764..8a2ba93b 100644 --- a/rich/rule.py +++ b/rich/rule.py @@ -1,6 +1,6 @@ from typing import Union -from .cells import cell_len +from .cells import cell_len, get_character_cell_size as get_char_size from .console import Console, ConsoleOptions, RenderResult from .jupyter import JupyterMixin from .style import Style @@ -44,8 +44,15 @@ class Rule(JupyterMixin): characters = self.characters or "─" + if cell_len(characters) == 1: + chars_len = get_char_size(characters) + else: + chars_len = 0 + for i in list(characters): + chars_len += get_char_size(i) + print(chars_len) if not self.title: - yield Text(characters * (width // len(characters)), self.style) + yield Text(characters * (width // chars_len), self.style) else: if isinstance(self.title, Text): title_text = self.title @@ -59,10 +66,10 @@ class Rule(JupyterMixin): title_text = title_text.tabs_to_spaces() rule_text = Text(end=self.end) side_width = (width - cell_len(title_text.plain)) // 2 - if cell_len(characters) == 1: + if chars_len == 1: side = Text(characters * side_width) else: - side = Text(characters * (side_width // (cell_len(characters) - 1))) + side = Text(characters * (side_width // (chars_len - 1))) side.truncate(side_width - 1) rule_text.append(str(side) + " ", self.style) rule_text.append(title_text) @@ -81,4 +88,4 @@ if __name__ == "__main__": # pragma: no cover except IndexError: text = "Hello" console = Console(width=16) - console.print(Rule(title="foo")) + console.print(Rule(title="foo", characters="😊😊")) diff --git a/tests/test_rule.py b/tests/test_rule.py index d9d11903..9c570cb5 100644 --- a/tests/test_rule.py +++ b/tests/test_rule.py @@ -26,6 +26,7 @@ def test_rule(): assert result == expected +# doesn't work for now def test_rule_cjk(): console = Console( width=16,