def test_align_center_middle(): console = Console(file=io.StringIO(), width=10) console.print(Align("foo\nbar", "center", vertical="middle"), height=5) expected = " \n foo \n bar \n \n \n" result = console.file.getvalue() print(repr(result)) assert result == expected
def test_align_width(): console = Console(file=io.StringIO(), width=40) words = "Deep in the human unconscious is a pervasive need for a logical universe that makes sense. But the real universe is always one step beyond logic" console.print(Align(words, "center", width=30)) result = console.file.getvalue() expected = " Deep in the human unconscious \n is a pervasive need for a \n logical universe that makes \n sense. But the real universe \n is always one step beyond \n logic \n" assert result == expected
def test_align_bottom(): console = Console(file=io.StringIO(), width=10) console.print(Align("foo", vertical="bottom"), height=5) expected = " \n \n \n \nfoo \n" result = console.file.getvalue() print(repr(result)) assert result == expected
def test_align_right_style(): console = Console( file=io.StringIO(), width=10, color_system="truecolor", force_terminal=True, _environ={}, ) console.print(Align("foo", "right", style="on blue")) assert console.file.getvalue() == "\x1b[44m \x1b[0m\x1b[44mfoo\x1b[0m\n"
def make_sponsor_message() -> Panel: """Some example content.""" sponsor_message = Table.grid(padding=1) sponsor_message.add_column(style="green", justify="right") sponsor_message.add_column(no_wrap=True) sponsor_message.add_row( "Sponsor me", "[u blue link=https://github.com/sponsors/willmcgugan]https://github.com/sponsors/willmcgugan", ) sponsor_message.add_row( "Buy me a :coffee:", "[u blue link=https://ko-fi.com/willmcgugan]https://ko-fi.com/willmcgugan", ) sponsor_message.add_row( "Twitter", "[u blue link=https://twitter.com/willmcgugan]https://twitter.com/willmcgugan", ) sponsor_message.add_row( "Blog", "[u blue link=https://www.willmcgugan.com]https://www.willmcgugan.com") intro_message = Text.from_markup( """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=1) message.add_column() message.add_column(no_wrap=True) message.add_row(intro_message, sponsor_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!", border_style="bright_blue", ) return message_panel
def test_shortcuts(): assert Align.left("foo").align == "left" assert Align.left("foo").renderable == "foo" assert Align.right("foo").align == "right" assert Align.right("foo").renderable == "foo" assert Align.center("foo").align == "center" assert Align.center("foo").renderable == "foo"
def test_bad_align_legal(): # Legal Align("foo", "left") Align("foo", "center") Align("foo", "right") # illegal with pytest.raises(ValueError): Align("foo", None) with pytest.raises(ValueError): Align("foo", "middle") with pytest.raises(ValueError): Align("foo", "") with pytest.raises(ValueError): Align("foo", "LEFT") with pytest.raises(ValueError): Align("foo", vertical="somewhere")
layout.split( Layout(name="header", size=1), Layout(ratio=1, name="main"), Layout(size=10, name="footer"), ) layout["main"].split_row(Layout(name="side"), Layout(name="body", ratio=2)) layout["side"].split(Layout(), Layout()) layout["body"].update( Align.center( Text( """This is a demonstration of rich.Layout\n\nHit Ctrl+C to exit""", justify="center", ), vertical="middle", ) ) class Clock: """Renders the time in the center of the screen.""" def __rich__(self) -> Text: return Text(datetime.now().ctime(), style="bold magenta", justify="center") layout["header"].update(Clock())
], ] console = Console() BEAT_TIME = 0.04 @contextmanager def beat(length: int = 1) -> None: yield time.sleep(length * BEAT_TIME) table = Table(show_footer=False) table_centered = Align.center(table) console.clear() with Live(table_centered, console=console, screen=False, refresh_per_second=20): with beat(10): table.add_column("Release Date", no_wrap=True) with beat(10): table.add_column("Title", Text.from_markup("[b]Total", justify="right")) with beat(10): table.add_column("Budget", "[u]$412,000,000", no_wrap=True)
""" Use Bar to renderer a sort-of circle. """ import math from mudrich.align import Align from mudrich.bar import Bar from mudrich.color import Color from mudrich import print SIZE = 40 for row in range(SIZE): y = (row / (SIZE - 1)) * 2 - 1 x = math.sqrt(1 - y * y) color = Color.from_rgb((1 + y) * 127.5, 0, 0) bar = Bar(2, width=SIZE * 2, begin=1 - x, end=1 + x, color=color) print(Align.center(bar))
def test_align_fit(): console = Console(file=io.StringIO(), width=10) console.print(Align("foobarbaze", "center")) assert console.file.getvalue() == "foobarbaze\n"
def test_align_right(): console = Console(file=io.StringIO(), width=10) console.print(Align("foo", "right")) assert console.file.getvalue() == " foo\n"
def test_repr(): repr(Align("foo", "left")) repr(Align("foo", "center")) repr(Align("foo", "right"))
def test_align_no_pad(): console = Console(file=io.StringIO(), width=10) console.print(Align("foo", "center", pad=False)) console.print(Align("foo", "left", pad=False)) assert console.file.getvalue() == " foo\nfoo\n"
def test_measure(): console = Console(file=io.StringIO(), width=20) _min, _max = Measurement.get(console, console.options, Align("foo bar", "left")) assert _min == 3 assert _max == 7