added time elapsed column

This commit is contained in:
Will McGugan 2020-12-28 16:24:30 +00:00
parent 0511792c33
commit aeec6b03fe
9 changed files with 100 additions and 17 deletions

View file

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [9.6.0] - Unreleased
### Changed
- MarkupError exception raise from None to omit internal exception
## [9.5.1] - 2020-12-19
### Fixed

View file

@ -16,7 +16,7 @@ To specify a foreground color use one of the 256 :ref:`appendix-colors`. For exa
console.print("Hello", style="magenta")
You may also use the color's number (an integer between 0 and 255) with the syntax `"color(<number>)"`. The following will give the equivalent output::
You may also use the color's number (an integer between 0 and 255) with the syntax ``"color(<number>)"``. The following will give the equivalent output::
console.print("Hello", style="color(5)")

View file

@ -2,7 +2,7 @@
name = "rich"
homepage = "https://github.com/willmcgugan/rich"
documentation = "https://rich.readthedocs.io/en/latest/"
version = "9.5.1"
version = "9.6.0"
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
authors = ["Will McGugan <willmcgugan@gmail.com>"]
license = "MIT"

View file

@ -364,9 +364,9 @@ class Color(NamedTuple):
"""Create a truecolor from three color components in the range(0->255).
Args:
red (float): Red component.
green (float): Green component.
blue (float): Blue component.
red (float): Red component in range 0-255.
green (float): Green component in range 0-255.
blue (float): Blue component in range 0-255.
Returns:
Color: A new color object.
@ -471,10 +471,8 @@ class Color(NamedTuple):
# Convert to 8-bit color from truecolor color
if system == ColorSystem.EIGHT_BIT and self.system == ColorSystem.TRUECOLOR:
assert self.triplet is not None
red, green, blue = self.triplet.normalized
_h, l, s = rgb_to_hls(red, green, blue)
# If saturation is under 10% assume it is grayscale
if s < 0.1:
gray = round(l * 25.0)

View file

@ -705,7 +705,7 @@ class Console:
width: Optional[int] = None
height: Optional[int] = None
if WINDOWS: # type: ignore
if WINDOWS: # pragma: no cover
width, height = shutil.get_terminal_size()
else:
try:

View file

@ -113,6 +113,7 @@ DEFAULT_STYLES: Dict[str, Style] = {
"progress.filesize": Style(color="green"),
"progress.filesize.total": Style(color="green"),
"progress.download": Style(color="green"),
"progress.elapsed": Style(color="yellow"),
"progress.percentage": Style(color="magenta"),
"progress.remaining": Style(color="cyan"),
"progress.data.speed": Style(color="red"),

View file

@ -104,6 +104,7 @@ def render(markup: str, style: Union[str, Style] = "", emoji: bool = True) -> Te
append_span = spans.append
_Span = Span
_Tag = Tag
def pop_style(style_name: str) -> Tuple[int, Tag]:
"""Pop tag matching given style name."""
@ -125,18 +126,18 @@ def render(markup: str, style: Union[str, Style] = "", emoji: bool = True) -> Te
except KeyError:
raise MarkupError(
f"closing tag '{tag.markup}' at position {position} doesn't match any open tag"
)
) from None
else: # implicit close
try:
start, open_tag = pop()
except IndexError:
raise MarkupError(
f"closing tag '[/]' at position {position} has nothing to close"
)
) from None
append_span(_Span(start, len(text), str(open_tag)))
else: # Opening tag
normalized_tag = Tag(normalize(tag.name), tag.parameters)
normalized_tag = _Tag(normalize(tag.name), tag.parameters)
style_stack.append((len(text), normalized_tag))
text_length = len(text)

View file

@ -1,9 +1,12 @@
from math import sqrt
from functools import lru_cache
from typing import Sequence, Tuple
from typing import Sequence, Tuple, TYPE_CHECKING
from .color_triplet import ColorTriplet
if TYPE_CHECKING:
from rich.table import Table
class Palette:
"""A palette of available colors."""
@ -14,6 +17,29 @@ class Palette:
def __getitem__(self, number: int) -> ColorTriplet:
return ColorTriplet(*self._colors[number])
def __rich__(self) -> "Table":
from rich.color import Color
from rich.style import Style
from rich.text import Text
from rich.table import Table
table = Table(
"index",
"RGB",
"Color",
title="Palette",
caption=f"{len(self._colors)} colors",
highlight=True,
caption_justify="right",
)
for index, color in enumerate(self._colors):
table.add_row(
str(index),
repr(color),
Text(" " * 16, style=Style(bgcolor=Color.from_rgb(*color))),
)
return table
# This is somewhat inefficient and needs caching
@lru_cache(maxsize=1024)
def match(self, color: Tuple[int, int, int]) -> int:
@ -43,3 +69,31 @@ class Palette:
min_index = min(range(len(self._colors)), key=get_color_distance)
return min_index
if __name__ == "__main__": # pragma: no cover
import colorsys
from typing import Iterable
from rich.color import Color
from rich.console import Console, ConsoleOptions
from rich.segment import Segment
from rich.style import Style
class ColorBox:
def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> Iterable[Segment]:
height = console.size.height - 3
for y in range(0, height):
for x in range(options.max_width):
h = x / options.max_width
l = y / (height + 1)
r1, g1, b1 = colorsys.hls_to_rgb(h, l, 1.0)
r2, g2, b2 = colorsys.hls_to_rgb(h, l + (1 / height / 2), 1.0)
bgcolor = Color.from_rgb(r1 * 255, g1 * 255, b1 * 255)
color = Color.from_rgb(r2 * 255, g2 * 255, b2 * 255)
yield Segment("", Style(color=color, bgcolor=bgcolor))
yield Segment.line()
console = Console()
console.print(ColorBox())

View file

@ -322,6 +322,18 @@ class BarColumn(ProgressColumn):
)
class TimeElapsedColumn(ProgressColumn):
"""Renders time elapsed."""
def render(self, task: "Task") -> Text:
"""Show time remaining."""
elapsed = task.finished_time if task.finished else task.elapsed
if elapsed is None:
return Text("-:--:--", style="progress.elapsed")
delta = timedelta(seconds=int(elapsed))
return Text(str(delta), style="progress.elapsed")
class TimeRemainingColumn(ProgressColumn):
"""Renders estimated time remaining."""
@ -434,6 +446,9 @@ class Task:
_get_time: GetTimeCallable
"""Callable to get the current time."""
finished_time: Optional[float] = None
"""float: Time task was finished."""
visible: bool = True
"""bool: Indicates if this task is visible in the progress display."""
@ -475,8 +490,8 @@ class Task:
@property
def finished(self) -> bool:
"""bool: Check if the task has completed."""
return self.completed >= self.total
"""Check if the task has finished."""
return self.finished_time is not None
@property
def percentage(self) -> float:
@ -518,6 +533,7 @@ class Task:
def _reset(self) -> None:
"""Reset progress."""
self._progress.clear()
self.finished_time = None
class _RefreshThread(Thread):
@ -799,9 +815,6 @@ class Progress(JupyterMixin, RenderHook):
task.fields.update(fields)
update_completed = task.completed - completed_start
if refresh:
self.refresh()
current_time = self.get_time()
old_sample_time = current_time - self.speed_estimate_period
_progress = task._progress
@ -813,6 +826,11 @@ class Progress(JupyterMixin, RenderHook):
popleft()
if update_completed > 0:
_progress.append(ProgressSample(current_time, update_completed))
if task.completed >= task.total and task.finished_time is None:
task.finished_time = task.elapsed
if refresh:
self.refresh()
def reset(
self,
@ -848,6 +866,7 @@ class Progress(JupyterMixin, RenderHook):
task.fields = fields
if description is not None:
task.description = description
task.finished_time = None
self.refresh()
def advance(self, task_id: TaskID, advance: float = 1) -> None:
@ -872,6 +891,8 @@ class Progress(JupyterMixin, RenderHook):
while len(_progress) > 1000:
popleft()
_progress.append(ProgressSample(current_time, update_completed))
if task.completed >= task.total and task.finished_time is None:
task.finished_time = task.elapsed
def refresh(self) -> None:
"""Refresh (render) the progress information."""
@ -1057,6 +1078,7 @@ if __name__ == "__main__": # pragma: no coverage
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
TimeRemainingColumn(),
TimeElapsedColumn(),
console=console,
transient=True,
) as progress:
@ -1074,3 +1096,4 @@ if __name__ == "__main__": # pragma: no coverage
except:
console.save_html("progress.html")
print("wrote progress.html")
raise