enhanced align

This commit is contained in:
Will McGugan 2020-07-10 16:11:33 +01:00
parent b90a35a0d1
commit 01410131cf
8 changed files with 78 additions and 22 deletions

View File

@ -511,7 +511,6 @@ class Progress(JupyterMixin, RenderHook):
self._live_render = LiveRender(self.get_renderable())
self._task_index: TaskID = TaskID(0)
self._refresh_thread: Optional[_RefreshThread] = None
self._refresh_count = 0
self._started = False
self.print = self.console.print
self.log = self.console.log
@ -728,25 +727,29 @@ class Progress(JupyterMixin, RenderHook):
def refresh(self) -> None:
"""Refresh (render) the progress information."""
if self.console.is_jupyter:
from ipywidgets import Output
from IPython.display import display
if self.console.is_jupyter: # pragma: no cover
try:
from ipywidgets import Output
from IPython.display import display
except ImportError:
import warnings
with self._lock:
if self.ipy_widget is None:
self.ipy_widget = Output()
display(self.ipy_widget)
warnings.warn('install "ipywidgets" for Jupyter support')
else:
with self._lock:
if self.ipy_widget is None:
self.ipy_widget = Output()
display(self.ipy_widget)
with self.ipy_widget:
self.ipy_widget.clear_output(wait=True)
self.console.print(self.get_renderable())
with self.ipy_widget:
self.ipy_widget.clear_output(wait=True)
self.console.print(self.get_renderable())
elif self.console.is_terminal:
with self._lock:
self._live_render.set_renderable(self.get_renderable())
with self.console:
self.console.print(Control(""))
self._refresh_count += 1
def get_renderable(self) -> RenderableType:
"""Get a renderable for the progress display."""

View File

@ -216,6 +216,15 @@ class Style:
self._style_definition = " ".join(attributes) or "none"
return self._style_definition
def __bool__(self) -> bool:
"""A Style is false if it has no attributes, colors, or links."""
return bool(
self._attributes & self._set_attributes
or self._color
or self._bgcolor
or self._link
)
def _make_ansi_codes(self, color_system: ColorSystem) -> str:
"""Generate ANSI codes for this style.

File diff suppressed because one or more lines are too long

View File

@ -4,6 +4,7 @@ import pytest
from rich.console import Console
from rich.align import Align
from rich.measure import Measurement
def test_bad_align_legal():
@ -24,7 +25,50 @@ def test_bad_align_legal():
Align("foo", "LEFT")
def test_render():
def test_align_left():
console = Console(file=io.StringIO(), width=10)
console.print(Align("foo", "left"))
assert console.file.getvalue() == "foo\n"
assert console.file.getvalue() == "foo \n"
def test_align_center():
console = Console(file=io.StringIO(), width=10)
console.print(Align("foo", "center"))
assert console.file.getvalue() == " foo \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_align_fit():
console = Console(file=io.StringIO(), width=10)
console.print(Align("foobarbaze", "center"))
assert console.file.getvalue() == "foobarbaze\n"
def test_align_right_style():
console = Console(
file=io.StringIO(), width=10, color_system="truecolor", force_terminal=True
)
console.print(Align("foo", "right", style="on blue"))
assert console.file.getvalue() == "\x1b[44m \x1b[0m\x1b[44mfoo\x1b[0m\n"
def test_measure():
console = Console(file=io.StringIO(), width=20)
_min, _max = Measurement.get(console, Align("foo bar", "left"), 20)
assert _min == 3
assert _max == 7
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"

View File

@ -172,18 +172,18 @@ def test_justify_renderable_none():
def test_justify_renderable_left():
console = Console(
file=io.StringIO(), force_terminal=True, width=20, legacy_windows=False
file=io.StringIO(), force_terminal=True, width=10, legacy_windows=False
)
console.print(Panel("FOO", expand=False), justify="left")
assert console.file.getvalue() == "╭───╮\n│FOO│\n╰───╯\n"
assert console.file.getvalue() == "╭───╮ \n│FOO│ \n╰───╯ \n"
def test_justify_renderable_center():
console = Console(
file=io.StringIO(), force_terminal=True, width=20, legacy_windows=False
file=io.StringIO(), force_terminal=True, width=10, legacy_windows=False
)
console.print(Panel("FOO", expand=False), justify="center")
assert console.file.getvalue() == " ╭───╮\n │FOO│\n ╰───╯\n"
assert console.file.getvalue() == " ╭───╮ \n │FOO│ \n ╰───╯ \n"
def test_justify_renderable_right():

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long