2from functools
import lru_cache
3from time
import monotonic
4from typing
import Iterable, List, Optional
6from .color
import Color, blend_rgb
7from .color_triplet
import ColorTriplet
8from .console
import Console, ConsoleOptions, RenderResult
9from .jupyter
import JupyterMixin
10from .measure
import Measurement
11from .segment
import Segment
12from .style
import Style, StyleType
19 """Renders a (progress) bar. Used by rich.progress.
22 total (float, optional): Number of steps in the bar. Defaults to 100. Set to None to render a pulsing animation.
23 completed (float, optional): Number of steps completed. Defaults to 0.
24 width (int, optional): Width of the bar, or ``None`` for maximum width. Defaults to None.
25 pulse (bool, optional): Enable pulse effect. Defaults to False. Will pulse if a None total was passed.
26 style (StyleType, optional): Style for the bar background. Defaults to "bar.back".
27 complete_style (StyleType, optional): Style for the completed bar. Defaults to "bar.complete".
28 finished_style (StyleType, optional): Style for a finished bar. Defaults to "bar.finished".
29 pulse_style (StyleType, optional): Style for pulsing bars. Defaults to "bar.pulse".
30 animation_time (Optional[float], optional): Time in seconds to use for animation, or None to use system time.
35 total: Optional[float] = 100.0,
37 width: Optional[int] =
None,
39 style: StyleType =
"bar.back",
40 complete_style: StyleType =
"bar.complete",
41 finished_style: StyleType =
"bar.finished",
42 pulse_style: StyleType =
"bar.pulse",
43 animation_time: Optional[float] =
None,
55 self._pulse_segments: Optional[List[Segment]] =
None
58 return f
"<Bar {self.completed!r} of {self.total!r}>"
62 """Calculate percentage complete."""
63 if self.
total is None:
66 completed = min(100, max(0.0, completed))
69 @lru_cache(maxsize=16)
78 """Get a list of segments to render a pulse animation.
81 List[Segment]: A list of segments, one segment per character.
83 bar =
"-" if ascii
else "━"
84 segments: List[Segment] = []
85 if color_system
not in (
"standard",
"eight_bit",
"truecolor")
or no_color:
86 segments += [
Segment(bar, fore_style)] * (PULSE_SIZE // 2)
87 segments += [
Segment(
" " if no_color
else bar, back_style)] * (
88 PULSE_SIZE - (PULSE_SIZE // 2)
109 for index
in range(PULSE_SIZE):
110 position = index / PULSE_SIZE
111 fade = 0.5 +
cos((position * pi * 2)) / 2.0
112 color = blend_rgb(fore_color, back_color, cross_fade=fade)
116 def update(self, completed: float, total: Optional[float] =
None) ->
None:
117 """Update progress with new values.
120 completed (float): Number of steps completed.
121 total (float, optional): Total number of steps, or ``None`` to not change. Defaults to None.
124 self.
total = total
if total
is not None else self.
total
127 self, console: Console, width: int, ascii: bool =
False
128 ) -> Iterable[Segment]:
129 """Renders the pulse animation.
132 console (Console): Console instance.
133 width (int): Width in characters of pulse animation.
136 RenderResult: [description]
139 Iterator[Segment]: Segments to render pulse
147 segment_count =
len(pulse_segments)
151 segments = pulse_segments * (int(width / segment_count) + 2)
152 offset = int(-current_time * 15) % segment_count
153 segments = segments[offset : offset + width]
157 self, console: Console, options: ConsoleOptions
162 should_pulse = self.
pulse or self.
total is None
167 completed: Optional[float] = (
171 bar =
"-" if ascii
else "━"
172 half_bar_right =
" " if ascii
else "╸"
173 half_bar_left =
" " if ascii
else "╺"
175 int(width * 2 * completed / self.
total)
176 if self.
total and completed
is not None
179 bar_count = complete_halves // 2
180 half_bar_count = complete_halves % 2
188 yield _Segment(bar * bar_count, complete_style)
190 yield _Segment(half_bar_right * half_bar_count, complete_style)
193 remaining_bars = width - bar_count - half_bar_count
195 if not half_bar_count
and bar_count:
196 yield _Segment(half_bar_left, style)
199 yield _Segment(bar * remaining_bars, style)
202 self, console: Console, options: ConsoleOptions
206 if self.
width is not None
211if __name__ ==
"__main__":
218 for n
in range(0, 101, 1):
__init__(self, Optional[float] total=100.0, float completed=0, Optional[int] width=None, bool pulse=False, StyleType style="bar.back", StyleType complete_style="bar.complete", StyleType finished_style="bar.finished", StyleType pulse_style="bar.pulse", Optional[float] animation_time=None)
List[Segment] _get_pulse_segments(self, Style fore_style, Style back_style, str color_system, bool no_color, bool ascii=False)
Optional[float] percentage_completed(self)
Iterable[Segment] _render_pulse(self, Console console, int width, bool ascii=False)
RenderResult __rich_console__(self, Console console, ConsoleOptions options)
None update(self, float completed, Optional[float] total=None)
Measurement __rich_measure__(self, Console console, ConsoleOptions options)