Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import rich
import rich.repr
from platformdirs import user_downloads_path
from rich.console import Console, RenderableType
from rich.console import Console, ConsoleDimensions, ConsoleOptions, RenderableType
from rich.control import Control
from rich.protocol import is_renderable
from rich.segment import Segment, Segments
Expand Down Expand Up @@ -1129,6 +1129,25 @@ def current_mode(self) -> str:
"""The name of the currently active mode."""
return self._current_mode

@property
def console_options(self) -> ConsoleOptions:
"""Get options for the Rich console.

Returns:
Console options (same object returned from `console.options`).
"""
size = ConsoleDimensions(*self.size)
console = self.console
return ConsoleOptions(
max_height=size.height,
size=size,
legacy_windows=console.legacy_windows,
min_width=1,
max_width=size.width,
encoding=console.encoding,
is_terminal=console.is_terminal,
)

def exit(
self,
result: ReturnType | None = None,
Expand Down
11 changes: 7 additions & 4 deletions src/textual/visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ def get_optimal_width(self, rules: RulesMap, container_width: int) -> int:
return width

def get_height(self, rules: RulesMap, width: int) -> int:
console = active_app.get().console
app = active_app.get()
console = app.console
renderable = self._renderable
if isinstance(renderable, Text):
height = len(
Expand All @@ -305,7 +306,8 @@ def get_height(self, rules: RulesMap, width: int) -> int:
)
)
else:
options = console.options.update_width(width).update(highlight=False)
console_options = app.console_options
options = console_options.update_width(width).update(highlight=False)
segments = console.render(renderable, options)
# Cheaper than counting the lines returned from render_lines!
height = sum([text.count("\n") for text, _, _ in segments])
Expand All @@ -326,8 +328,9 @@ def render_strips(
Returns:
An list of Strips.
"""
console = active_app.get().console
console_options = console.options.update(
app = active_app.get()
console = app.console
console_options = app.console_options.update(
highlight=False,
width=width,
height=height,
Expand Down
6 changes: 3 additions & 3 deletions src/textual/widgets/_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -2121,17 +2121,17 @@ def _render_cell(

if is_header_cell:
row_height = self.header_height
options = self.app.console.options.update_dimensions(width, row_height)
options = self.app.console_options.update_dimensions(width, row_height)
else:
# If an auto-height row hasn't had its height calculated, we don't fix
# the value for `height` so that we can measure the height of the cell.
row = self.rows[row_key]
if row.auto_height and row.height == 0:
row_height = 0
options = self.app.console.options.update_width(width)
options = self.app.console_options.update_width(width)
else:
row_height = row.height
options = self.app.console.options.update_dimensions(
options = self.app.console_options.update_dimensions(
width, row_height
)

Expand Down
5 changes: 3 additions & 2 deletions src/textual/widgets/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ def render_line(self, y: int) -> Strip:
return Strip.blank(self.size.width)

console = self.app.console
console_options = self.app.console_options
max_content_width = self.scrollable_content_region.width

if not self.value:
Expand All @@ -617,7 +618,7 @@ def render_line(self, y: int) -> Strip:

strip = Strip(
console.render(
placeholder, console.options.update_width(max_content_width + 1)
placeholder, console_options.update_width(max_content_width + 1)
)
)
else:
Expand Down Expand Up @@ -650,7 +651,7 @@ def render_line(self, y: int) -> Strip:
result.stylize(cursor_style, cursor, cursor + 1)

segments = list(
console.render(result, console.options.update_width(self.content_width))
console.render(result, console_options.update_width(self.content_width))
)

strip = Strip(segments)
Expand Down
Loading