ratatui_ruby 1.2.0 → 1.2.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 (259) 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 -255
  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 -147
  13. data/CHANGELOG.md +0 -751
  14. data/README.md +0 -187
  15. data/README.rdoc +0 -302
  16. data/Rakefile +0 -11
  17. data/Steepfile +0 -50
  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 -448
  27. data/doc/contributors/design/rust_backend.md +0 -434
  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/releasing.md +0 -215
  33. data/doc/contributors/todo/align/api_completeness_audit-finished.md +0 -381
  34. data/doc/contributors/todo/align/api_completeness_audit-unfinished.md +0 -200
  35. data/doc/contributors/todo/align/term.md +0 -351
  36. data/doc/contributors/todo/align/terminal.md +0 -647
  37. data/doc/contributors/todo/future_work.md +0 -169
  38. data/doc/contributors/upstream_requests/paragraph_span_rects.md +0 -259
  39. data/doc/contributors/upstream_requests/tab_rects.md +0 -173
  40. data/doc/contributors/upstream_requests/title_rects.md +0 -132
  41. data/doc/custom.css +0 -22
  42. data/doc/getting_started/quickstart.md +0 -291
  43. data/doc/getting_started/why.md +0 -93
  44. data/doc/images/app_all_events.png +0 -0
  45. data/doc/images/app_cli_rich_moments.gif +0 -0
  46. data/doc/images/app_color_picker.png +0 -0
  47. data/doc/images/app_debugging_showcase.gif +0 -0
  48. data/doc/images/app_debugging_showcase.png +0 -0
  49. data/doc/images/app_external_editor.gif +0 -0
  50. data/doc/images/app_login_form.png +0 -0
  51. data/doc/images/app_stateful_interaction.png +0 -0
  52. data/doc/images/verify_quickstart_dsl.png +0 -0
  53. data/doc/images/verify_quickstart_layout.png +0 -0
  54. data/doc/images/verify_quickstart_lifecycle.png +0 -0
  55. data/doc/images/verify_readme_usage.png +0 -0
  56. data/doc/images/widget_barchart.png +0 -0
  57. data/doc/images/widget_block.png +0 -0
  58. data/doc/images/widget_box.png +0 -0
  59. data/doc/images/widget_calendar.png +0 -0
  60. data/doc/images/widget_canvas.png +0 -0
  61. data/doc/images/widget_cell.png +0 -0
  62. data/doc/images/widget_center.png +0 -0
  63. data/doc/images/widget_chart.png +0 -0
  64. data/doc/images/widget_gauge.png +0 -0
  65. data/doc/images/widget_layout_split.png +0 -0
  66. data/doc/images/widget_line_gauge.png +0 -0
  67. data/doc/images/widget_list.png +0 -0
  68. data/doc/images/widget_map.png +0 -0
  69. data/doc/images/widget_overlay.png +0 -0
  70. data/doc/images/widget_popup.png +0 -0
  71. data/doc/images/widget_ratatui_logo.png +0 -0
  72. data/doc/images/widget_ratatui_mascot.png +0 -0
  73. data/doc/images/widget_rect.png +0 -0
  74. data/doc/images/widget_render.png +0 -0
  75. data/doc/images/widget_rich_text.png +0 -0
  76. data/doc/images/widget_scroll_text.png +0 -0
  77. data/doc/images/widget_scrollbar.png +0 -0
  78. data/doc/images/widget_sparkline.png +0 -0
  79. data/doc/images/widget_style_colors.png +0 -0
  80. data/doc/images/widget_table.png +0 -0
  81. data/doc/images/widget_tabs.png +0 -0
  82. data/doc/images/widget_text_width.png +0 -0
  83. data/doc/index.md +0 -34
  84. data/doc/troubleshooting/async.md +0 -4
  85. data/doc/troubleshooting/terminal_limitations.md +0 -131
  86. data/doc/troubleshooting/tui_output.md +0 -197
  87. data/examples/app_all_events/README.md +0 -114
  88. data/examples/app_all_events/app.rb +0 -98
  89. data/examples/app_all_events/model/app_model.rb +0 -159
  90. data/examples/app_all_events/model/event_color_cycle.rb +0 -43
  91. data/examples/app_all_events/model/event_entry.rb +0 -94
  92. data/examples/app_all_events/model/msg.rb +0 -39
  93. data/examples/app_all_events/model/timestamp.rb +0 -56
  94. data/examples/app_all_events/update.rb +0 -75
  95. data/examples/app_all_events/view/app_view.rb +0 -80
  96. data/examples/app_all_events/view/controls_view.rb +0 -54
  97. data/examples/app_all_events/view/counts_view.rb +0 -61
  98. data/examples/app_all_events/view/live_view.rb +0 -72
  99. data/examples/app_all_events/view/log_view.rb +0 -57
  100. data/examples/app_all_events/view.rb +0 -9
  101. data/examples/app_cli_rich_moments/README.md +0 -81
  102. data/examples/app_cli_rich_moments/app.rb +0 -189
  103. data/examples/app_color_picker/README.md +0 -156
  104. data/examples/app_color_picker/app.rb +0 -76
  105. data/examples/app_color_picker/clipboard.rb +0 -86
  106. data/examples/app_color_picker/color.rb +0 -193
  107. data/examples/app_color_picker/controls.rb +0 -92
  108. data/examples/app_color_picker/copy_dialog.rb +0 -168
  109. data/examples/app_color_picker/export_pane.rb +0 -128
  110. data/examples/app_color_picker/harmony.rb +0 -58
  111. data/examples/app_color_picker/input.rb +0 -176
  112. data/examples/app_color_picker/main_container.rb +0 -180
  113. data/examples/app_color_picker/palette.rb +0 -111
  114. data/examples/app_debugging_showcase/README.md +0 -119
  115. data/examples/app_debugging_showcase/app.rb +0 -318
  116. data/examples/app_external_editor/README.md +0 -62
  117. data/examples/app_external_editor/app.rb +0 -344
  118. data/examples/app_login_form/README.md +0 -58
  119. data/examples/app_login_form/app.rb +0 -109
  120. data/examples/app_stateful_interaction/README.md +0 -35
  121. data/examples/app_stateful_interaction/app.rb +0 -328
  122. data/examples/timeout_demo.rb +0 -45
  123. data/examples/verify_quickstart_dsl/README.md +0 -55
  124. data/examples/verify_quickstart_dsl/app.rb +0 -49
  125. data/examples/verify_quickstart_layout/README.md +0 -77
  126. data/examples/verify_quickstart_layout/app.rb +0 -73
  127. data/examples/verify_quickstart_lifecycle/README.md +0 -68
  128. data/examples/verify_quickstart_lifecycle/app.rb +0 -62
  129. data/examples/verify_readme_usage/README.md +0 -49
  130. data/examples/verify_readme_usage/app.rb +0 -42
  131. data/examples/verify_website_managed/README.md +0 -48
  132. data/examples/verify_website_managed/app.rb +0 -36
  133. data/examples/verify_website_menu/README.md +0 -60
  134. data/examples/verify_website_menu/app.rb +0 -84
  135. data/examples/verify_website_spinner/README.md +0 -44
  136. data/examples/verify_website_spinner/app.rb +0 -34
  137. data/examples/widget_barchart/README.md +0 -58
  138. data/examples/widget_barchart/app.rb +0 -240
  139. data/examples/widget_block/README.md +0 -44
  140. data/examples/widget_block/app.rb +0 -258
  141. data/examples/widget_box/README.md +0 -54
  142. data/examples/widget_box/app.rb +0 -255
  143. data/examples/widget_calendar/README.md +0 -48
  144. data/examples/widget_calendar/app.rb +0 -115
  145. data/examples/widget_canvas/README.md +0 -31
  146. data/examples/widget_canvas/app.rb +0 -130
  147. data/examples/widget_cell/README.md +0 -45
  148. data/examples/widget_cell/app.rb +0 -112
  149. data/examples/widget_center/README.md +0 -33
  150. data/examples/widget_center/app.rb +0 -118
  151. data/examples/widget_chart/README.md +0 -50
  152. data/examples/widget_chart/app.rb +0 -220
  153. data/examples/widget_gauge/README.md +0 -50
  154. data/examples/widget_gauge/app.rb +0 -229
  155. data/examples/widget_layout_split/README.md +0 -53
  156. data/examples/widget_layout_split/app.rb +0 -260
  157. data/examples/widget_line_gauge/README.md +0 -50
  158. data/examples/widget_line_gauge/app.rb +0 -219
  159. data/examples/widget_list/README.md +0 -58
  160. data/examples/widget_list/app.rb +0 -382
  161. data/examples/widget_map/README.md +0 -48
  162. data/examples/widget_map/app.rb +0 -95
  163. data/examples/widget_overlay/README.md +0 -45
  164. data/examples/widget_overlay/app.rb +0 -250
  165. data/examples/widget_popup/README.md +0 -45
  166. data/examples/widget_popup/app.rb +0 -106
  167. data/examples/widget_ratatui_logo/README.md +0 -43
  168. data/examples/widget_ratatui_logo/app.rb +0 -104
  169. data/examples/widget_ratatui_mascot/README.md +0 -43
  170. data/examples/widget_ratatui_mascot/app.rb +0 -95
  171. data/examples/widget_rect/README.md +0 -53
  172. data/examples/widget_rect/app.rb +0 -222
  173. data/examples/widget_render/README.md +0 -46
  174. data/examples/widget_render/app.rb +0 -186
  175. data/examples/widget_render/app.rbs +0 -41
  176. data/examples/widget_rich_text/README.md +0 -44
  177. data/examples/widget_rich_text/app.rb +0 -193
  178. data/examples/widget_scroll_text/README.md +0 -46
  179. data/examples/widget_scroll_text/app.rb +0 -109
  180. data/examples/widget_scrollbar/README.md +0 -46
  181. data/examples/widget_scrollbar/app.rb +0 -155
  182. data/examples/widget_sparkline/README.md +0 -51
  183. data/examples/widget_sparkline/app.rb +0 -277
  184. data/examples/widget_style_colors/README.md +0 -43
  185. data/examples/widget_style_colors/app.rb +0 -83
  186. data/examples/widget_table/README.md +0 -57
  187. data/examples/widget_table/app.rb +0 -285
  188. data/examples/widget_tabs/README.md +0 -50
  189. data/examples/widget_tabs/app.rb +0 -183
  190. data/examples/widget_text_width/README.md +0 -44
  191. data/examples/widget_text_width/app.rb +0 -117
  192. data/migrate_to_buffer.rb +0 -145
  193. data/mise.toml +0 -8
  194. data/tasks/autodoc/examples.rb +0 -87
  195. data/tasks/autodoc/member.rb +0 -58
  196. data/tasks/autodoc/name.rb +0 -21
  197. data/tasks/autodoc.rake +0 -21
  198. data/tasks/bump/bump_workflow.rb +0 -49
  199. data/tasks/bump/cargo_lockfile.rb +0 -21
  200. data/tasks/bump/changelog.rb +0 -104
  201. data/tasks/bump/header.rb +0 -32
  202. data/tasks/bump/history.rb +0 -32
  203. data/tasks/bump/links.rb +0 -69
  204. data/tasks/bump/manifest.rb +0 -33
  205. data/tasks/bump/patch_release.rb +0 -19
  206. data/tasks/bump/release_branch.rb +0 -17
  207. data/tasks/bump/release_from_trunk.rb +0 -49
  208. data/tasks/bump/repository.rb +0 -54
  209. data/tasks/bump/ruby_gem.rb +0 -29
  210. data/tasks/bump/sem_ver.rb +0 -44
  211. data/tasks/bump/unreleased_section.rb +0 -73
  212. data/tasks/bump.rake +0 -61
  213. data/tasks/doc/documentation.rb +0 -59
  214. data/tasks/doc/link/file_url.rb +0 -30
  215. data/tasks/doc/link/relative_path.rb +0 -61
  216. data/tasks/doc/link/web_url.rb +0 -55
  217. data/tasks/doc/link.rb +0 -52
  218. data/tasks/doc/link_audit.rb +0 -116
  219. data/tasks/doc/problem.rb +0 -40
  220. data/tasks/doc/source_file.rb +0 -93
  221. data/tasks/doc.rake +0 -905
  222. data/tasks/example_viewer.html.erb +0 -172
  223. data/tasks/extension.rake +0 -14
  224. data/tasks/license/headers_md.rb +0 -223
  225. data/tasks/license/headers_rb.rb +0 -210
  226. data/tasks/license/license_utils.rb +0 -130
  227. data/tasks/license/snippets_md.rb +0 -315
  228. data/tasks/license/snippets_rdoc.rb +0 -150
  229. data/tasks/license.rake +0 -91
  230. data/tasks/lint.rake +0 -170
  231. data/tasks/rbs_predicates/predicate_catalog.rb +0 -52
  232. data/tasks/rbs_predicates/predicate_tests.rb +0 -124
  233. data/tasks/rbs_predicates/rbs_signature.rb +0 -63
  234. data/tasks/rbs_predicates.rake +0 -31
  235. data/tasks/rdoc_config.rb +0 -29
  236. data/tasks/resources/build.yml.erb +0 -60
  237. data/tasks/resources/index.html.erb +0 -141
  238. data/tasks/resources/rubies.yml +0 -7
  239. data/tasks/sourcehut.rake +0 -122
  240. data/tasks/steep.rake +0 -11
  241. data/tasks/terminal_preview/app_screenshot.rb +0 -45
  242. data/tasks/terminal_preview/crash_report.rb +0 -54
  243. data/tasks/terminal_preview/example_app.rb +0 -27
  244. data/tasks/terminal_preview/launcher_script.rb +0 -48
  245. data/tasks/terminal_preview/preview_collection.rb +0 -60
  246. data/tasks/terminal_preview/preview_timing.rb +0 -24
  247. data/tasks/terminal_preview/safety_confirmation.rb +0 -58
  248. data/tasks/terminal_preview/saved_screenshot.rb +0 -56
  249. data/tasks/terminal_preview/system_appearance.rb +0 -13
  250. data/tasks/terminal_preview/terminal_window.rb +0 -138
  251. data/tasks/terminal_preview/window_id.rb +0 -16
  252. data/tasks/terminal_preview.rake +0 -30
  253. data/tasks/test.rake +0 -36
  254. data/tasks/website/index_page.rb +0 -30
  255. data/tasks/website/version.rb +0 -122
  256. data/tasks/website/version_menu.rb +0 -68
  257. data/tasks/website/versioned_documentation.rb +0 -83
  258. data/tasks/website/website.rb +0 -53
  259. data/tasks/website.rake +0 -28
