Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
eumiro committed Aug 1, 2023
1 parent 4ce138b commit a486cda
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 54 deletions.
6 changes: 2 additions & 4 deletions rich/_ratio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 1 addition & 4 deletions rich/_wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
5 changes: 2 additions & 3 deletions rich/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 2 additions & 4 deletions rich/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 2 additions & 5 deletions rich/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,15 @@ 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
elif codepoint > end:
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


Expand Down
29 changes: 10 additions & 19 deletions rich/containers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from itertools import zip_longest
from typing import (
Iterator,
Iterable,
Expand Down Expand Up @@ -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)
5 changes: 2 additions & 3 deletions rich/progress_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion rich/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
14 changes: 3 additions & 11 deletions rich/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a486cda

Please sign in to comment.