Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add param to disable render of rich markup #132

Merged
merged 1 commit into from
Oct 31, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

- Adds an optional parameter to DataTable to disable rendering of string data as Rich Markup.

## [0.9.0] - 2024-07-23

- Adds a PolarsBackend implementation of DataTableBackend. You must have `polars` installed to use the PolarsBackend. You can install it using the `polars` extra for this package.
Expand Down
8 changes: 7 additions & 1 deletion src/textual_fastdatatable/data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ def __init__(
classes: str | None = None,
disabled: bool = False,
null_rep: str = "",
render_markup: bool = True,
) -> None:
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
try:
Expand Down Expand Up @@ -652,6 +653,8 @@ def __init__(
"""The type of cursor of the `DataTable`."""
self.null_rep = Text.from_markup(null_rep)
"""The string used to represent missing data (None or null)"""
self.render_markup = render_markup
"""If true, render string data as Rich markup."""

@property
def hover_row(self) -> int:
Expand Down Expand Up @@ -1775,7 +1778,9 @@ def _get_row_renderables(self, row_index: int) -> RowRenderables:
empty = self.null_rep

formatted_row_cells = [
cell_formatter(datum, null_rep=empty, col=col)
cell_formatter(
datum, null_rep=empty, col=col, render_markup=self.render_markup
)
for datum, col in zip_longest(ordered_row, self.ordered_columns)
]
label = None
Expand Down Expand Up @@ -1811,6 +1816,7 @@ def _get_cell_renderable(
datum,
null_rep=self.null_rep,
col=self.ordered_columns[column_index],
render_markup=self.render_markup,
)

def _render_cell(
Expand Down
6 changes: 4 additions & 2 deletions src/textual_fastdatatable/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def cell_formatter(
obj: object, null_rep: Text, col: Column | None = None
obj: object, null_rep: Text, col: Column | None = None, render_markup: bool = True
) -> RenderableType:
"""Convert a cell into a Rich renderable for display.

Expand All @@ -30,12 +30,14 @@ def cell_formatter(
"""
if obj is None:
return Align(null_rep, align="center")
elif isinstance(obj, str):
elif isinstance(obj, str) and render_markup:
try:
rich_text: Text | str = Text.from_markup(obj)
except MarkupError:
rich_text = escape(obj)
return rich_text
elif isinstance(obj, str):
return escape(obj)
elif isinstance(obj, bool):
return Align(
f"[dim]{'✓' if obj else 'X'}[/] {obj}{' ' if obj else ''}",
Expand Down
Loading