From a486cda79c45c4493341791ff3761fc1f25b00c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Tue, 1 Aug 2023 20:34:04 +0200 Subject: [PATCH] Simplify code --- rich/_ratio.py | 6 ++---- rich/_wrap.py | 5 +---- rich/align.py | 5 ++--- rich/bar.py | 6 ++---- rich/cells.py | 7 ++----- rich/containers.py | 29 ++++++++++------------------- rich/progress_bar.py | 5 ++--- rich/prompt.py | 2 +- rich/syntax.py | 14 +++----------- 9 files changed, 25 insertions(+), 54 deletions(-) diff --git a/rich/_ratio.py b/rich/_ratio.py index f7dbe9270..ee613e216 100644 --- a/rich/_ratio.py +++ b/rich/_ratio.py @@ -132,10 +132,8 @@ def ratio_distribute( distributed_total: List[int] = [] append = distributed_total.append if minimums is None: - _minimums = [0] * len(ratios) - else: - _minimums = minimums - for ratio, minimum in zip(ratios, _minimums): + minimums = [0] * len(ratios) + for ratio, minimum in zip(ratios, minimums): if total_ratio > 0: distributed = max(minimum, ceil(ratio * total_remaining / total_ratio)) else: diff --git a/rich/_wrap.py b/rich/_wrap.py index c45f193f7..341b04d42 100644 --- a/rich/_wrap.py +++ b/rich/_wrap.py @@ -8,13 +8,10 @@ def words(text: str) -> Iterable[Tuple[int, int, str]]: - position = 0 - word_match = re_word.match(text, position) - while word_match is not None: + for word_match in re_word.finditer(text): start, end = word_match.span() word = word_match.group(0) yield start, end, word - word_match = re_word.match(text, end) def divide_line(text: str, width: int, fold: bool = True) -> List[int]: diff --git a/rich/align.py b/rich/align.py index e8fc30623..bf6ea4540 100644 --- a/rich/align.py +++ b/rich/align.py @@ -196,9 +196,8 @@ def generate_segments() -> Iterable[Segment]: ) def blank_lines(count: int) -> Iterable[Segment]: - if count > 0: - for _ in range(count): - yield blank_line + for _ in range(count): + yield blank_line vertical_height = self.height or options.height iter_segments: Iterable[Segment] diff --git a/rich/bar.py b/rich/bar.py index ed86a552d..6c9638e04 100644 --- a/rich/bar.py +++ b/rich/bar.py @@ -60,12 +60,10 @@ def __rich_console__( return prefix_complete_eights = int(width * 8 * self.begin / self.size) - prefix_bar_count = prefix_complete_eights // 8 - prefix_eights_count = prefix_complete_eights % 8 + prefix_bar_count, prefix_eights_count = divmod(prefix_complete_eights, 8) body_complete_eights = int(width * 8 * self.end / self.size) - body_bar_count = body_complete_eights // 8 - body_eights_count = body_complete_eights % 8 + body_bar_count, body_eights_count = divmod(body_complete_eights, 8) # When start and end fall into the same cell, we ideally should render # a symbol that's "center-aligned", but there is no good symbol in Unicode. diff --git a/rich/cells.py b/rich/cells.py index 9354f9e31..b774a70e1 100644 --- a/rich/cells.py +++ b/rich/cells.py @@ -69,8 +69,8 @@ def _get_codepoint_cell_size(codepoint: int) -> int: _table = CELL_WIDTHS lower_bound = 0 upper_bound = len(_table) - 1 - index = (lower_bound + upper_bound) // 2 - while True: + while lower_bound <= upper_bound: + index = (lower_bound + upper_bound) // 2 start, end, width = _table[index] if codepoint < start: upper_bound = index - 1 @@ -78,9 +78,6 @@ def _get_codepoint_cell_size(codepoint: int) -> int: lower_bound = index + 1 else: return 0 if width == -1 else width - if upper_bound < lower_bound: - break - index = (lower_bound + upper_bound) // 2 return 1 diff --git a/rich/containers.py b/rich/containers.py index e29cf3689..98276275e 100644 --- a/rich/containers.py +++ b/rich/containers.py @@ -1,4 +1,3 @@ -from itertools import zip_longest from typing import ( Iterator, Iterable, @@ -141,27 +140,19 @@ def justify( line.truncate(width, overflow=overflow) line.pad_left(width - cell_len(line.plain)) elif justify == "full": - for line_index, line in enumerate(self._lines): - if line_index == len(self._lines) - 1: - break + for line_index, line in enumerate(self._lines[:-1]): words = line.split(" ") words_size = sum(cell_len(word.plain) for word in words) num_spaces = len(words) - 1 - spaces = [1 for _ in range(num_spaces)] - index = 0 - if spaces: - while words_size + num_spaces < width: - spaces[len(spaces) - index - 1] += 1 - num_spaces += 1 - index = (index + 1) % len(spaces) + space, longer_spaces = divmod(width - words_size, num_spaces) + spaces = [space] * (num_spaces - longer_spaces) + [space + 1] * longer_spaces + tokens: List[Text] = [] - for index, (word, next_word) in enumerate( - zip_longest(words, words[1:]) - ): + for word, space, next_word in zip(words, spaces, words[1:]): tokens.append(word) - if index < len(spaces): - style = word.get_style_at_offset(console, -1) - next_style = next_word.get_style_at_offset(console, 0) - space_style = style if style == next_style else line.style - tokens.append(Text(" " * spaces[index], style=space_style)) + style = word.get_style_at_offset(console, -1) + next_style = next_word.get_style_at_offset(console, 0) + space_style = style if style == next_style else line.style + tokens.append(Text(" " * space, style=space_style)) + tokens.append(words[-1]) self[line_index] = Text("").join(tokens) diff --git a/rich/progress_bar.py b/rich/progress_bar.py index da1dd6091..7eaa6bf21 100644 --- a/rich/progress_bar.py +++ b/rich/progress_bar.py @@ -148,7 +148,7 @@ def _render_pulse( current_time = ( monotonic() if self.animation_time is None else self.animation_time ) - segments = pulse_segments * (int(width / segment_count) + 2) + segments = pulse_segments * (width // segment_count + 2) offset = int(-current_time * 15) % segment_count segments = segments[offset : offset + width] yield from segments @@ -176,8 +176,7 @@ def __rich_console__( if self.total and completed is not None else width * 2 ) - bar_count = complete_halves // 2 - half_bar_count = complete_halves % 2 + bar_count, half_bar_count = divmod(complete_halves, 2) style = console.get_style(self.style) is_finished = self.total is None or self.completed >= self.total complete_style = console.get_style( diff --git a/rich/prompt.py b/rich/prompt.py index 064c959b1..e95527a77 100644 --- a/rich/prompt.py +++ b/rich/prompt.py @@ -354,7 +354,7 @@ def process_response(self, value: str) -> bool: result = IntPrompt.ask( ":rocket: Enter a number between [b]1[/b] and [b]10[/b]", default=5 ) - if result >= 1 and result <= 10: + if 1 <= result <= 10: break print(":pile_of_poo: [prompt.invalid]Number must be between 1 and 10") print(f"number={result}") diff --git a/rich/syntax.py b/rich/syntax.py index ac669b834..1d9086def 100644 --- a/rich/syntax.py +++ b/rich/syntax.py @@ -492,18 +492,10 @@ def tokens_to_spans() -> Iterable[Tuple[str, Optional[Style]]]: line_no = 0 _line_start = line_start - 1 if line_start else 0 - # Skip over tokens until line start - while line_no < _line_start: - try: - _token_type, token = next(tokens) - except StopIteration: - break - yield (token, None) - if token.endswith("\n"): - line_no += 1 - # Generate spans until line end for token_type, token in tokens: - yield (token, _get_theme_style(token_type)) + # Skip over tokens until line start, then generate spans until line end + style = None if line_no < _line_start else _get_theme_style(token_type) + yield token, style if token.endswith("\n"): line_no += 1 if line_end and line_no >= line_end: