Fixes #5385. Add Markdown.RenderToAnsi() API for headless ANSI rendering#5388
Conversation
Add a public instance method on the Markdown view that renders markdown content to ANSI escape sequences without requiring Application.Init() or an interactive TUI session. The method encapsulates the headless driver workaround into a single call: string ansi = markdownView.RenderToAnsi(text, width: 80); The method: - Creates a temporary headless ANSI driver internally - Copies SyntaxHighlighter, MarkdownPipeline, UseThemeBackground, and ShowHeadingPrefix configuration from the instance - Calculates full content height and renders the entire document - Returns the ANSI string without mutating the calling instance Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9757f7eb22
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Wrap the temporary IApplication and Markdown view in using declarations so they are disposed on all paths, including when Layout(), Draw(), or a custom ISyntaxHighlighter throws an exception. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR adds a new public API on Terminal.Gui.Views.Markdown to render Markdown content into an ANSI-escaped string in a headless context (no interactive TUI session), enabling “cat/print mode” scenarios like those described in #5385.
Changes:
- Added
Markdown.RenderToAnsi(...)instance method that bootstraps a temporary ANSI-driverIApplication, lays out/draws a configured Markdown view, and returnsDriver.ToAnsi()output. - Added parallelizable unit tests validating basic rendering, parameter behavior, width wrapping, syntax highlighting, and non-mutation of the calling instance.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| Terminal.Gui/Views/Markdown/MarkdownView.Ansi.cs | Implements the new RenderToAnsi headless ANSI rendering API on Markdown. |
| Tests/UnitTestsParallelizable/Views/Markdown/MarkdownRenderToAnsiTests.cs | Adds unit tests covering the new ANSI-rendering API behavior. |
- Add MAX_RENDER_WIDTH (4096) upper bound to prevent OOM from absurd widths - Wrap env var set/restore in a static lock for thread safety - Use initial height of 1 instead of width for first layout pass - Fix XML docs to clarify ShowCopyButtons is always disabled for ANSI output Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Summary
Adds a public
RenderToAnsiinstance method on theMarkdownview that renders markdown content to ANSI escape sequences without requiringApplication.Init()or an interactive TUI session.API
Parameters
markdown(optional) — Text to render. Ifnull, uses the currentTextproperty.width(optional) — Target column width for word-wrapping. Defaults to 80.How it works
The method encapsulates the ~20-line headless driver workaround described in the issue into a single call:
DisableRealDriverIO=1to prevent real terminal I/OIApplicationwith the ANSI driverMarkdownview copying configuration from the calling instanceDriver.ToAnsi()Configuration properties respected
SyntaxHighlighter— Syntax highlighting for code blocksMarkdownPipeline— Custom Markdig pipelineUseThemeBackground— Theme background colorsShowHeadingPrefix— Whether to show#prefixesTesting
Closes #5385