@@ -1,434 +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
- **Lifecycle 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
- **Crossterm Capability Queries:**
175
-
176
- These functions expose crossterm's terminal capability detection to Ruby. In the three-tier architecture, they'll be surfaced via the `Crossterm::` namespace:
177
-
178
- - `terminal_window_size()` — Returns `(columns, rows, pixel_width, pixel_height)` via `crossterm::terminal::window_size()`
179
- - `available_color_count()` — Returns color depth (8/256/65535) via crossterm's `COLORTERM`/`TERM` detection
180
- - `supports_keyboard_enhancement()` — Queries Kitty keyboard protocol support
181
- - `force_color_output(enable)` — Overrides `NO_COLOR` detection via `crossterm::style::force_color_output()`
182
-
183
- **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.
184
-
185
- ### `frame.rs` — Frame Wrapper
186
-
187
- 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.
188
-
189
- ### `events.rs` — Event Conversion
190
-
191
- Polls crossterm events and converts them to Ruby `Event::*` objects. Handles key, mouse, resize, paste, and focus events.
192
-
193
- ### `style.rs` — Style Parsing
194
-
195
- Pure functions for extracting style information from Ruby values. Handles `parse_style`, `parse_color` (symbols, integers 0-255, hex strings), and `parse_modifiers`.
196
-
197
- ### `rendering.rs` — Central Dispatcher
198
-
199
- The routing layer that maps Ruby class names to widget renderers:
200
-
201
- <!-- SPDX-SnippetBegin -->
202
- <!--
203
- SPDX-FileCopyrightText: 2026 Kerrick Long
204
- SPDX-License-Identifier: MIT-0
205
- -->
206
- ```rust
207
- pub fn render_widget(frame: &mut Frame, area: Rect, node: Value) -> Result<(), Error> {
208
- let class_name: String = node.class().name()?.into_owned();
209
-
210
- match class_name.as_str() {
211
- // Widgets module
212
- "RatatuiRuby::Widgets::Paragraph" => widgets::paragraph::render(frame, area, node),
213
- "RatatuiRuby::Widgets::Block" => widgets::block::render(frame, area, node),
214
- "RatatuiRuby::Widgets::Table" => widgets::table::render(frame, area, node),
215
- "RatatuiRuby::Widgets::List" => widgets::list::render(frame, area, node),
216
- "RatatuiRuby::Widgets::Tabs" => widgets::tabs::render(frame, area, node),
217
- "RatatuiRuby::Widgets::Gauge" => widgets::gauge::render(frame, area, node),
218
- "RatatuiRuby::Widgets::Chart" => widgets::chart::render(frame, area, node),
219
- "RatatuiRuby::Widgets::Canvas" => widgets::canvas::render(frame, area, node),
220
- "RatatuiRuby::Widgets::Scrollbar" => widgets::scrollbar::render(frame, area, node),
221
- "RatatuiRuby::Widgets::Calendar" => widgets::calendar::render(frame, area, node),
222
- // ... all widgets
223
-
224
- // Special widgets
225
- "RatatuiRuby::Widgets::Clear" => widgets::clear::render(frame, area, node),
226
- "RatatuiRuby::Widgets::Cursor" => widgets::cursor::render(frame, area, node),
227
-
228
- // Custom widgets (Ruby escape hatch)
229
- _ if has_render_method(node) => widgets::custom::render(frame, area, node),
230
-
231
- _ => Err(Error::new(
232
- magnus::exception::type_error(),
233
- format!("Unknown widget type: {}", class_name)
234
- ))
235
- }
236
- }
237
- ```
238
- <!-- SPDX-SnippetEnd -->
239
-
240
- **Namespace Pattern:** All built-in widgets use the `RatatuiRuby::Widgets::*` namespace. The dispatcher matches on full class names, not prefixes.
241
-
242
- > [!NOTE]
243
- > The dispatcher will be updated to also match `Ratatui::Widgets::*` class names when the three-tier namespace architecture rolls out. This is additive—existing `RatatuiRuby::` names will continue to work. See [Ruby Frontend Design](./ruby_frontend.md) for details.
244
-
245
- ### `widgets/*.rs` — Widget Renderers
246
-
247
- Each widget has its own module with a standard interface:
248
-
249
- <!-- SPDX-SnippetBegin -->
250
- <!--
251
- SPDX-FileCopyrightText: 2026 Kerrick Long
252
- SPDX-License-Identifier: MIT-0
253
- -->
254
- ```rust
255
- // widgets/paragraph.rs
256
- pub fn render(frame: &mut Frame, area: Rect, node: Value) -> Result<(), Error> {
257
- // 1. Extract properties from Ruby object
258
- let text = parse_text(node.funcall("text", ())?)?;
259
- let style = parse_style(node.funcall("style", ())?)?;
260
- let alignment = parse_alignment(node.funcall("alignment", ())?)?;
261
- let block_val: Value = node.funcall("block", ())?;
262
-
263
- // 2. Build Ratatui widget
264
- let mut paragraph = Paragraph::new(text)
265
- .style(style)
266
- .alignment(alignment);
267
-
268
- // 3. Handle optional block wrapper
269
- if !block_val.is_nil() {
270
- paragraph = paragraph.block(parse_block(block_val)?);
271
- }
272
-
273
- // 4. Render
274
- frame.render_widget(paragraph, area);
275
- Ok(())
276
- }
277
- ```
278
- <!-- SPDX-SnippetEnd -->
279
-
280
- ---
281
-
282
- ## Adding a New Widget
283
-
284
- ### Step 1: Create the Widget Module
285
-
286
- <!-- SPDX-SnippetBegin -->
287
- <!--
288
- SPDX-FileCopyrightText: 2026 Kerrick Long
289
- SPDX-License-Identifier: MIT-0
290
- -->
291
- ```rust
292
- // src/widgets/my_widget.rs
293
-
294
- use magnus::{Error, Value};
295
- use ratatui::prelude::*;
296
-
297
- use crate::style::parse_style;
298
-
299
- pub fn render(frame: &mut Frame, area: Rect, node: Value) -> Result<(), Error> {
300
- // Extract properties
301
- let content: String = node.funcall::<_, String>("content", ())?.into_owned();
302
- let style = parse_style(node.funcall("style", ())?)?;
303
-
304
- // Build and render
305
- let widget = MyWidget::new(content).style(style);
306
- frame.render_widget(widget, area);
307
-
308
- Ok(())
309
- }
310
- ```
311
- <!-- SPDX-SnippetEnd -->
312
-
313
- ### Step 2: Register in `widgets/mod.rs`
314
-
315
- <!-- SPDX-SnippetBegin -->
316
- <!--
317
- SPDX-FileCopyrightText: 2026 Kerrick Long
318
- SPDX-License-Identifier: MIT-0
319
- -->
320
- ```rust
321
- pub mod my_widget;
322
- ```
323
- <!-- SPDX-SnippetEnd -->
324
-
325
- ### Step 3: Add Dispatch Arm in `rendering.rs`
326
-
327
- <!-- SPDX-SnippetBegin -->
328
- <!--
329
- SPDX-FileCopyrightText: 2026 Kerrick Long
330
- SPDX-License-Identifier: MIT-0
331
- -->
332
- ```rust
333
- "RatatuiRuby::Widgets::MyWidget" => widgets::my_widget::render(frame, area, node),
334
- ```
335
- <!-- SPDX-SnippetEnd -->
336
-
337
- ### Step 4: Test
338
-
339
- Run `cargo test` for Rust unit tests, then `rake test` for Ruby integration tests.
340
-
341
- ---
342
-
343
- ## Stateful Widget Rendering
344
-
345
- Some widgets (List, Table, Scrollbar) support stateful rendering where a mutable State object tracks scroll position and selection.
346
-
347
- ### The Pattern
348
-
349
- <!-- SPDX-SnippetBegin -->
350
- <!--
351
- SPDX-FileCopyrightText: 2026 Kerrick Long
352
- SPDX-License-Identifier: MIT-0
353
- -->
354
- ```rust
355
- pub fn render_stateful_widget(
356
- frame: &mut Frame,
357
- area: Rect,
358
- widget_node: Value,
359
- state_node: Value
360
- ) -> Result<(), Error> {
361
- // 1. Build the widget (immutable configuration)
362
- let list = build_list(widget_node)?;
363
-
364
- // 2. Extract mutable state
365
- let mut state = ListState::default();
366
- if let Ok(selected) = state_node.funcall::<_, Option<i64>>("selected", ()) {
367
- state.select(selected.map(|i| i as usize));
368
- }
369
-
370
- // 3. Render with state (Ratatui may mutate offset)
371
- frame.render_stateful_widget(list, area, &mut state);
372
-
373
- // 4. Write computed values back to Ruby state object
374
- state_node.funcall::<_, Value>("set_offset", (state.offset() as i64,))?;
375
-
376
- Ok(())
377
- }
378
- ```
379
- <!-- SPDX-SnippetEnd -->
380
-
381
- **State Precedence:** When using stateful rendering, the State object's values take precedence over Widget properties. This is documented in Ruby.
382
-
383
- ---
384
-
385
- ## Custom Widget Escape Hatch
386
-
387
- 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.
388
-
389
- ---
390
-
391
- ## Error Handling
392
-
393
- All Rust functions that can fail return `Result<T, magnus::Error>`. Magnus automatically converts these to Ruby exceptions.
394
-
395
- **Error Types:**
396
-
397
- | Scenario | Ruby Exception | Notes |
398
- |----------|---------------|-------|
399
- | Invalid argument | `ArgumentError` | Wrong type, out of range |
400
- | Unknown widget | `TypeError` | Class name not in dispatch table |
401
- | Terminal not initialized | `RatatuiRuby::Error::Terminal` | Custom exception class |
402
- | Frame used after draw block | `RatatuiRuby::Error::Safety` | Memory safety violation |
403
-
404
- ---
405
-
406
- ## Testing Strategy
407
-
408
- ### Rust Unit Tests (`cargo test`)
409
-
410
- 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.
411
-
412
- ### Ruby Integration Tests (`rake test`)
413
-
414
- The primary testing strategy. Ruby tests exercise the full stack and verify end-to-end behavior without testing Rust internals.
415
-
416
- ### Buffer Verification
417
-
418
- For Rust-level rendering tests, use Ratatui's `TestBackend` or `Buffer` to assert cells are filled correctly.
419
-
420
- ---
421
-
422
- ## Performance Considerations
423
-
424
- ### Avoid Repeated `funcall`
425
-
426
- Each `funcall` crosses the Ruby/Rust boundary. Cache results when accessing the same property multiple times rather than calling `funcall` repeatedly.
427
-
428
- ### String Ownership
429
-
430
- Always convert to owned `String` immediately via `into_owned()` to avoid GC-related memory safety issues.
431
-
432
- ### Batch Collection Iteration
433
-
434
- 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.