import math from functools import lru_cache from time import monotonic from typing import Iterable, List, Optional
from .color import Color, blend_rgb from .color_triplet import ColorTriplet from .console import Console, ConsoleOptions, RenderResult from .jupyter import JupyterMixin from .measure import Measurement from .segment import Segment from .style import Style, StyleType
# Number of characters before 'pulse' animation repeats
PULSE_SIZE = 20
class ProgressBar(JupyterMixin): """Renders a (progress) bar. Used by rich.progress.
Args:
total (float, optional): Number of steps in the bar. Defaults to 100. Set to None to render a pulsing animation.
completed (float, optional): Number of steps completed. Defaults to 0.
width (int, optional): Width of the bar, or ``None`` for maximum width. Defaults to None.
pulse (bool, optional): Enable pulse effect. Defaults to False. Will pulse if a None total was passed.
style (StyleType, optional): Style for the bar background. Defaults to "bar.back".
complete_style (StyleType, optional): Style for the completed bar. Defaults to "bar.complete".
finished_style (StyleType, optional): Style for a finished bar. Defaults to "bar.finished".
pulse_style (StyleType, optional): Style for pulsing bars. Defaults to "bar.pulse".
animation_time (Optional[float], optional): Time in seconds to use for animation, orNone to use system time. """
@lru_cache(maxsize=16) def _get_pulse_segments(
self,
fore_style: Style,
back_style: Style,
color_system: str,
no_color: bool,
ascii: bool = False,
) -> List[Segment]: """Get a list of segments to render a pulse animation.
Returns:
List[Segment]: A list of segments, one segment per character. """
bar = "-"if ascii else"━"
segments: List[Segment] = [] if color_system notin ("standard", "eight_bit", "truecolor") or no_color:
segments += [Segment(bar, fore_style)] * (PULSE_SIZE // 2)
segments += [Segment(" "if no_color else bar, back_style)] * (
PULSE_SIZE - (PULSE_SIZE // 2)
) return segments
append = segments.append
fore_color = (
fore_style.color.get_truecolor() if fore_style.color else ColorTriplet(255, 0, 255)
)
back_color = (
back_style.color.get_truecolor() if back_style.color else ColorTriplet(0, 0, 0)
)
cos = math.cos
pi = math.pi
_Segment = Segment
_Style = Style
from_triplet = Color.from_triplet
for index in range(PULSE_SIZE):
position = index / PULSE_SIZE
fade = 0.5 + cos(position * pi * 2) / 2.0
color = blend_rgb(fore_color, back_color, cross_fade=fade)
append(_Segment(bar, _Style(color=from_triplet(color)))) return segments
def update(self, completed: float, total: Optional[float] = None) -> None: """Update progress with new values.
Args:
completed (float): Number of steps completed.
total (float, optional): Total number of steps, or ``None`` to not change. Defaults to None. """
self.completed = completed
self.total = total if total isnotNoneelse self.total
if __name__ == "__main__": # pragma: no cover
console = Console()
bar = ProgressBar(width=50, total=100)
import time
console.show_cursor(False) for n in range(0, 101, 1):
bar.update(n)
console.print(bar)
console.file.write("\r")
time.sleep(0.05)
console.show_cursor(True)
console.print()
Messung V0.5
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.