column index

This commit is contained in:
Will McGugan 2020-09-18 15:27:07 +01:00
parent e389c05ec3
commit eb7737590d
4 changed files with 20 additions and 14 deletions

View file

@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Text.tabs_to_spaces was renamed to Text.expand_tabs, which works in place rather than returning a new instance
-
- Optimized Style.combine and Style.chain
- Optimized text rendering by fixing internal cache mechanism
- Optimized hash generation for Styles

View file

@ -59,7 +59,7 @@ You may also add columns by specifying them in the positional arguments of the :
table = Table("Released", "Title", "Box Office", title="Star Wars Movies")
This allows you to specify the text of the column only. If you want to set other attributes, such as width, style, and alignment, you can add an :class:`~rich.table.Column` class. Here's an example::
This allows you to specify the text of the column only. If you want to set other attributes, such as width and style, you can add an :class:`~rich.table.Column` class. Here's an example::
from rich.table import Column
table = Table(

View file

@ -28,9 +28,6 @@ if TYPE_CHECKING:
class Column:
"""Defines a column in a table."""
index: int
"""Index of column."""
header: "RenderableType" = ""
"""RenderableType: Renderable for the header (typically a string)"""
@ -60,6 +57,9 @@ class Column:
no_wrap: bool = False
"""bool: Prevent wrapping of text within the column. Defaults to ``False``."""
_index: int = 0
"""Index of column."""
_cells: List["RenderableType"] = field(default_factory=list)
@property
@ -138,10 +138,15 @@ class Table(JupyterMixin):
caption_style: StyleType = None,
) -> None:
self.columns = [
(Column(index, header) if isinstance(header, str) else header)
for index, header in enumerate(headers)
]
self.columns: List[Column] = []
append_column = self.columns.append
for index, header in enumerate(headers):
if isinstance(header, str):
append_column(Column(_index=index, header=header))
else:
header._index = index
append_column(header)
self.title = title
self.caption = caption
self.width = width
@ -296,7 +301,7 @@ class Table(JupyterMixin):
"""
column = Column(
index=len(self.columns),
_index=len(self.columns),
header=header,
footer=footer,
header_style=Style.pick_first(
@ -343,7 +348,7 @@ class Table(JupyterMixin):
]
for index, renderable in enumerate(cell_renderables):
if index == len(columns):
column = Column(index)
column = Column(_index=index)
for _ in range(self._row_count):
add_cell(column, Text(""))
self.columns.append(column)
@ -411,7 +416,7 @@ class Table(JupyterMixin):
for _range, column in zip(width_ranges, columns)
]
flex_minimum = [
(column.width or 1) + get_padding_width(column.index)
(column.width or 1) + get_padding_width(column._index)
for column in columns
if column.flexible
]
@ -552,7 +557,7 @@ class Table(JupyterMixin):
if max_width < 1:
return Measurement(0, 0)
padding_width = self._get_padding_width(column.index)
padding_width = self._get_padding_width(column._index)
if column.width is not None:
# Fixed width column
@ -565,7 +570,7 @@ class Table(JupyterMixin):
append_min = min_widths.append
append_max = max_widths.append
get_render_width = Measurement.get
for cell in self._get_cells(column.index, column):
for cell in self._get_cells(column._index, column):
_min, _max = get_render_width(console, cell.renderable, max_width)
append_min(_min)
append_max(_max)

View file

@ -810,7 +810,7 @@ class Text(JupyterMixin):
return self
def append_tokens(self, tokens: Iterable[Tuple[str, StyleType]]):
"""Append iterable of str and style. Style may be a Style instance or a style definition.
"""Append iterable of str and style. Style may be a Style instance or a str style definition.
Args:
pairs (Iterable[Tuple[str, StyleType]]): An iterable of tuples containing str content and style.