Skip navigation
"Please fix Markdown tables!"
0:00 / 0:37

"Please fix Markdown tables!"

kmdr

@kmdrfx7.5K followers

Visit on X
689 likesDownloadOriginal post

208,680 views

"Please fix Markdown tables!" Okay: text-only, wrapping, columnar selection, proper border conjunctions, fill full width, in opencode beta now. "What took so long?" Here's a writeup: OpenTUI uses yoga layout and has elements called Renderables. Boxes with borders, plain text, code with tree-sitter backed highlighting and other primitives. Organised in a tree structure resembling somewhat of a DOM. Approaching a table naively, given the primitives are there, one would think to just stack box and text elements the right way in a flex-box layout to visually represent a table. Boxes support borders. Problem solved. This is what an LLM would one-shot in a working state, given OpenTUI's API surface. Ignoring the fact that just using box local borders don't handle border conjunctions properly. Benchmarking something like that quickly shows that instantiating an average table takes >70ms and incremental updates become expensive. Hugely due to yoga-layout via wasm having a painful price on yoga API calls. The whole ordeal becomes memory hungry, because a Text element handles more than just plain text. A simple 4x6 table needs a Box and Text per cell, ending up with 48 heavy nodes that yoga must lay out. "But that's just OpenTUI being slow" - you might say. Yes, but no. Yoga should be integrated in the zig native binary core of OpenTUI. It is on the roadmap to do so, which will speed up render passes by 2-5x. Yoga-layout has an open PR to support CSS Grids, which would greatly ease building something like a table. We will use that for fully laid out tables when it gets there. Below the typescript core level Renderables, there are lower level primitives like TextBuffers and TextBufferViews, bound via FFI and completely handled in Zig. I was stuck expecting a table primitive to handle a full layout like a table in the browser does. For Markdown all we need is a text-only table. So we had to come up with a better idea, something that is feasible now. A table layout is pretty straight forward. No need to have yoga deal with that. Using TextBufferViews for cells directly gives lower level control and eliminates some overhead that Boxes and Text renderables have. A simple native method to draw a grid with proper conjunctions is a nice library method. It will surely be used for other cases, so that's what we added. Using this simplified approach we were able to bring down initial instantiation to <1ms, more than 70x improvement. With a far smaller memory footprint. Given all the low level primitives are known and implementing a text-only table like this is possible, Codex was of great help to carve out the PoC, setup the benchmarks and tests. That's only a fraction of what was needed though. The table needs options to span the full available width, render different border styles, show/hide borders, padding, selection etc. So many iterations later OpenTUI now has a text-only, performant and relatively cheap TextTable that we can leverage to render Markdown tables in a streaming/incremental manner. Efficiently and properly.

38 Comments

Comments from the original post on X (Twitter)

  • dax's profile picture

    @thdxr

    lol the ending

    139 likes

  • kmdr's profile picture

    @kmdrfx

    Nobody's watching a video to the end, right?

    71 likes

  • Anthony Shew's profile picture

    @anthonysheww

    Normie: “If you can render DOOM, you can render a table.” kmdr: 😩😩😩😩😩

    15 likes

  • ily⚡️'s profile picture

    @0xIlyy

    you can show off tech without being a degenerate gooner

    9 likes

  • Wes Winder's profile picture

    @weswinder

    so kinda like a small game engine

    6 likes

  • banteg's profile picture

    @banteg

    looks really good. the only remaining fix is to get rid of excel styling. delete all borders and rules and only keep one horiz rule after the heading.

    6 likes

  • Tim Culverhouse's profile picture

    @rockorager

    Why is yoga so slow?

    6 likes

  • kmdr's profile picture

    @kmdrfx

    It's not that bad, just the wasm version with the binding overhead is.

    8 likes

  • Shreyas Mididoddi's profile picture

    @Shreyassanthu77

    Niceee what is stopping you from integrating yoga into zig build and linking it statically? I am a professional zig build script writer (ask @neogoose_btw ) I can do it for you if you want

    4 likes

  • kmdr's profile picture

    @kmdrfx

    @neogoose_btw It's mostly a matter of priorities and it has to work for millions of users. @simonkleee was already looking into it, join us on the OpenCode discord!

    7 likes

  • Shreyas Mididoddi's profile picture

    @Shreyassanthu77

    @neogoose_btw @simonkleee well assuming yoga supports Target platforms i don't see why it'd not work for millions of users Let me join the discord brb

    2 likes

  • Libegato's profile picture

    @Libegato

    Wtf is this ending

    4 likes

  • Quentin Merabet's profile picture

    @QuentinMerabet

    oh yeah let's disrespect 51% of the society for engagement this place sucks

    2 likes

  • banteg's profile picture

    @banteg

    @willmcgugan please rate this tui table implementation

    2 likes

  • albs—'s profile picture

    @albfresco

    ayeeeyooo am I the only one that made it to the end

    2 likes

  • Leonardo Trapani's profile picture

    @leo_trapani

    Good technical posts are back

    2 likes

  • Jeff Tang's profile picture

    @jefftangx

    Such a good deep dive and then insane ending. It’s like a reverse hook

    1 like

  • Sami Yerunkar's profile picture

    @SYerunkar

    so this is a game engine

    1 like

  • White Zohran Mamdani's profile picture

    @0xgunboats

    @0xcarnation crazy ass ending

    1 like

  • SIGKITTEN's profile picture

    @SIGKITTEN

    very nice

    1 like