Skip to content

Commit

Permalink
tabs to space changes
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jul 27, 2023
1 parent aca9467 commit f25cb16
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions rich/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ def right_crop(self, offset: int) -> "Span":
return self
return Span(start, min(offset, end), style)

def add_padding(self, padding: int) -> "Span":
"""Add spaces the the end of a span.
Args:
padding (int): Number of additional spaces.
Returns:
Span: A span.
"""
if padding:
start, end, style = self
return Span(start, end + padding, style)
else:
return self


class Text(JupyterMixin):
"""Text with color / style.
Expand Down Expand Up @@ -549,6 +564,27 @@ def get_style_at_offset(self, console: "Console", offset: int) -> Style:
style += get_style(span_style, default="")
return style

def extend_style(self, spaces: int) -> None:
"""Extend the Text and styles by a given number of spaces.
Args:
spaces (int): Number of spaces to add to the Text.
"""
if not spaces:
return
spans = self.spans
new_spaces = " " * spaces
if spans:
end_offset = len(self)
self._spans = [
span.add_padding(spaces) if span.end >= end_offset else span
for span in spans
]
self._text.append(new_spaces)
self._length += spaces
else:
self += new_spaces

def highlight_regex(
self,
re_highlight: str,
Expand Down Expand Up @@ -793,15 +829,14 @@ def expand_tabs(self, tab_size: Optional[int] = None) -> None:
parts = line.split("\t", include_separator=True)
for part in parts:
if part.plain.endswith("\t"):
part._text = [part.plain[:-1] + " "]
append(part)
part.right_crop(1)
pos += len(part)
spaces = tab_size - ((pos - 1) % tab_size) - 1
spaces = tab_size - ((pos - 1) % tab_size)
if spaces:
append(" " * spaces, _style)
part.extend_style(spaces)
pos += spaces
else:
append(part)
append(part)

self._text = [result.plain]
self._length = len(self.plain)
self._spans[:] = result._spans
Expand Down Expand Up @@ -1088,7 +1123,6 @@ def divide(self, offsets: Iterable[int]) -> Lines:
_Span = Span

for span_start, span_end, style in self._spans:

lower_bound = 0
upper_bound = line_count
start_line_no = (lower_bound + upper_bound) // 2
Expand Down

0 comments on commit f25cb16

Please sign in to comment.