This commit is contained in:
Will McGugan 2021-02-15 20:48:36 +00:00
parent 9a29b36514
commit a7fa23dcb3
7 changed files with 71 additions and 62 deletions

View file

@ -6,7 +6,8 @@ Demonstrates a Rich "application" using the Layout and Live classes.
from datetime import datetime
from rich import box
from rich.console import Console
from rich.align import Align
from rich.console import Console, RenderGroup
from rich.layout import Layout
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, BarColumn, TextColumn
@ -28,7 +29,7 @@ def make_layout() -> Layout:
)
layout["main"].split(
Layout(name="side"),
Layout(name="body", ratio=2),
Layout(name="body", ratio=2, minimum_size=60),
direction="horizontal",
)
layout["side"].split(Layout(name="box1"), Layout(name="box2"))
@ -57,21 +58,19 @@ def make_sponsor_message() -> Panel:
)
intro_message = Text.from_markup(
"""\
It takes a lot of time to develop Rich and to provide support.
Consider supporting my work via Github Sponsors (ask your company / organization), or buy me a coffee to say thanks.
- Will McGugan"""
"""Consider supporting my work via Github Sponsors (ask your company / organization), or buy me a coffee to say thanks. - Will McGugan"""
)
message = Table.grid(padding=2)
message = Table.grid(padding=1)
message.add_column()
message.add_column(no_wrap=True)
message.add_row(intro_message, sponsor_message)
message_panel = Panel.fit(
message,
message_panel = Panel(
Align.center(
RenderGroup(intro_message, "\n", Align.center(sponsor_message)),
vertical="middle",
),
box=box.ROUNDED,
padding=(1, 2),
title="[b red]Thanks for trying out Rich!",
@ -96,43 +95,43 @@ class Header:
def make_syntax() -> Syntax:
code = """\
def ratio_resolve(total: int, edges: List[Edge]) -> List[int]:
sizes = [(edge.size or None) for edge in edges]
def ratio_resolve(total: int, edges: List[Edge]) -> List[int]:
sizes = [(edge.size or None) for edge in edges]
# While any edges haven't been calculated
while any(size is None for size in sizes):
# Get flexible edges and index to map these back on to sizes list
flexible_edges = [
(index, edge)
for index, (size, edge) in enumerate(zip(sizes, edges))
if size is None
]
# Remaining space in total
remaining = total - sum(size or 0 for size in sizes)
if remaining <= 0:
# No room for flexible edges
sizes[:] = [(size or 0) for size in sizes]
# While any edges haven't been calculated
while any(size is None for size in sizes):
# Get flexible edges and index to map these back on to sizes list
flexible_edges = [
(index, edge)
for index, (size, edge) in enumerate(zip(sizes, edges))
if size is None
]
# Remaining space in total
remaining = total - sum(size or 0 for size in sizes)
if remaining <= 0:
# No room for flexible edges
sizes[:] = [(size or 0) for size in sizes]
break
# Calculate number of characters in a ratio portion
portion = remaining / sum((edge.ratio or 1) for _, edge in flexible_edges)
# If any edges will be less than their minimum, replace size with the minimum
for index, edge in flexible_edges:
if portion * edge.ratio <= edge.minimum_size:
sizes[index] = edge.minimum_size
break
# Calculate number of characters in a ratio portion
portion = remaining / sum((edge.ratio or 1) for _, edge in flexible_edges)
# If any edges will be less than their minimum, replace size with the minimum
else:
# Distribute flexible space and compensate for rounding error
# Since edge sizes can only be integers we need to add the remainder
# to the following line
_modf = modf
remainder = 0.0
for index, edge in flexible_edges:
if portion * edge.ratio <= edge.minimum_size:
sizes[index] = edge.minimum_size
break
else:
# Distribute flexible space and compensate for rounding error
# Since edge sizes can only be integers we need to add the remainder
# to the following line
_modf = modf
remainder = 0.0
for index, edge in flexible_edges:
remainder, size = _modf(portion * edge.ratio + remainder)
sizes[index] = int(size)
break
# Sizes now contains integers only
return cast(List[int], sizes)
remainder, size = _modf(portion * edge.ratio + remainder)
sizes[index] = int(size)
break
# Sizes now contains integers only
return cast(List[int], sizes)
"""
syntax = Syntax(code, "python", line_numbers=True)
return syntax