Reverting status renderable change and adding spinner text change

This commit is contained in:
Isaac Wahhab 2021-03-29 09:30:44 -07:00
parent 4007a11160
commit 1d2ae78117
4 changed files with 29 additions and 18 deletions

View File

@ -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

View File

@ -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".

View File

@ -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.
"""

View File

@ -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.