From 8b4e24dfb3b2f5a038986af5038ac939ec43652b Mon Sep 17 00:00:00 2001 From: oppZ Date: Fri, 12 Jul 2024 00:25:49 +0200 Subject: [PATCH 1/5] Fix the size of the tab not being fixed after being justified on "fully" --- CHANGELOG.md | 1 + CONTRIBUTORS.md | 1 + rich/text.py | 8 ++++++++ 3 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1a042249..1dffd33fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Progress track thread is now a daemon thread https://github.com/Textualize/rich/pull/3402 - Fixed cached hash preservation upon clearing meta and links https://github.com/Textualize/rich/issues/2942 - Fixed overriding the `background_color` of `Syntax` not including padding https://github.com/Textualize/rich/issues/3295 +- The size of the tab (\t) was not fixed when justifying text "fully" https://github.com/Textualize/rich/issues/3424 ### Changed diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index edacc5885..52b886f47 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -85,3 +85,4 @@ The following people have contributed to the development of Rich: - [Pierro](https://github.com/xpierroz) - [Bernhard Wagner](https://github.com/bwagner) - [Aaron Beaudoin](https://github.com/AaronBeaudoin) +- [oppZ](https://github.com/oppZ) diff --git a/rich/text.py b/rich/text.py index 7b32967f7..d456d6c81 100644 --- a/rich/text.py +++ b/rich/text.py @@ -1235,9 +1235,17 @@ def wrap( for line in new_lines: line.rstrip_end(width) if wrap_justify: + new_lines = Lines( + Text(line.plain.replace(" " * tab_size, "\t" * tab_size)) + for line in new_lines._lines + ) new_lines.justify( console, width, justify=wrap_justify, overflow=wrap_overflow ) + new_lines = Lines( + Text(line.plain.replace("\t" * tab_size, " " * tab_size)) + for line in new_lines._lines + ) for line in new_lines: line.truncate(width, overflow=wrap_overflow) lines.extend(new_lines) From d6546e970701d769ef08f207e89e35f2894775d7 Mon Sep 17 00:00:00 2001 From: oppZ Date: Fri, 12 Jul 2024 01:10:43 +0200 Subject: [PATCH 2/5] Fix the case if tab_size would be 1 --- rich/text.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/rich/text.py b/rich/text.py index d456d6c81..05b9724de 100644 --- a/rich/text.py +++ b/rich/text.py @@ -1226,7 +1226,7 @@ def wrap( lines = Lines() for line in self.split(allow_blank=True): if "\t" in line: - line.expand_tabs(tab_size) + line = Text(line.plain.replace("\t", "\t" * tab_size)) if no_wrap: new_lines = Lines([line]) else: @@ -1235,17 +1235,12 @@ def wrap( for line in new_lines: line.rstrip_end(width) if wrap_justify: - new_lines = Lines( - Text(line.plain.replace(" " * tab_size, "\t" * tab_size)) - for line in new_lines._lines - ) new_lines.justify( console, width, justify=wrap_justify, overflow=wrap_overflow ) - new_lines = Lines( - Text(line.plain.replace("\t" * tab_size, " " * tab_size)) - for line in new_lines._lines - ) + new_lines = Lines( + Text(line.plain.replace("\t" * tab_size, " " * tab_size)) for line in new_lines._lines + ) for line in new_lines: line.truncate(width, overflow=wrap_overflow) lines.extend(new_lines) From 5d2d23a488f52d3c0c5aa5587dbc38ac24c381da Mon Sep 17 00:00:00 2001 From: oppZ Date: Fri, 12 Jul 2024 11:21:36 +0200 Subject: [PATCH 3/5] black --- rich/text.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rich/text.py b/rich/text.py index 05b9724de..0bc5b1568 100644 --- a/rich/text.py +++ b/rich/text.py @@ -1239,7 +1239,8 @@ def wrap( console, width, justify=wrap_justify, overflow=wrap_overflow ) new_lines = Lines( - Text(line.plain.replace("\t" * tab_size, " " * tab_size)) for line in new_lines._lines + Text(line.plain.replace("\t" * tab_size, " " * tab_size)) + for line in new_lines._lines ) for line in new_lines: line.truncate(width, overflow=wrap_overflow) From 802d00dcc4ce8772ea0a32bd81d169828fb115c2 Mon Sep 17 00:00:00 2001 From: oppZ Date: Fri, 12 Jul 2024 17:18:21 +0200 Subject: [PATCH 4/5] keep Text's attributes --- rich/text.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/rich/text.py b/rich/text.py index 0bc5b1568..e9dde49a8 100644 --- a/rich/text.py +++ b/rich/text.py @@ -1226,7 +1226,7 @@ def wrap( lines = Lines() for line in self.split(allow_blank=True): if "\t" in line: - line = Text(line.plain.replace("\t", "\t" * tab_size)) + line.plain = line.plain.replace("\t", "\t" * tab_size) if no_wrap: new_lines = Lines([line]) else: @@ -1238,11 +1238,8 @@ def wrap( new_lines.justify( console, width, justify=wrap_justify, overflow=wrap_overflow ) - new_lines = Lines( - Text(line.plain.replace("\t" * tab_size, " " * tab_size)) - for line in new_lines._lines - ) for line in new_lines: + line.plain = line.plain.replace("\t" * tab_size, " " * tab_size) line.truncate(width, overflow=wrap_overflow) lines.extend(new_lines) return lines From f11558439da0beeddd5540e9073bbef014efb6b6 Mon Sep 17 00:00:00 2001 From: oppZ Date: Sat, 13 Jul 2024 00:57:59 +0200 Subject: [PATCH 5/5] do not add spaces to "word" spaces instead --- rich/containers.py | 8 +++++++- rich/text.py | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/rich/containers.py b/rich/containers.py index 901ff8ba6..74493d0c2 100644 --- a/rich/containers.py +++ b/rich/containers.py @@ -151,9 +151,15 @@ def justify( index = 0 if spaces: while words_size + num_spaces < width: - spaces[len(spaces) - index - 1] += 1 + word_index = len(spaces) - index - 1 + while cell_len(words[word_index].plain) == 0 and not words_size == 0: + index = (index + 1) % len(spaces) + word_index = len(spaces) - index - 1 + + spaces[word_index] += 1 num_spaces += 1 index = (index + 1) % len(spaces) + tokens: List[Text] = [] for index, (word, next_word) in enumerate( zip_longest(words, words[1:]) diff --git a/rich/text.py b/rich/text.py index e9dde49a8..d899ce5a0 100644 --- a/rich/text.py +++ b/rich/text.py @@ -1226,7 +1226,8 @@ def wrap( lines = Lines() for line in self.split(allow_blank=True): if "\t" in line: - line.plain = line.plain.replace("\t", "\t" * tab_size) + line.plain = line.plain.replace("\t", " " * tab_size) + line._length = len(line.plain) if no_wrap: new_lines = Lines([line]) else: @@ -1239,7 +1240,6 @@ def wrap( console, width, justify=wrap_justify, overflow=wrap_overflow ) for line in new_lines: - line.plain = line.plain.replace("\t" * tab_size, " " * tab_size) line.truncate(width, overflow=wrap_overflow) lines.extend(new_lines) return lines