Skip to content

Fix right justification: conditionally preserve trailing space character #3649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ The following people have contributed to the development of Rich:
- [L. Yeung](https://github.com/lewis-yeung)
- [chthollyphile](https://github.com/chthollyphile)
- [Jonathan Helmus](https://github.com/jjhelmus)
- [Benjamin Ben Rachmiel](https://github.com/BenRachmiel)
8 changes: 3 additions & 5 deletions rich/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,10 @@ def __iter__(self) -> Iterator["Text"]:
return iter(self._lines)

@overload
def __getitem__(self, index: int) -> "Text":
...
def __getitem__(self, index: int) -> "Text": ...

@overload
def __getitem__(self, index: slice) -> List["Text"]:
...
def __getitem__(self, index: slice) -> List["Text"]: ...

def __getitem__(self, index: Union[slice, int]) -> Union["Text", List["Text"]]:
return self._lines[index]
Expand Down Expand Up @@ -137,7 +135,7 @@ def justify(
line.pad_right(width - cell_len(line.plain))
elif justify == "right":
for line in self._lines:
line.rstrip()
line[:-1].rstrip() if line[-1] == Text(" ") else line.rstrip()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the markup is irrelevent for the condition, it is faster to do line.plain.endswith(" ")

line.truncate(width, overflow=overflow)
line.pad_left(width - cell_len(line.plain))
elif justify == "full":
Expand Down