Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Small refactor of Syntax padding
  • Loading branch information
darrenburns committed May 3, 2022
commit 00c787f3d19b32a5746a6cf5186f43d3ad4e7aa1
31 changes: 13 additions & 18 deletions rich/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,24 +547,18 @@ def __rich_measure__(
def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> RenderResult:
(
background_style,
number_style,
highlight_number_style,
) = self._get_number_styles(console)
segments = Segments(
self._get_syntax(console, options, self._theme.get_background_style())
)
segments = Segments(self._get_syntax(console, options))
if self.padding:
yield Padding(segments, style=background_style, pad=self.padding)
yield Padding(
segments, style=self._theme.get_background_style(), pad=self.padding
)
else:
yield segments

def _get_syntax(
self,
console: Console,
options: ConsoleOptions,
background_style: Style,
) -> Iterable[Segment]:
"""
Get the Segments for the Syntax object, excluding any vertical/horizontal padding
Expand Down Expand Up @@ -642,13 +636,16 @@ def _get_syntax(

highlight_line = self.highlight_lines.__contains__
_Segment = Segment
wrapped_line_left_pad = _Segment(
" " * numbers_column_width + " ", background_style
)
new_line = _Segment("\n")

line_pointer = "> " if options.legacy_windows else "❱ "

(
background_style,
number_style,
highlight_number_style,
) = self._get_number_styles(console)

for line_no, line in enumerate(lines, self.start_line + line_offset):
if self.word_wrap:
wrapped_lines = console.render_lines(
Expand All @@ -672,11 +669,9 @@ def _get_syntax(
]

if self.line_numbers:
(
background_style,
number_style,
highlight_number_style,
) = self._get_number_styles(console)
wrapped_line_left_pad = _Segment(
" " * numbers_column_width + " ", background_style
)
for first, wrapped_line in loop_first(wrapped_lines):
if first:
line_column = str(line_no).rjust(numbers_column_width - 2) + " "
Expand Down