1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright (c) 2022-2025 R3BL LLC. Licensed under Apache License, Version 2.0.
//! # Rendering Pipeline Architecture
//!
//! **Source of Truth:** This module documents the complete 6-stage rendering pipeline.
//! Each stage has its own module with "You Are Here" breadcrumbs. Use the stage reference
//! table below to understand data flow and find relevant implementations.
//!
//! <div class="warning">
//!
//! **Architecture Context:** This pipeline implements **Path 1 (Composed Component
//! Pipeline)** from the dual rendering paths architecture. For the high-level overview
//! of both rendering paths (Path 1: full TUI vs Path 2: simple CLI), see the
//! [dual rendering paths].
//!
//! </div>
//!
//! ## Quick Pipeline Overview
//!
//! ```text
//! Application Code
//! ↓
//! Component (generates RenderOpIR)
//! ↓
//! RenderPipeline (organizes by ZOrder)
//! ↓
//! OffscreenBuffer (2D grid of styled pixels)
//! ↓
//! RenderOpOutputVec (backend-independent ops)
//! ↓
//! Terminal Backend (Crossterm or DirectToAnsi)
//! ↓
//! Terminal Display
//! ```
//!
//! ## Visual Pipeline Flow
//!
//! ```text
//! ┌───────────────────────────────────────┐
//! │ Stage 1: Application/Component Layer │
//! │ (Generates RenderOpIR with clipping) │
//! └────────────────┬──────────────────────┘
//! │
//! ┌────────────────▼───────────────────────────┐
//! │ Stage 2: RenderPipeline │
//! │ (Collects & organizes RenderOpIR by ZOrder)│
//! └────────────────┬───────────────────────────┘
//! │
//! ┌────────────────▼─────────────────────────┐
//! │ Stage 3: Compositor │
//! │ (Renders RenderOpIR to OffscreenBuffer) │
//! └────────────────┬─────────────────────────┘
//! │
//! ┌────────────────▼────────────────────────────────┐
//! │ Stage 4: Backend Converter Layer │
//! │ (Render OffscreenBuffer to RenderOpOutput; │
//! │ handle diff calculation for selective redraw) │
//! │ - OffscreenBufferPaint trait implementation │
//! │ - Converts PixelChars to styled text operations │
//! └────────────────┬────────────────────────────────┘
//! │
//! ┌────────────────▼──────────────────────────┐
//! │ Stage 5: Backend Executor │
//! │ (Execute RenderOpOutput via backend) │
//! │ - RenderOpPaint trait implementations │
//! │ - Crossterm or DirectToAnsi │
//! │ - Cursor movement, colors, text painting │
//! │ - Raw mode management & terminal flushing │
//! └────────────────┬──────────────────────────┘
//! │
//! ┌────────────────▼───────────────────┐
//! │ Stage 6: Terminal Output │
//! │ (Rendered content visible to user) │
//! └────────────────────────────────────┘
//! ```
//!
//! ## Stage Reference
//!
//! Use this table to navigate to specific pipeline stages. Each stage has a module
//! with "You Are Here" breadcrumbs to help orient yourself.
//!
//! | Stage | What It Does | Key Types | Module |
//! |:-------------------------------|:---------------------------------------------------------|:------------------------------------------|:------------------------------------------------------------------------------|
//! | **Stage 1: App/Component** | Components generate IR operations with clipping metadata | [`RenderOpIR`], [`RenderOpIRVec`] | [`render_op::render_op_ir`] |
//! | **Stage 2: Pipeline** | Organizes operations by Z-order into a render queue | [`RenderPipeline`], [`ZOrder`] | [`render_pipeline`] |
//! | **Stage 3: Compositor** | Executes IR operations, writes styled pixels to buffer | [`OffscreenBuffer`], [`PixelChar`] | [`compositor_render_ops_to_ofs_buf`] |
//! | **Stage 4: Backend Converter** | Compares buffers, generates optimized output operations | [`RenderOpOutput`], [`RenderOpOutputVec`] | [`offscreen_buffer::paint_impl`] (shared) |
//! | **Stage 5: Backend Executor** | Translates operations to terminal escape sequences | [`RenderOpPaint`] trait | [`crossterm_paint_render_op_impl`] or [`direct_to_ansi_paint_render_op_impl`] |
//! | **Stage 6: Terminal** | User-visible rendered content | Terminal emulator | (external) |
//!
//! ## Architecture: Shared Stages (1-4) vs Backend-Specific Stage (5)
//!
//! **Key Principle**: Stages 1-4 are **shared** across all terminal backends (Crossterm
//! and `DirectToAnsi`). Only Stage 5 (Backend Executor) is backend-specific.
//!
//! ### Shared Stages (1-4)
//! - **Stage 1**: Components generate [`RenderOpIR`] operations
//! - **Stage 2**: [`RenderPipeline`] organizes operations by [`ZOrder`]
//! - **Stage 3**: Compositor renders to [`OffscreenBuffer`] (2D grid of styled pixels)
//! - **Stage 4**: Backend Converter transforms [`OffscreenBuffer`] →
//! [`RenderOpOutputVec`]
//! - Implementation: [`offscreen_buffer::paint_impl`]
//! - Compares consecutive frames for diff-based selective redraw
//! - Generates abstract rendering operations (backend-independent)
//!
//! ### Backend-Specific Stage (5)
//! **Why Stage 5 is different**: Each backend has its own [`RenderOpPaint`] trait
//! implementation that translates abstract operations to terminal-specific commands:
//!
//! - **Crossterm**: Implementation in [`crossterm_backend`]
//! - Translates operations to Crossterm API calls
//! - **`DirectToAnsi`**: Implementation in [`direct_to_ansi`]
//! - Generates raw ANSI escape sequences
//!
//! The backend selection is made at compile-time via the [`TERMINAL_LIB_BACKEND`]
//! constant, ensuring both backends use the same Stage 1-4 pipeline.
//!
//! ## Data Flow Across Stages
//!
//! - **Input → Stage 1**: User code, component state
//! - **Stage 1 → Stage 2**: [`RenderOpIRVec`] (IR ops with clipping)
//! - **Stage 2 → Stage 3**: Organized [`RenderOpIRVec`] by [`ZOrder`]
//! - **Stage 3 → Stage 4**: Complete [`OffscreenBuffer`] (2D grid)
//! - **Stage 4 → Stage 5**: [`RenderOpOutputVec`] (optimized ops, already clipped)
//! - **Stage 5 → Stage 6**: ANSI escape sequences written to terminal
//!
//! ## Type Safety & Semantic Boundaries
//!
//! The pipeline enforces strict separation through types:
//! - **IR Operations** ([`RenderOpIR`]): Used by components, require clipping
//! - **Output Operations** ([`RenderOpOutput`]): Used by backends, already clipped
//! - **Execution Barrier**: Only [`RenderOpOutputVec`] can be executed via
//! [`RenderOpsExec`] trait, preventing IR operations from bypassing the compositor
//!
//! See [`render_op`] module for architectural details and type definitions.
//!
//! ## Module Map
//!
//! When navigating to individual modules below, you'll find a "You are here" comment at
//! the top showing which stage(s) the module implements. This helps you quickly
//! understand where the module fits in the 6-stage rendering pipeline.
//!
//! ### Core Data Types (Cross-Stage)
//! - [`render_op`] - `RenderOpIR`, `RenderOpOutput`, `RenderOpCommon`,
//! `RenderOpsLocalData`, type safety details
//!
//! ### Pipeline Stages
//! - [`render_pipeline`] - **(Stage 2)** Collects & organizes `RenderOpIR` by Z-order
//! - [`compositor_render_ops_to_ofs_buf`] - **(Stage 3)** Renders `RenderOpsIR` to
//! `OffscreenBuffer`
//! - [`offscreen_buffer`] - Virtual terminal buffer (2D grid of styled `PixelChars`)
//! - [`offscreen_buffer::paint_impl`] - **(Stage 4: Shared)** Converts buffer →
//! optimized operations (used by both Crossterm and `DirectToAnsi`)
//! - [`crossterm_backend::crossterm_paint_render_op_impl`] - **(Stage 5: Crossterm
//! Executor)** Executes operations via Crossterm
//!
//! ### Supporting Modules
//! - [`offscreen_buffer_pool`] - Buffer pooling for efficiency
//! - [`z_order`] - Z-order layer management
//! - [`raw_mode`] - Terminal raw mode setup/teardown
//! - [`mod@paint`] - Text painting utilities
//! - [`direct_to_ansi`] - **(Stage 5 Alternative)** Direct ANSI escape sequence
//! generation (Linux only)
//!
//! [`OffscreenBufferPaint`]: trait@offscreen_buffer::OffscreenBufferPaint
//! [`OffscreenBuffer`]: struct@offscreen_buffer::OffscreenBuffer
//! [`PixelChar`]: enum@offscreen_buffer::PixelChar
//! [`RenderOpCommon`]: enum@render_op::RenderOpCommon
//! [`RenderOpIRVec`]: struct@render_op::RenderOpIRVec
//! [`RenderOpIR`]: enum@render_op::RenderOpIR
//! [`RenderOpOutputVec`]: struct@render_op::RenderOpOutputVec
//! [`RenderOpOutput`]: enum@render_op::RenderOpOutput
//! [`RenderOpPaint`]: trait@render_op::RenderOpPaint
//! [`RenderOpsExec`]: trait@render_op::RenderOpsExec
//! [`RenderPipeline`]: struct@render_pipeline::RenderPipeline
//! [`ZOrder`]: enum@z_order::ZOrder
//! [`crossterm_paint_render_op_impl`]: mod@crossterm_backend::crossterm_paint_render_op_impl
//! [`direct_to_ansi_paint_render_op_impl`]: mod@direct_to_ansi::output::direct_to_ansi_paint_render_op_impl
//! [`paint_impl`]: mod@offscreen_buffer::paint_impl
//! [`paint_render_op_impl`]: mod@crossterm_backend::crossterm_paint_render_op_impl
//! [dual rendering paths]: mod@crate#dual-rendering-paths
// Skip rustfmt for rest of file.
// https://stackoverflow.com/a/75910283/2085356
/**************************/
/** Attach source files. **/
/**************************/
// Private mod.
// Private in production, public for docs/tests (enables rustdoc links to submodules).
/***********************************************/
/** Re-export shared components (Stages 1-5). **/
/***********************************************/
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
// Both backends are compiled; selection happens at compile time via TERMINAL_LIB_BACKEND.
pub use *;
pub use *;
/**********/
/* Tests. */
/**********/
/***************/
/* Benchmarks. */
/***************/