diff --git a/src/textual/widgets/_data_table.py b/src/textual/widgets/_data_table.py index b6cf4e7651..32fab94cef 100644 --- a/src/textual/widgets/_data_table.py +++ b/src/textual/widgets/_data_table.py @@ -171,7 +171,7 @@ class Column: content_width: int = 0 auto_width: bool = False - def render_width(self, data_table: DataTable[Any]) -> int: + def get_render_width(self, data_table: DataTable[Any]) -> int: """Width, in cells, required to render the column with padding included. Args: @@ -1037,7 +1037,7 @@ def watch_show_header(self, show: bool) -> None: def watch_show_row_labels(self, show: bool) -> None: width, height = self.virtual_size - column_width = self._label_column.render_width(self) + column_width = self._label_column.get_render_width(self) width_change = column_width if show else -column_width self.virtual_size = Size(width + width_change, height) self._scroll_cursor_into_view() @@ -1206,7 +1206,7 @@ def _highlight_cursor(self) -> None: def _row_label_column_width(self) -> int: """The render width of the column containing row labels""" return ( - self._label_column.render_width(self) + self._label_column.get_render_width(self) if self._should_render_row_labels else 0 ) @@ -1304,7 +1304,7 @@ def _update_dimensions(self, new_rows: Iterable[RowKey]) -> None: row_index, column_index, style, - column.render_width(self), + column.get_render_width(self), cursor=should_highlight( cursor_location, cell_location, cursor_type ), @@ -1314,7 +1314,7 @@ def _update_dimensions(self, new_rows: Iterable[RowKey]) -> None: ) cell_height = len(rendered_cell) rendered_cells.append( - (rendered_cell, cell_height, column.render_width(self)) + (rendered_cell, cell_height, column.get_render_width(self)) ) height = max(height, cell_height) @@ -1332,7 +1332,7 @@ def _update_dimensions(self, new_rows: Iterable[RowKey]) -> None: ) data_cells_width = sum( - column.render_width(self) for column in self.columns.values() + column.get_render_width(self) for column in self.columns.values() ) total_width = data_cells_width + self._row_label_column_width header_height = self.header_height if self.show_header else 0 @@ -1354,13 +1354,13 @@ def _get_cell_region(self, coordinate: Coordinate) -> Region: # plus the width of the render width of the longest row label. x = ( sum( - column.render_width(self) + column.get_render_width(self) for column in self.ordered_columns[:column_index] ) + self._row_label_column_width ) column_key = self._column_locations.get_key(column_index) - width = self.columns[column_key].render_width(self) + width = self.columns[column_key].get_render_width(self) height = row.height y = sum(ordered_row.height for ordered_row in self.ordered_rows[:row_index]) if self.show_header: @@ -1377,7 +1377,7 @@ def _get_row_region(self, row_index: int) -> Region: row_key = self._row_locations.get_key(row_index) row = rows[row_key] row_width = ( - sum(column.render_width(self) for column in self.columns.values()) + sum(column.get_render_width(self) for column in self.columns.values()) + self._row_label_column_width ) y = sum(ordered_row.height for ordered_row in self.ordered_rows[:row_index]) @@ -1394,13 +1394,13 @@ def _get_column_region(self, column_index: int) -> Region: columns = self.columns x = ( sum( - column.render_width(self) + column.get_render_width(self) for column in self.ordered_columns[:column_index] ) + self._row_label_column_width ) column_key = self._column_locations.get_key(column_index) - width = columns[column_key].render_width(self) + width = columns[column_key].get_render_width(self) header_height = self.header_height if self.show_header else 0 height = self._total_row_height + header_height full_column_region = Region(x, 0, width, height) @@ -2083,7 +2083,7 @@ def _render_line_in_row( row_index, column_index, fixed_style, - column.render_width(self), + column.get_render_width(self), cursor=should_highlight( cursor_location, cell_location, cursor_type ), @@ -2100,7 +2100,7 @@ def _render_line_in_row( row_index, column_index, row_style, - column.render_width(self), + column.get_render_width(self), cursor=should_highlight(cursor_location, cell_location, cursor_type), hover=should_highlight(hover_location, cell_location, cursor_type), )[line_no] @@ -2110,7 +2110,7 @@ def _render_line_in_row( widget_width = self.size.width table_width = ( sum( - column.render_width(self) + column.get_render_width(self) for column in self.ordered_columns[self.fixed_columns :] ) + self._row_label_column_width @@ -2194,7 +2194,7 @@ def _render_line(self, y: int, x1: int, x2: int, base_style: Style) -> Strip: hover_location=self.hover_coordinate, ) fixed_width = sum( - column.render_width(self) + column.get_render_width(self) for column in self.ordered_columns[: self.fixed_columns] ) @@ -2314,7 +2314,7 @@ def _get_fixed_offset(self) -> Spacing: top += sum(row.height for row in self.ordered_rows[: self.fixed_rows]) left = ( sum( - column.render_width(self) + column.get_render_width(self) for column in self.ordered_columns[: self.fixed_columns] ) + self._row_label_column_width diff --git a/tests/test_data_table.py b/tests/test_data_table.py index 5e424d6610..65df13cd4b 100644 --- a/tests/test_data_table.py +++ b/tests/test_data_table.py @@ -273,7 +273,7 @@ async def test_add_column_with_width(): assert table.get_cell(row, column) == "123" assert table.columns[column].width == 10 assert ( - table.columns[column].render_width(table) + table.columns[column].get_render_width(table) == 10 + 2 * _DEFAULT_CELL_X_PADDING ) @@ -694,7 +694,7 @@ async def test_update_cell_at_column_width(label, new_value, new_content_width): await wait_for_idle() assert first_column.content_width == new_content_width assert ( - first_column.render_width(table) + first_column.get_render_width(table) == new_content_width + 2 * _DEFAULT_CELL_X_PADDING ) @@ -1202,21 +1202,21 @@ async def test_add_row_expands_column_widths(): table.add_column("Second", width=10) await pilot.pause() assert ( - table.ordered_columns[0].render_width(table) + table.ordered_columns[0].get_render_width(table) == 5 + 2 * _DEFAULT_CELL_X_PADDING ) assert ( - table.ordered_columns[1].render_width(table) + table.ordered_columns[1].get_render_width(table) == 10 + 2 * _DEFAULT_CELL_X_PADDING ) table.add_row("a" * 20, "a" * 20) await pilot.pause() assert ( - table.ordered_columns[0].render_width(table) + table.ordered_columns[0].get_render_width(table) == 20 + 2 * _DEFAULT_CELL_X_PADDING ) assert ( - table.ordered_columns[1].render_width(table) + table.ordered_columns[1].get_render_width(table) == 10 + 2 * _DEFAULT_CELL_X_PADDING )