ratatui_ruby 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. checksums.yaml +4 -4
  2. data/ext/ratatui_ruby/Cargo.lock +1 -1
  3. data/ext/ratatui_ruby/Cargo.toml +1 -1
  4. data/lib/ratatui_ruby/version.rb +1 -1
  5. metadata +1 -232
  6. data/.builds/ruby-3.2.yml +0 -54
  7. data/.builds/ruby-3.3.yml +0 -54
  8. data/.builds/ruby-3.4.yml +0 -54
  9. data/.builds/ruby-4.0.0.yml +0 -54
  10. data/.pre-commit-config.yaml +0 -16
  11. data/.rubocop.yml +0 -10
  12. data/AGENTS.md +0 -146
  13. data/CHANGELOG.md +0 -710
  14. data/README.md +0 -187
  15. data/README.rdoc +0 -302
  16. data/Rakefile +0 -11
  17. data/Steepfile +0 -49
  18. data/doc/concepts/application_architecture.md +0 -321
  19. data/doc/concepts/application_testing.md +0 -193
  20. data/doc/concepts/async.md +0 -190
  21. data/doc/concepts/custom_widgets.md +0 -247
  22. data/doc/concepts/debugging.md +0 -401
  23. data/doc/concepts/event_handling.md +0 -162
  24. data/doc/concepts/interactive_design.md +0 -146
  25. data/doc/contributors/auditing/parity.md +0 -239
  26. data/doc/contributors/design/ruby_frontend.md +0 -420
  27. data/doc/contributors/design/rust_backend.md +0 -422
  28. data/doc/contributors/design.md +0 -11
  29. data/doc/contributors/developing_examples.md +0 -400
  30. data/doc/contributors/documentation_style.md +0 -121
  31. data/doc/contributors/index.md +0 -21
  32. data/doc/contributors/todo/align/api_completeness_audit-finished.md +0 -375
  33. data/doc/contributors/todo/align/api_completeness_audit-unfinished.md +0 -206
  34. data/doc/contributors/todo/align/terminal.md +0 -647
  35. data/doc/contributors/todo/future_work.md +0 -169
  36. data/doc/contributors/upstream_requests/tab_rects.md +0 -173
  37. data/doc/contributors/upstream_requests/title_rects.md +0 -132
  38. data/doc/custom.css +0 -22
  39. data/doc/getting_started/quickstart.md +0 -291
  40. data/doc/getting_started/why.md +0 -93
  41. data/doc/images/app_all_events.png +0 -0
  42. data/doc/images/app_cli_rich_moments.gif +0 -0
  43. data/doc/images/app_color_picker.png +0 -0
  44. data/doc/images/app_debugging_showcase.gif +0 -0
  45. data/doc/images/app_debugging_showcase.png +0 -0
  46. data/doc/images/app_login_form.png +0 -0
  47. data/doc/images/app_stateful_interaction.png +0 -0
  48. data/doc/images/verify_quickstart_dsl.png +0 -0
  49. data/doc/images/verify_quickstart_layout.png +0 -0
  50. data/doc/images/verify_quickstart_lifecycle.png +0 -0
  51. data/doc/images/verify_readme_usage.png +0 -0
  52. data/doc/images/widget_barchart.png +0 -0
  53. data/doc/images/widget_block.png +0 -0
  54. data/doc/images/widget_box.png +0 -0
  55. data/doc/images/widget_calendar.png +0 -0
  56. data/doc/images/widget_canvas.png +0 -0
  57. data/doc/images/widget_cell.png +0 -0
  58. data/doc/images/widget_center.png +0 -0
  59. data/doc/images/widget_chart.png +0 -0
  60. data/doc/images/widget_gauge.png +0 -0
  61. data/doc/images/widget_layout_split.png +0 -0
  62. data/doc/images/widget_line_gauge.png +0 -0
  63. data/doc/images/widget_list.png +0 -0
  64. data/doc/images/widget_map.png +0 -0
  65. data/doc/images/widget_overlay.png +0 -0
  66. data/doc/images/widget_popup.png +0 -0
  67. data/doc/images/widget_ratatui_logo.png +0 -0
  68. data/doc/images/widget_ratatui_mascot.png +0 -0
  69. data/doc/images/widget_rect.png +0 -0
  70. data/doc/images/widget_render.png +0 -0
  71. data/doc/images/widget_rich_text.png +0 -0
  72. data/doc/images/widget_scroll_text.png +0 -0
  73. data/doc/images/widget_scrollbar.png +0 -0
  74. data/doc/images/widget_sparkline.png +0 -0
  75. data/doc/images/widget_style_colors.png +0 -0
  76. data/doc/images/widget_table.png +0 -0
  77. data/doc/images/widget_tabs.png +0 -0
  78. data/doc/images/widget_text_width.png +0 -0
  79. data/doc/index.md +0 -39
  80. data/doc/troubleshooting/async.md +0 -4
  81. data/doc/troubleshooting/terminal_limitations.md +0 -131
  82. data/doc/troubleshooting/tui_output.md +0 -197
  83. data/examples/app_all_events/README.md +0 -114
  84. data/examples/app_all_events/app.rb +0 -98
  85. data/examples/app_all_events/model/app_model.rb +0 -159
  86. data/examples/app_all_events/model/event_color_cycle.rb +0 -43
  87. data/examples/app_all_events/model/event_entry.rb +0 -94
  88. data/examples/app_all_events/model/msg.rb +0 -39
  89. data/examples/app_all_events/model/timestamp.rb +0 -56
  90. data/examples/app_all_events/update.rb +0 -75
  91. data/examples/app_all_events/view/app_view.rb +0 -80
  92. data/examples/app_all_events/view/controls_view.rb +0 -54
  93. data/examples/app_all_events/view/counts_view.rb +0 -61
  94. data/examples/app_all_events/view/live_view.rb +0 -72
  95. data/examples/app_all_events/view/log_view.rb +0 -57
  96. data/examples/app_all_events/view.rb +0 -9
  97. data/examples/app_cli_rich_moments/README.md +0 -81
  98. data/examples/app_cli_rich_moments/app.rb +0 -189
  99. data/examples/app_color_picker/README.md +0 -156
  100. data/examples/app_color_picker/app.rb +0 -76
  101. data/examples/app_color_picker/clipboard.rb +0 -86
  102. data/examples/app_color_picker/color.rb +0 -193
  103. data/examples/app_color_picker/controls.rb +0 -92
  104. data/examples/app_color_picker/copy_dialog.rb +0 -168
  105. data/examples/app_color_picker/export_pane.rb +0 -128
  106. data/examples/app_color_picker/harmony.rb +0 -58
  107. data/examples/app_color_picker/input.rb +0 -176
  108. data/examples/app_color_picker/main_container.rb +0 -180
  109. data/examples/app_color_picker/palette.rb +0 -111
  110. data/examples/app_debugging_showcase/README.md +0 -119
  111. data/examples/app_debugging_showcase/app.rb +0 -318
  112. data/examples/app_login_form/README.md +0 -58
  113. data/examples/app_login_form/app.rb +0 -109
  114. data/examples/app_stateful_interaction/README.md +0 -35
  115. data/examples/app_stateful_interaction/app.rb +0 -328
  116. data/examples/timeout_demo.rb +0 -45
  117. data/examples/verify_quickstart_dsl/README.md +0 -55
  118. data/examples/verify_quickstart_dsl/app.rb +0 -49
  119. data/examples/verify_quickstart_layout/README.md +0 -77
  120. data/examples/verify_quickstart_layout/app.rb +0 -73
  121. data/examples/verify_quickstart_lifecycle/README.md +0 -68
  122. data/examples/verify_quickstart_lifecycle/app.rb +0 -62
  123. data/examples/verify_readme_usage/README.md +0 -49
  124. data/examples/verify_readme_usage/app.rb +0 -42
  125. data/examples/verify_website_managed/README.md +0 -48
  126. data/examples/verify_website_managed/app.rb +0 -36
  127. data/examples/verify_website_menu/README.md +0 -60
  128. data/examples/verify_website_menu/app.rb +0 -84
  129. data/examples/verify_website_spinner/README.md +0 -44
  130. data/examples/verify_website_spinner/app.rb +0 -34
  131. data/examples/widget_barchart/README.md +0 -58
  132. data/examples/widget_barchart/app.rb +0 -240
  133. data/examples/widget_block/README.md +0 -44
  134. data/examples/widget_block/app.rb +0 -258
  135. data/examples/widget_box/README.md +0 -54
  136. data/examples/widget_box/app.rb +0 -255
  137. data/examples/widget_calendar/README.md +0 -48
  138. data/examples/widget_calendar/app.rb +0 -115
  139. data/examples/widget_canvas/README.md +0 -31
  140. data/examples/widget_canvas/app.rb +0 -130
  141. data/examples/widget_cell/README.md +0 -45
  142. data/examples/widget_cell/app.rb +0 -112
  143. data/examples/widget_center/README.md +0 -33
  144. data/examples/widget_center/app.rb +0 -118
  145. data/examples/widget_chart/README.md +0 -50
  146. data/examples/widget_chart/app.rb +0 -220
  147. data/examples/widget_gauge/README.md +0 -50
  148. data/examples/widget_gauge/app.rb +0 -229
  149. data/examples/widget_layout_split/README.md +0 -53
  150. data/examples/widget_layout_split/app.rb +0 -260
  151. data/examples/widget_line_gauge/README.md +0 -50
  152. data/examples/widget_line_gauge/app.rb +0 -219
  153. data/examples/widget_list/README.md +0 -58
  154. data/examples/widget_list/app.rb +0 -384
  155. data/examples/widget_map/README.md +0 -48
  156. data/examples/widget_map/app.rb +0 -95
  157. data/examples/widget_overlay/README.md +0 -45
  158. data/examples/widget_overlay/app.rb +0 -250
  159. data/examples/widget_popup/README.md +0 -45
  160. data/examples/widget_popup/app.rb +0 -106
  161. data/examples/widget_ratatui_logo/README.md +0 -43
  162. data/examples/widget_ratatui_logo/app.rb +0 -104
  163. data/examples/widget_ratatui_mascot/README.md +0 -43
  164. data/examples/widget_ratatui_mascot/app.rb +0 -95
  165. data/examples/widget_rect/README.md +0 -53
  166. data/examples/widget_rect/app.rb +0 -222
  167. data/examples/widget_render/README.md +0 -46
  168. data/examples/widget_render/app.rb +0 -186
  169. data/examples/widget_render/app.rbs +0 -41
  170. data/examples/widget_rich_text/README.md +0 -44
  171. data/examples/widget_rich_text/app.rb +0 -193
  172. data/examples/widget_scroll_text/README.md +0 -46
  173. data/examples/widget_scroll_text/app.rb +0 -109
  174. data/examples/widget_scrollbar/README.md +0 -46
  175. data/examples/widget_scrollbar/app.rb +0 -155
  176. data/examples/widget_sparkline/README.md +0 -51
  177. data/examples/widget_sparkline/app.rb +0 -277
  178. data/examples/widget_style_colors/README.md +0 -43
  179. data/examples/widget_style_colors/app.rb +0 -83
  180. data/examples/widget_table/README.md +0 -57
  181. data/examples/widget_table/app.rb +0 -279
  182. data/examples/widget_tabs/README.md +0 -50
  183. data/examples/widget_tabs/app.rb +0 -183
  184. data/examples/widget_text_width/README.md +0 -44
  185. data/examples/widget_text_width/app.rb +0 -117
  186. data/migrate_to_buffer.rb +0 -145
  187. data/mise.toml +0 -8
  188. data/tasks/autodoc/examples.rb +0 -87
  189. data/tasks/autodoc/member.rb +0 -58
  190. data/tasks/autodoc/name.rb +0 -21
  191. data/tasks/autodoc.rake +0 -21
  192. data/tasks/bump/cargo_lockfile.rb +0 -21
  193. data/tasks/bump/changelog.rb +0 -47
  194. data/tasks/bump/header.rb +0 -32
  195. data/tasks/bump/history.rb +0 -32
  196. data/tasks/bump/links.rb +0 -69
  197. data/tasks/bump/manifest.rb +0 -33
  198. data/tasks/bump/ruby_gem.rb +0 -49
  199. data/tasks/bump/sem_ver.rb +0 -40
  200. data/tasks/bump/unreleased_section.rb +0 -56
  201. data/tasks/bump.rake +0 -51
  202. data/tasks/doc.rake +0 -887
  203. data/tasks/example_viewer.html.erb +0 -172
  204. data/tasks/extension.rake +0 -14
  205. data/tasks/license/headers_md.rb +0 -223
  206. data/tasks/license/headers_rb.rb +0 -210
  207. data/tasks/license/license_utils.rb +0 -130
  208. data/tasks/license/snippets_md.rb +0 -315
  209. data/tasks/license/snippets_rdoc.rb +0 -150
  210. data/tasks/license.rake +0 -91
  211. data/tasks/lint.rake +0 -170
  212. data/tasks/rdoc_config.rb +0 -29
  213. data/tasks/resources/build.yml.erb +0 -60
  214. data/tasks/resources/index.html.erb +0 -141
  215. data/tasks/resources/rubies.yml +0 -7
  216. data/tasks/sourcehut.rake +0 -110
  217. data/tasks/steep.rake +0 -11
  218. data/tasks/terminal_preview/app_screenshot.rb +0 -45
  219. data/tasks/terminal_preview/crash_report.rb +0 -54
  220. data/tasks/terminal_preview/example_app.rb +0 -27
  221. data/tasks/terminal_preview/launcher_script.rb +0 -48
  222. data/tasks/terminal_preview/preview_collection.rb +0 -60
  223. data/tasks/terminal_preview/preview_timing.rb +0 -24
  224. data/tasks/terminal_preview/safety_confirmation.rb +0 -58
  225. data/tasks/terminal_preview/saved_screenshot.rb +0 -56
  226. data/tasks/terminal_preview/system_appearance.rb +0 -13
  227. data/tasks/terminal_preview/terminal_window.rb +0 -138
  228. data/tasks/terminal_preview/window_id.rb +0 -16
  229. data/tasks/terminal_preview.rake +0 -30
  230. data/tasks/test.rake +0 -33
  231. data/tasks/website/index_page.rb +0 -30
  232. data/tasks/website/version.rb +0 -127
  233. data/tasks/website/version_menu.rb +0 -68
  234. data/tasks/website/versioned_documentation.rb +0 -83
  235. data/tasks/website/website.rb +0 -53
  236. data/tasks/website.rake +0 -28
