Skip to content

Instantly share code, notes, and snippets.

@TylerHendrickson
Created May 28, 2025 23:42
Show Gist options
  • Select an option

  • Save TylerHendrickson/d0fcda688adc58b44270ecd37feb6416 to your computer and use it in GitHub Desktop.

Select an option

Save TylerHendrickson/d0fcda688adc58b44270ecd37feb6416 to your computer and use it in GitHub Desktop.
Some fancy columns to use when tracking progress with rich
import rich.progress
import rich.style
class ColorChangingBarColumn(rich.progress.BarColumn):
"""Like `rich.progress.BarColumn` but fades from red to green as progress moves from 0 to 100%"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.finished_style = rich.style.Style(color="rgb(0, 255, 0)")
def render(self, task: rich.progress.Task) -> rich.progress_bar.ProgressBar:
progress_bar = super().render(task)
pct = (progress_bar.percentage_completed or 0) / 100.0
if pct == 0:
r, g, b = 255., 0., 0.
elif pct == 1:
r, g, b = 0., 255., 0.
elif pct > 0.5:
r, g, b = (1 - 2 * (pct - 0.5)) * 255, 255.0, 0.0
else:
r, g, b = (255.0, (255 * 2 * (pct / 1)), 0.0)
color = rich.style.Style(color=f"rgb({round(r)}, {round(g)}, {round(b)})")
progress_bar.complete_style = color
return progress_bar
class IncrementingCounterColumn(rich.progress.RenderableColumn):
"""Useful for displaying columns with secondary progress measures (e.g. transformed output, etc.)"""
def __init__(
self,
unit: str = "",
prefix: str = "",
suffix: str = "",
start: int = 0,
*,
plural_unit: typing.Optional[str] = None,
blank_at_zero=False,
count_color="green",
):
super().__init__()
self.count = start
self.unit = unit
self.plural_unit = plural_unit or f"{unit}s"
self.prefix = prefix
self.suffix = suffix
self.blank_at_zero = blank_at_zero
self.count_color = count_color
@property
def current_unit(self):
if self.count == 1:
return self.unit
return self.plural_unit
def increment(self, amount: int = 1):
self.count += amount
def render(self, task: rich.progress.Task) -> str:
if self.blank_at_zero and self.count == 0:
return ""
return f"{self.prefix}[{self.count_color}]{self.count}[/{self.count_color}] {self.current_unit}{self.suffix}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment