[PR #715] [CLOSED] feat: Support multiple TUI renderers in a single process #1516

Closed
opened 2026-03-14 09:40:52 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/anomalyco/opentui/pull/715
Author: @jettptacek
Created: 2/19/2026
Status: Closed

Base: mainHead: main


📝 Commits (2)

  • 70b3999 feat: Support multiple TUI renderers in a single process
  • a02beff Fix: renderer-stack.ts for prettier

📊 Changes

12 files changed (+556 additions, -119 deletions)

View changed files

📝 packages/core/src/animation/Timeline.ts (+47 -29)
📝 packages/core/src/renderer.ts (+96 -29)
📝 packages/core/src/testing/test-renderer.ts (+0 -1)
packages/core/src/tests/renderer.multi-instance.test.ts (+138 -0)
📝 packages/core/src/zig.ts (+9 -0)
📝 packages/core/src/zig/lib.zig (+4 -0)
📝 packages/core/src/zig/renderer.zig (+70 -42)
📝 packages/core/src/zig/tests/renderer_test.zig (+89 -0)
📝 packages/solid/index.ts (+35 -14)
📝 packages/solid/src/elements/hooks.ts (+2 -1)
📝 packages/solid/src/reconciler.ts (+22 -3)
packages/solid/src/renderer-stack.ts (+44 -0)

📄 Description

This PR is meant to be the start of the conversation of allowing opentui to render multiple TUIs. I have allowed multiple independent CliRenderer instances to coexist within a single process. Previously, the Zig renderer used static/global output buffers and always wrote to stdout, meaning only one renderer could exist at a time. The timeline engine also only tracked a single renderer.

I understand this is a large change..... that cuts across the native renderer, the TypeScript layer, and the Solid integration and I'm expecting that others might want a different implementation

I am using this right now in ssh server where I can make a different tui per client connected.

Changes

Zig renderer (renderer.zig)

  • Output buffers moved from static var declarations to per-instance heap-allocated fields
  • OutputBufferWriter now holds a *CliRenderer pointer instead of referencing global state
  • All hardcoded std.fs.File.stdout() calls replaced with a per-instance output_file field (defaults to stdout)
  • New setOutputFd / fileFromFd to redirect a renderer's output to an arbitrary file descriptor, with cross-platform handle type support

FFI bindings (lib.zig, zig.ts)

  • Exposed setOutputFd through the FFI layer

TypeScript renderer (renderer.ts)

  • New remote config flag: when true, the renderer skips process-level signal handlers (SIGWINCH, uncaughtException, beforeExit), stdout interception, exit listeners, and global.requestAnimationFrame overwriting
  • Passes config.stdout fd to the native renderer via setOutputFd, with Windows _get_osfhandle conversion through msvcrt.dll
  • processResize changed from private to public so callers can trigger resizes on specific renderer instances

Timeline engine (Timeline.ts)

  • TimelineEngine changed from tracking a single renderer to a Map<CliRenderer, AttachedRenderer> where each renderer gets its own frame callback and live state
  • detach() can target a specific renderer or detach all

Solid integration (index.ts, reconciler.ts, hooks.ts, renderer-stack.ts)

  • New renderer-stack.ts module provides renderer lookup via a synchronous stack (during initial mount) and a WeakMap keyed by Solid owner (for effect re-runs)
  • useRenderer() falls back to getActiveRenderer() when RendererContext is not available
  • render() and testRender() now call engine.detach(renderer) on destroy
  • render() return type changed to Promise<CliRenderer>

Test renderer (test-renderer.ts)

  • Removed @ts-expect-error on processResize call (now public)

Tests

  • New TypeScript tests (renderer.multi-instance.test.ts): independent coexistence, independent resize, independent render buffers, destroy isolation, remote signal handler behavior, timeline engine multi-attach/detach
  • New Zig tests (renderer_test.zig): setOutputFd output redirect via pipe, two renderers writing to separate fds

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/anomalyco/opentui/pull/715 **Author:** [@jettptacek](https://github.com/jettptacek) **Created:** 2/19/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (2) - [`70b3999`](https://github.com/anomalyco/opentui/commit/70b39999366ecadaf842dd715f4ca9805e216c8c) feat: Support multiple TUI renderers in a single process - [`a02beff`](https://github.com/anomalyco/opentui/commit/a02beff83c29d916dd2ea874f67410e9ebee2606) Fix: renderer-stack.ts for prettier ### 📊 Changes **12 files changed** (+556 additions, -119 deletions) <details> <summary>View changed files</summary> 📝 `packages/core/src/animation/Timeline.ts` (+47 -29) 📝 `packages/core/src/renderer.ts` (+96 -29) 📝 `packages/core/src/testing/test-renderer.ts` (+0 -1) ➕ `packages/core/src/tests/renderer.multi-instance.test.ts` (+138 -0) 📝 `packages/core/src/zig.ts` (+9 -0) 📝 `packages/core/src/zig/lib.zig` (+4 -0) 📝 `packages/core/src/zig/renderer.zig` (+70 -42) 📝 `packages/core/src/zig/tests/renderer_test.zig` (+89 -0) 📝 `packages/solid/index.ts` (+35 -14) 📝 `packages/solid/src/elements/hooks.ts` (+2 -1) 📝 `packages/solid/src/reconciler.ts` (+22 -3) ➕ `packages/solid/src/renderer-stack.ts` (+44 -0) </details> ### 📄 Description This PR is meant to be the start of the conversation of allowing opentui to render multiple TUIs. I have allowed multiple independent `CliRenderer` instances to coexist within a single process. Previously, the Zig renderer used static/global output buffers and always wrote to `stdout`, meaning only one renderer could exist at a time. The timeline engine also only tracked a single renderer. I understand this is a large change..... that cuts across the native renderer, the TypeScript layer, and the Solid integration and I'm expecting that others might want a different implementation I am using this right now in ssh server where I can make a different tui per client connected. ## Changes **Zig renderer (`renderer.zig`)** - Output buffers moved from static `var` declarations to per-instance heap-allocated fields - `OutputBufferWriter` now holds a `*CliRenderer` pointer instead of referencing global state - All hardcoded `std.fs.File.stdout()` calls replaced with a per-instance `output_file` field (defaults to stdout) - New `setOutputFd` / `fileFromFd` to redirect a renderer's output to an arbitrary file descriptor, with cross-platform handle type support **FFI bindings (`lib.zig`, `zig.ts`)** - Exposed `setOutputFd` through the FFI layer **TypeScript renderer (`renderer.ts`)** - New `remote` config flag: when true, the renderer skips process-level signal handlers (`SIGWINCH`, `uncaughtException`, `beforeExit`), stdout interception, exit listeners, and `global.requestAnimationFrame` overwriting - Passes `config.stdout` fd to the native renderer via `setOutputFd`, with Windows `_get_osfhandle` conversion through `msvcrt.dll` - `processResize` changed from `private` to `public` so callers can trigger resizes on specific renderer instances **Timeline engine (`Timeline.ts`)** - `TimelineEngine` changed from tracking a single renderer to a `Map<CliRenderer, AttachedRenderer>` where each renderer gets its own frame callback and live state - `detach()` can target a specific renderer or detach all **Solid integration (`index.ts`, `reconciler.ts`, `hooks.ts`, `renderer-stack.ts`)** - New `renderer-stack.ts` module provides renderer lookup via a synchronous stack (during initial mount) and a `WeakMap` keyed by Solid owner (for effect re-runs) - `useRenderer()` falls back to `getActiveRenderer()` when `RendererContext` is not available - `render()` and `testRender()` now call `engine.detach(renderer)` on destroy - `render()` return type changed to `Promise<CliRenderer>` **Test renderer (`test-renderer.ts`)** - Removed `@ts-expect-error` on `processResize` call (now public) ## Tests - New TypeScript tests (`renderer.multi-instance.test.ts`): independent coexistence, independent resize, independent render buffers, destroy isolation, remote signal handler behavior, timeline engine multi-attach/detach - New Zig tests (`renderer_test.zig`): `setOutputFd` output redirect via pipe, two renderers writing to separate fds --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-14 09:40:52 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/opentui#1516
No description provided.