@@ -1,422 +0,0 @@
1
- <!--
2
- SPDX-FileCopyrightText: 2026 Kerrick Long <me@kerricklong.com>
3
- SPDX-License-Identifier: CC-BY-SA-4.0
4
- -->
5
-
6
- # Rust Backend Design (`ratatui_ruby` extension)
7
-
8
- This document describes the internal architecture of the `ratatui_ruby` Rust extension. It is intended for contributors, architects, and AI agents working on the codebase.
9
-
10
- This is the companion document to [Ruby Frontend Design](./ruby_frontend.md). The Ruby layer defines data structures; the Rust layer renders them.
11
-
12
- ## Key Dependencies
13
-
14
- | Crate | Purpose |
15
- |-------|---------|
16
- | `ratatui` | TUI framework providing widgets, layout, and rendering |
17
- | `crossterm` | Cross-platform terminal manipulation (raw mode, events, colors) |
18
- | `magnus` | Ruby FFI bindings for Rust (value extraction, exception handling) |
19
-
20
- **Why `ratatui` vs `ratatui-crossterm`?**
21
-
22
- Ratatui's workspace includes modular crates (`ratatui-crossterm`, `ratatui-core`, etc.) for library authors who need fine-grained dependency control. We use the main `ratatui` crate because:
23
-
24
- 1. We're building an application extension, not a widget library
25
- 2. The main crate includes crossterm backend by default
26
- 3. It provides the complete API surface we need
27
-
28
- ## Guiding Design Principles
29
-
30
- ### 1. Ruby Defines, Rust Renders
31
-
32
- The Rust backend is a pure rendering engine. It receives Ruby objects representing the desired UI state and converts them to Ratatui primitives. It does not own or manage UI state—that responsibility belongs to Ruby.
33
-
34
- **The Contract:**
35
- - Ruby constructs a tree of `Data.define` objects describing the UI
36
- - Ruby calls `RatatuiRuby.draw { |frame| ... }` or passes a widget to `frame.render_widget`
37
- - Rust walks the Ruby object tree via `magnus::Value` and `funcall`
38
- - Rust builds Ratatui widgets and renders them to the terminal buffer
39
-
40
- ### 2. Single Generic Renderer
41
-
42
- The backend implements one generic rendering function that accepts any Ruby `Value` and dispatches based on class name. There is no compile-time knowledge of Ruby types—everything is runtime reflection.
43
-
44
- <!-- SPDX-SnippetBegin -->
45
- <!--
46
- SPDX-FileCopyrightText: 2026 Kerrick Long
47
- SPDX-License-Identifier: MIT-0
48
- -->
49
- ```rust
50
- // rendering.rs
51
- pub fn render_widget(frame: &mut Frame, area: Rect, node: Value) -> Result<(), Error> {
52
- let class_name: String = node.class().name()?.into_owned();
53
-
54
- match class_name.as_str() {
55
- "RatatuiRuby::Widgets::Paragraph" => paragraph::render(frame, area, node),
56
- "RatatuiRuby::Widgets::Block" => block::render(frame, area, node),
57
- "RatatuiRuby::Widgets::Table" => table::render(frame, area, node),
58
- // ... etc
59
- _ => Err(Error::new(
60
- magnus::exception::type_error(),
61
- format!("Unknown widget type: {}", class_name)
62
- ))
63
- }
64
- }
65
- ```
66
- <!-- SPDX-SnippetEnd -->
67
-
68
- ### 3. No Custom Rust Structs for UI
69
-
70
- Do not define Rust structs that mirror Ruby UI components. This would create synchronization problems when Ruby classes change.
71
-
72
- **What We Do:**
73
- <!-- SPDX-SnippetBegin -->
74
- <!--
75
- SPDX-FileCopyrightText: 2026 Kerrick Long
76
- SPDX-License-Identifier: MIT-0
77
- -->
78
- ```rust
79
- // Extract directly from Ruby object
80
- let text: String = node.funcall("text", ())?;
81
- let style_val: Value = node.funcall("style", ())?;
82
- let style = parse_style(style_val)?;
83
- ```
84
- <!-- SPDX-SnippetEnd -->
85
-
86
- **What We Don't Do:**
87
- <!-- SPDX-SnippetBegin -->
88
- <!--
89
- SPDX-FileCopyrightText: 2026 Kerrick Long
90
- SPDX-License-Identifier: MIT-0
91
- -->
92
- ```rust
93
- // NO: Rust struct mirroring Ruby
94
- struct Paragraph {
95
- text: String,
96
- style: Option<Style>,
97
- block: Option<Block>,
98
- }
99
- ```
100
- <!-- SPDX-SnippetEnd -->
101
-
102
- ### 4. Immediate Mode Rendering
103
-
104
- The renderer traverses the Ruby object tree every frame and rebuilds the Ratatui widget tree from scratch. No widget state persists between frames in Rust.
105
-
106
- This mirrors Ratatui's own immediate mode paradigm. The Rust backend is stateless (except for terminal state).
107
-
108
- ### 5. Memory Safety via Value Extraction
109
-
110
- Ruby's GC can move or collect objects at any time. All data extracted from Ruby must be owned (copied) before use, never borrowed.
111
-
112
- <!-- SPDX-SnippetBegin -->
113
- <!--
114
- SPDX-FileCopyrightText: 2026 Kerrick Long
115
- SPDX-License-Identifier: MIT-0
116
- -->
117
- ```rust
118
- // SAFE: Convert to owned String immediately
119
- let text: String = node.funcall::<_, String>("text", ())?.into_owned();
120
-
121
- // UNSAFE: Holding reference across GC-safe point
122
- let text_ref: &str = node.funcall("text", ())?; // DON'T
123
- do_something_that_might_gc();
124
- use(text_ref); // CRASH: text_ref may be invalid
125
- ```
126
- <!-- SPDX-SnippetEnd -->
127
-
128
- ---
129
-
130
- ## Directory Structure
131
-
132
- <!-- SPDX-SnippetBegin -->
133
- <!--
134
- SPDX-FileCopyrightText: 2026 Kerrick Long
135
- SPDX-License-Identifier: MIT-0
136
- -->
137
- ```
138
- ext/ratatui_ruby/src/
139
- ├── lib.rs # Entry point, Ruby module registration
140
- ├── terminal.rs # Global TERMINAL state, init/restore
141
- ├── frame.rs # Frame wrapper for render_widget, area access
142
- ├── events.rs # Event polling, crossterm -> Ruby conversion
143
- ├── style.rs # Style/Color parsing from Ruby values
144
- ├── text.rs # Span/Line parsing
145
- ├── rendering.rs # Central dispatcher, class name -> widget module
146
- └── widgets/ # Per-widget rendering modules
147
- ├── mod.rs # Re-exports all widget modules
148
- ├── paragraph.rs
149
- ├── block.rs
150
- ├── table.rs
151
- ├── list.rs
152
- ├── canvas.rs
153
- └── ...
154
- ```
155
- <!-- SPDX-SnippetEnd -->
156
-
157
- ---
158
-
159
- ## Module Responsibilities
160
-
161
- ### `lib.rs` — Entry Point
162
-
163
- Defines the Ruby module hierarchy using `magnus` and exports public functions (`init_terminal`, `restore_terminal`, `draw`, `poll_event`, `get_cell_at`).
164
-
165
- ### `terminal.rs` — Terminal State
166
-
167
- Manages the global `TERMINAL` singleton (mutex-wrapped `CrosstermBackend<Stdout>`).
168
-
169
- Key functions:
170
- - `init()` — Enter raw mode, enable mouse capture, switch to alternate screen
171
- - `restore()` — Disable raw mode, restore main screen
172
- - `get_cell_at(x, y)` — Return buffer cell as Ruby `Buffer::Cell` object
173
-
174
- **Safety Note:** The terminal is a global mutable resource. All access goes through a mutex. Holding the lock across Ruby calls risks deadlock—release the lock before calling back into Ruby.
175
-
176
- ### `frame.rs` — Frame Wrapper
177
-
178
- Wraps Ratatui's `Frame` struct for safe Ruby access. The `Frame` reference is only valid inside the `draw` closure. The `FrameWrapper` tracks validity and raises `Safety` error if used after the closure returns.
179
-
180
- ### `events.rs` — Event Conversion
181
-
182
- Polls crossterm events and converts them to Ruby `Event::*` objects. Handles key, mouse, resize, paste, and focus events.
183
-
184
- ### `style.rs` — Style Parsing
185
-
186
- Pure functions for extracting style information from Ruby values. Handles `parse_style`, `parse_color` (symbols, integers 0-255, hex strings), and `parse_modifiers`.
187
-
188
- ### `rendering.rs` — Central Dispatcher
189
-
190
- The routing layer that maps Ruby class names to widget renderers:
191
-
192
- <!-- SPDX-SnippetBegin -->
193
- <!--
194
- SPDX-FileCopyrightText: 2026 Kerrick Long
195
- SPDX-License-Identifier: MIT-0
196
- -->
197
- ```rust
198
- pub fn render_widget(frame: &mut Frame, area: Rect, node: Value) -> Result<(), Error> {
199
- let class_name: String = node.class().name()?.into_owned();
200
-
201
- match class_name.as_str() {
202
- // Widgets module
203
- "RatatuiRuby::Widgets::Paragraph" => widgets::paragraph::render(frame, area, node),
204
- "RatatuiRuby::Widgets::Block" => widgets::block::render(frame, area, node),
205
- "RatatuiRuby::Widgets::Table" => widgets::table::render(frame, area, node),
206
- "RatatuiRuby::Widgets::List" => widgets::list::render(frame, area, node),
207
- "RatatuiRuby::Widgets::Tabs" => widgets::tabs::render(frame, area, node),
208
- "RatatuiRuby::Widgets::Gauge" => widgets::gauge::render(frame, area, node),
209
- "RatatuiRuby::Widgets::Chart" => widgets::chart::render(frame, area, node),
210
- "RatatuiRuby::Widgets::Canvas" => widgets::canvas::render(frame, area, node),
211
- "RatatuiRuby::Widgets::Scrollbar" => widgets::scrollbar::render(frame, area, node),
212
- "RatatuiRuby::Widgets::Calendar" => widgets::calendar::render(frame, area, node),
213
- // ... all widgets
214
-
215
- // Special widgets
216
- "RatatuiRuby::Widgets::Clear" => widgets::clear::render(frame, area, node),
217
- "RatatuiRuby::Widgets::Cursor" => widgets::cursor::render(frame, area, node),
218
-
219
- // Custom widgets (Ruby escape hatch)
220
- _ if has_render_method(node) => widgets::custom::render(frame, area, node),
221
-
222
- _ => Err(Error::new(
223
- magnus::exception::type_error(),
224
- format!("Unknown widget type: {}", class_name)
225
- ))
226
- }
227
- }
228
- ```
229
- <!-- SPDX-SnippetEnd -->
230
-
231
- **Namespace Pattern:** All built-in widgets use the `RatatuiRuby::Widgets::*` namespace. The dispatcher matches on full class names, not prefixes.
232
-
233
- ### `widgets/*.rs` — Widget Renderers
234
-
235
- Each widget has its own module with a standard interface:
236
-
237
- <!-- SPDX-SnippetBegin -->
238
- <!--
239
- SPDX-FileCopyrightText: 2026 Kerrick Long
240
- SPDX-License-Identifier: MIT-0
241
- -->
242
- ```rust
243
- // widgets/paragraph.rs
244
- pub fn render(frame: &mut Frame, area: Rect, node: Value) -> Result<(), Error> {
245
- // 1. Extract properties from Ruby object
246
- let text = parse_text(node.funcall("text", ())?)?;
247
- let style = parse_style(node.funcall("style", ())?)?;
248
- let alignment = parse_alignment(node.funcall("alignment", ())?)?;
249
- let block_val: Value = node.funcall("block", ())?;
250
-
251
- // 2. Build Ratatui widget
252
- let mut paragraph = Paragraph::new(text)
253
- .style(style)
254
- .alignment(alignment);
255
-
256
- // 3. Handle optional block wrapper
257
- if !block_val.is_nil() {
258
- paragraph = paragraph.block(parse_block(block_val)?);
259
- }
260
-
261
- // 4. Render
262
- frame.render_widget(paragraph, area);
263
- Ok(())
264
- }
265
- ```
266
- <!-- SPDX-SnippetEnd -->
267
-
268
- ---
269
-
270
- ## Adding a New Widget
271
-
272
- ### Step 1: Create the Widget Module
273
-
274
- <!-- SPDX-SnippetBegin -->
275
- <!--
276
- SPDX-FileCopyrightText: 2026 Kerrick Long
277
- SPDX-License-Identifier: MIT-0
278
- -->
279
- ```rust
280
- // src/widgets/my_widget.rs
281
-
282
- use magnus::{Error, Value};
283
- use ratatui::prelude::*;
284
-
285
- use crate::style::parse_style;
286
-
287
- pub fn render(frame: &mut Frame, area: Rect, node: Value) -> Result<(), Error> {
288
- // Extract properties
289
- let content: String = node.funcall::<_, String>("content", ())?.into_owned();
290
- let style = parse_style(node.funcall("style", ())?)?;
291
-
292
- // Build and render
293
- let widget = MyWidget::new(content).style(style);
294
- frame.render_widget(widget, area);
295
-
296
- Ok(())
297
- }
298
- ```
299
- <!-- SPDX-SnippetEnd -->
300
-
301
- ### Step 2: Register in `widgets/mod.rs`
302
-
303
- <!-- SPDX-SnippetBegin -->
304
- <!--
305
- SPDX-FileCopyrightText: 2026 Kerrick Long
306
- SPDX-License-Identifier: MIT-0
307
- -->
308
- ```rust
309
- pub mod my_widget;
310
- ```
311
- <!-- SPDX-SnippetEnd -->
312
-
313
- ### Step 3: Add Dispatch Arm in `rendering.rs`
314
-
315
- <!-- SPDX-SnippetBegin -->
316
- <!--
317
- SPDX-FileCopyrightText: 2026 Kerrick Long
318
- SPDX-License-Identifier: MIT-0
319
- -->
320
- ```rust
321
- "RatatuiRuby::Widgets::MyWidget" => widgets::my_widget::render(frame, area, node),
322
- ```
323
- <!-- SPDX-SnippetEnd -->
324
-
325
- ### Step 4: Test
326
-
327
- Run `cargo test` for Rust unit tests, then `rake test` for Ruby integration tests.
328
-
329
- ---
330
-
331
- ## Stateful Widget Rendering
332
-
333
- Some widgets (List, Table, Scrollbar) support stateful rendering where a mutable State object tracks scroll position and selection.
334
-
335
- ### The Pattern
336
-
337
- <!-- SPDX-SnippetBegin -->
338
- <!--
339
- SPDX-FileCopyrightText: 2026 Kerrick Long
340
- SPDX-License-Identifier: MIT-0
341
- -->
342
- ```rust
343
- pub fn render_stateful_widget(
344
- frame: &mut Frame,
345
- area: Rect,
346
- widget_node: Value,
347
- state_node: Value
348
- ) -> Result<(), Error> {
349
- // 1. Build the widget (immutable configuration)
350
- let list = build_list(widget_node)?;
351
-
352
- // 2. Extract mutable state
353
- let mut state = ListState::default();
354
- if let Ok(selected) = state_node.funcall::<_, Option<i64>>("selected", ()) {
355
- state.select(selected.map(|i| i as usize));
356
- }
357
-
358
- // 3. Render with state (Ratatui may mutate offset)
359
- frame.render_stateful_widget(list, area, &mut state);
360
-
361
- // 4. Write computed values back to Ruby state object
362
- state_node.funcall::<_, Value>("set_offset", (state.offset() as i64,))?;
363
-
364
- Ok(())
365
- }
366
- ```
367
- <!-- SPDX-SnippetEnd -->
368
-
369
- **State Precedence:** When using stateful rendering, the State object's values take precedence over Widget properties. This is documented in Ruby.
370
-
371
- ---
372
-
373
- ## Custom Widget Escape Hatch
374
-
375
- Ruby users can define custom widgets that implement a `render(area)` method returning an array of `Draw` commands. The dispatcher detects a `render` method and calls it, processing the returned commands to manipulate the buffer directly. This is the "escape hatch" for functionality not yet wrapped by built-in widgets.
376
-
377
- ---
378
-
379
- ## Error Handling
380
-
381
- All Rust functions that can fail return `Result<T, magnus::Error>`. Magnus automatically converts these to Ruby exceptions.
382
-
383
- **Error Types:**
384
-
385
- | Scenario | Ruby Exception | Notes |
386
- |----------|---------------|-------|
387
- | Invalid argument | `ArgumentError` | Wrong type, out of range |
388
- | Unknown widget | `TypeError` | Class name not in dispatch table |
389
- | Terminal not initialized | `RatatuiRuby::Error::Terminal` | Custom exception class |
390
- | Frame used after draw block | `RatatuiRuby::Error::Safety` | Memory safety violation |
391
-
392
- ---
393
-
394
- ## Testing Strategy
395
-
396
- ### Rust Unit Tests (`cargo test`)
397
-
398
- Test pure parsing functions that don't require Ruby VM. Most tests require Ruby VM via magnus, which means they run in integration test style.
399
-
400
- ### Ruby Integration Tests (`rake test`)
401
-
402
- The primary testing strategy. Ruby tests exercise the full stack and verify end-to-end behavior without testing Rust internals.
403
-
404
- ### Buffer Verification
405
-
406
- For Rust-level rendering tests, use Ratatui's `TestBackend` or `Buffer` to assert cells are filled correctly.
407
-
408
- ---
409
-
410
- ## Performance Considerations
411
-
412
- ### Avoid Repeated `funcall`
413
-
414
- Each `funcall` crosses the Ruby/Rust boundary. Cache results when accessing the same property multiple times rather than calling `funcall` repeatedly.
415
-
416
- ### String Ownership
417
-
418
- Always convert to owned `String` immediately via `into_owned()` to avoid GC-related memory safety issues.
419
-
420
- ### Batch Collection Iteration
421
-
422
- When processing Ruby arrays, collect all values into a `Vec<Value>` before processing to avoid holding references across iterations.
@@ -1,11 +0,0 @@
1
- <!--
2
- SPDX-FileCopyrightText: 2025 Kerrick Long <me@kerricklong.com>
3
- SPDX-License-Identifier: CC-BY-SA-4.0
4
- -->
5
-
6
- # Design Documentation
7
-
8
- This directory contains detailed design documents for the `ratatui_ruby` project.
9
-
10
- * [Rust Backend Design](design/rust_backend.md): Details on the internal architecture of the Rust extension (`ext/ratatui_ruby`), including module structure, rendering pipeline, and widget implementation guide.
11
- * [Ruby Frontend Design](design/ruby_frontend.md): Explains the Data-Driven UI, Immediate Mode paradigm, and the View Tree structure.