From 1d2ae781175fa18070b10b01f2de441cc215e87d Mon Sep 17 00:00:00 2001 From: Isaac Wahhab Date: Mon, 29 Mar 2021 09:30:44 -0700 Subject: [PATCH] Reverting status renderable change and adding spinner text change --- CHANGELOG.md | 2 +- rich/console.py | 5 +++-- rich/spinner.py | 27 ++++++++++++++++++++------- rich/status.py | 13 +++++-------- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59367ea0..91008078 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Improved layout.tree - Changed default theme color for repr.number to cyan - `__rich_measure__` signature changed to accept ConsoleOptions rather than max_width -- `status` parameter to rich.status.Status changed to TextType +- `text` parameter to rich.spinner.Spinner changed to RenderableType ### Added diff --git a/rich/console.py b/rich/console.py index f22620a9..5d434eaf 100644 --- a/rich/console.py +++ b/rich/console.py @@ -5,6 +5,7 @@ import shutil import sys import threading from abc import ABC, abstractmethod +from collections import abc from dataclasses import dataclass, field from datetime import datetime from functools import wraps @@ -997,7 +998,7 @@ class Console: def status( self, - status: TextType, + status: RenderableType, *, spinner: str = "dots", spinner_style: str = "status.spinner", @@ -1007,7 +1008,7 @@ class Console: """Display a status and spinner. Args: - status (TextType): Text for a given status. + status (RenderableType): A status renderable (str or Text typically). console (Console, optional): Console instance to use, or None for global console. Defaults to None. spinner (str, optional): Name of spinner animation (see python -m rich.spinner). Defaults to "dots". spinner_style (StyleType, optional): Style of spinner. Defaults to "status.spinner". diff --git a/rich/spinner.py b/rich/spinner.py index 5e0bb075..fe5c34f0 100644 --- a/rich/spinner.py +++ b/rich/spinner.py @@ -2,22 +2,28 @@ from typing import cast, List, Optional, TYPE_CHECKING from ._spinners import SPINNERS from .measure import Measurement +from .table import Table from .text import Text, TextType if TYPE_CHECKING: - from .console import Console, ConsoleOptions, RenderResult + from .console import Console, ConsoleOptions, RenderResult, RenderableType from .style import StyleType class Spinner: def __init__( - self, name: str, text: TextType = "", *, style: "StyleType" = None, speed=1.0 + self, + name: str, + text: "RenderableType" = "", + *, + style: "StyleType" = None, + speed=1.0, ) -> None: """A spinner animation. Args: name (str): Name of spinner (run python -m rich.spinner). - text (TextType, optional): Text to display at the right of the spinner. Defaults to "". + text (TextType, optional): A renderable to display at the right of the spinner (str or Text typically). Defaults to "". style (StyleType, optional): Style for spinner animation. Defaults to None. speed (float, optional): Speed factor for animation. Defaults to 1.0. @@ -51,14 +57,14 @@ class Spinner: text = self.render(0) return Measurement.get(console, options, text) - def render(self, time: float) -> Text: + def render(self, time: float) -> "RenderableType": """Render the spinner for a given time. Args: time (float): Time in seconds. Returns: - Text: A Text instance containing animation frame. + RenderableType: A renderable containing animation frame. """ frame_no = int((time * self.speed) / (self.interval / 1000.0)) if self._update_speed: @@ -66,7 +72,14 @@ class Spinner: self.speed = self._update_speed self._update_speed = 0.0 frame = Text(self.frames[frame_no % len(self.frames)], style=self.style or "") - return Text.assemble(frame, " ", self.text) if self.text else frame + if not self.text: + return frame + elif isinstance(self.text, (str, Text)): + return Text.assemble(frame, " ", self.text) + else: + table = Table.grid(padding=1) + table.add_row(frame, self.text) + return table def update( self, *, text: TextType = "", style: "StyleType" = None, speed: float = None @@ -74,7 +87,7 @@ class Spinner: """Updates attributes of a spinner after it has been started. Args: - text (TextType, optional): Text to display at the right of the spinner. Defaults to "". + text (TextType, optional): A renderable to display at the right of the spinner (str or Text typically). Defaults to "". style (StyleType, optional): Style for spinner animation. Defaults to None. speed (float, optional): Speed factor for animation. Defaults to None. """ diff --git a/rich/status.py b/rich/status.py index 1cff3d59..bd535cfc 100644 --- a/rich/status.py +++ b/rich/status.py @@ -1,4 +1,4 @@ -from typing import Optional, TYPE_CHECKING +from typing import Optional from .console import Console, RenderableType from .jupyter import JupyterMixin @@ -6,15 +6,12 @@ from .live import Live from .spinner import Spinner from .style import StyleType -if TYPE_CHECKING: - from .text import TextType - class Status(JupyterMixin): """Displays a status indicator with a 'spinner' animation. Args: - status (TextType): Text for a given status. + status (RenderableType): A status renderable (str or Text typically). console (Console, optional): Console instance to use, or None for global console. Defaults to None. spinner (str, optional): Name of spinner animation (see python -m rich.spinner). Defaults to "dots". spinner_style (StyleType, optional): Style of spinner. Defaults to "status.spinner". @@ -24,7 +21,7 @@ class Status(JupyterMixin): def __init__( self, - status: "TextType", + status: RenderableType, *, console: Console = None, spinner: str = "dots", @@ -54,7 +51,7 @@ class Status(JupyterMixin): def update( self, - status: Optional["TextType"] = None, + status: Optional[RenderableType] = None, *, spinner: Optional[str] = None, spinner_style: Optional[StyleType] = None, @@ -63,7 +60,7 @@ class Status(JupyterMixin): """Update status. Args: - status (Optional[TextType], optional): New status text or None for no change. Defaults to None. + status (Optional[RenderableType], optional): New status renderable or None for no change. Defaults to None. spinner (Optional[str], optional): New spinner or None for no change. Defaults to None. spinner_style (Optional[StyleType], optional): New spinner style or None for no change. Defaults to None. speed (Optional[float], optional): Speed factor for spinner animation or None for no change. Defaults to None.