feat(markdown): implement table rendering with imgui.begin_table
This commit is contained in:
@@ -1,8 +1,28 @@
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from imgui_bundle import imgui
|
||||
|
||||
_TABLE_SEPARATOR = re.compile(r"^\|?\s*:?-{2,}:?\s*(\|\s*:?-{2,}:?\s*)+\|?\s*$")
|
||||
|
||||
def render_table(block: "TableBlock") -> None:
|
||||
"""Render a GFM table block via imgui.begin_table.
|
||||
[C: src/markdown_helper.py:MarkdownRenderer.render]
|
||||
"""
|
||||
n_cols = len(block.headers)
|
||||
if n_cols == 0: return
|
||||
flags = imgui.TableFlags_.borders | imgui.TableFlags_.row_bg | imgui.TableFlags_.resizable
|
||||
if not imgui.begin_table("md_table", n_cols, flags): return
|
||||
imgui.table_headers_row()
|
||||
for h in block.headers:
|
||||
imgui.table_next_column()
|
||||
imgui.text(h)
|
||||
for row in block.rows:
|
||||
imgui.table_next_row()
|
||||
for c in row:
|
||||
imgui.table_next_column()
|
||||
imgui.text(c)
|
||||
imgui.end_table()
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TableBlock:
|
||||
"""Frozen GFM table block.
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
from unittest.mock import patch
|
||||
from src.markdown_table import parse_tables, render_table, TableBlock
|
||||
|
||||
def test_render_table_invokes_begin_table(app_instance):
|
||||
with patch("src.markdown_table.imgui") as mock_imgui:
|
||||
mock_imgui.TableFlags_ = type("T", (), {"borders": 1, "row_bg": 2, "resizable": 4})()
|
||||
mock_imgui.begin_table.return_value = True
|
||||
block = TableBlock(headers=["A", "B"], rows=[["1", "2"]], span=(0, 3))
|
||||
render_table(block)
|
||||
assert mock_imgui.begin_table.called
|
||||
args, _ = mock_imgui.begin_table.call_args
|
||||
assert args[0] == "md_table"
|
||||
assert args[1] == 2
|
||||
assert mock_imgui.end_table.called
|
||||
|
||||
def test_render_table_skips_when_no_columns():
|
||||
with patch("src.markdown_table.imgui") as mock_imgui:
|
||||
block = TableBlock(headers=[], rows=[], span=(0, 1))
|
||||
render_table(block)
|
||||
assert not mock_imgui.begin_table.called
|
||||
Reference in New Issue
Block a user