From 47fa89a0cce4be3d2426926a60da561f8ca3e805 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sun, 2 May 2021 19:57:04 +0100 Subject: [PATCH] add Console.measure --- CHANGELOG.md | 5 +++-- examples/table_movie.py | 4 ++-- rich/align.py | 2 +- rich/console.py | 17 +++++++++++++++++ rich/panel.py | 4 ++-- 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ebebabd..6eb13a54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +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). -## [10.1.1] - Unreleased +## [10.2.0] - Unreleased ### Added -- Added syntax for call, i.e. "Foo(bar)" will highlight Foo.pyth +- Added syntax for call, i.e. "Foo(bar)" +- Added Console.measure as a convenient alias for Measurement.get ## [10.1.0] - 2020-04-03 diff --git a/examples/table_movie.py b/examples/table_movie.py index 8081d7da..2f05c9e8 100644 --- a/examples/table_movie.py +++ b/examples/table_movie.py @@ -111,7 +111,7 @@ with Live(table_centered, console=console, screen=False, refresh_per_second=20): with beat(10): table.show_footer = True - table_width = Measurement.get(console, console.options, table).maximum + table_width = console.measure(table).maximum with beat(10): table.columns[2].justify = "right" @@ -175,7 +175,7 @@ with Live(table_centered, console=console, screen=False, refresh_per_second=20): with beat(10): table.pad_edge = False - original_width = Measurement.get(console, console.options, table).maximum + original_width = console.measure(table).maximum for width in range(original_width, console.width, 2): with beat(1): diff --git a/rich/align.py b/rich/align.py index cd169ece..d5bb50f8 100644 --- a/rich/align.py +++ b/rich/align.py @@ -137,7 +137,7 @@ class Align(JupyterMixin): self, console: "Console", options: "ConsoleOptions" ) -> "RenderResult": align = self.align - width = Measurement.get(console, options, self.renderable).maximum + width = console.measure(self.renderable, options=options).maximum rendered = console.render( Constrain( self.renderable, width if self.width is None else min(width, self.width) diff --git a/rich/console.py b/rich/console.py index 05e6dd7c..b86f7177 100644 --- a/rich/console.py +++ b/rich/console.py @@ -1092,6 +1092,23 @@ class Console: """ return ScreenContext(self, hide_cursor=hide_cursor, style=style or "") + def measure( + self, renderable: RenderableType, *, options: Optional[ConsoleOptions] = None + ) -> Measurement: + """Measure a renderable. Returns a :class:`~rich.measure.Measurement` object which contains + information regarding the number of characters required to print the renderable. + + Args: + renderable (RenderableType): Any renderable or string. + options (Optional[ConsoleOptions], optional): Options to use when measuring, or None + to use default options. Defaults to None. + + Returns: + Measurement: A measurement of the renderable. + """ + measurement = Measurement.get(self, options or self.options, renderable) + return measurement + def render( self, renderable: RenderableType, options: Optional[ConsoleOptions] = None ) -> Iterable[Segment]: diff --git a/rich/panel.py b/rich/panel.py index 0fa42d70..7baf988f 100644 --- a/rich/panel.py +++ b/rich/panel.py @@ -133,8 +133,8 @@ class Panel(JupyterMixin): child_width = ( width - 2 if self.expand - else Measurement.get( - console, options.update_width(width - 2), renderable + else console.measure( + renderable, options=options.update_width(width - 2) ).maximum ) child_height = self.height or options.height or None