ratatui_ruby 0.5.0 → 0.7.0

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 (311) hide show
  1. checksums.yaml +4 -4
  2. data/.builds/ruby-3.2.yml +1 -1
  3. data/.builds/ruby-3.3.yml +1 -1
  4. data/.builds/ruby-3.4.yml +1 -1
  5. data/.builds/ruby-4.0.0.yml +1 -1
  6. data/AGENTS.md +10 -4
  7. data/CHANGELOG.md +79 -7
  8. data/README.md +37 -5
  9. data/REUSE.toml +2 -7
  10. data/doc/application_architecture.md +96 -22
  11. data/doc/application_testing.md +76 -30
  12. data/doc/contributors/architectural_overhaul/chat_conversations.md +4952 -0
  13. data/doc/contributors/architectural_overhaul/implementation_plan.md +60 -0
  14. data/doc/contributors/architectural_overhaul/task.md +37 -0
  15. data/doc/contributors/design/ruby_frontend.md +288 -56
  16. data/doc/contributors/design/rust_backend.md +349 -54
  17. data/doc/contributors/developing_examples.md +134 -49
  18. data/doc/contributors/index.md +7 -5
  19. data/doc/contributors/v1.0.0_blockers.md +1729 -0
  20. data/doc/event_handling.md +11 -3
  21. data/doc/images/app_all_events.png +0 -0
  22. data/doc/images/app_color_picker.png +0 -0
  23. data/doc/images/app_login_form.png +0 -0
  24. data/doc/images/app_stateful_interaction.png +0 -0
  25. data/doc/images/verify_quickstart_dsl.png +0 -0
  26. data/doc/images/verify_quickstart_layout.png +0 -0
  27. data/doc/images/verify_quickstart_lifecycle.png +0 -0
  28. data/doc/images/verify_readme_usage.png +0 -0
  29. data/doc/images/widget_barchart_demo.png +0 -0
  30. data/doc/images/widget_block_demo.png +0 -0
  31. data/doc/images/widget_canvas_demo.png +0 -0
  32. data/doc/images/widget_cell_demo.png +0 -0
  33. data/doc/images/widget_center_demo.png +0 -0
  34. data/doc/images/widget_chart_demo.png +0 -0
  35. data/doc/images/widget_list_demo.png +0 -0
  36. data/doc/images/widget_overlay_demo.png +0 -0
  37. data/doc/images/widget_render.png +0 -0
  38. data/doc/images/widget_rich_text.png +0 -0
  39. data/doc/images/widget_scroll_text.png +0 -0
  40. data/doc/images/widget_sparkline_demo.png +0 -0
  41. data/doc/images/widget_table_demo.png +0 -0
  42. data/doc/images/widget_tabs_demo.png +0 -0
  43. data/doc/images/widget_text_width.png +0 -0
  44. data/doc/index.md +11 -6
  45. data/doc/interactive_design.md +2 -2
  46. data/doc/quickstart.md +127 -165
  47. data/doc/terminal_limitations.md +92 -0
  48. data/doc/v0.7.0_migration.md +236 -0
  49. data/doc/why.md +93 -0
  50. data/examples/app_all_events/README.md +47 -27
  51. data/examples/app_all_events/app.rb +38 -35
  52. data/examples/app_all_events/model/app_model.rb +157 -0
  53. data/examples/app_all_events/model/event_entry.rb +17 -0
  54. data/examples/app_all_events/model/msg.rb +37 -0
  55. data/examples/app_all_events/update.rb +73 -0
  56. data/examples/app_all_events/view/app_view.rb +9 -9
  57. data/examples/app_all_events/view/controls_view.rb +9 -7
  58. data/examples/app_all_events/view/counts_view.rb +13 -9
  59. data/examples/app_all_events/view/live_view.rb +9 -8
  60. data/examples/app_all_events/view/log_view.rb +11 -16
  61. data/examples/app_color_picker/README.md +84 -42
  62. data/examples/app_color_picker/app.rb +24 -62
  63. data/examples/app_color_picker/controls.rb +90 -0
  64. data/examples/app_color_picker/copy_dialog.rb +45 -49
  65. data/examples/app_color_picker/export_pane.rb +126 -0
  66. data/examples/app_color_picker/input.rb +99 -67
  67. data/examples/app_color_picker/main_container.rb +178 -0
  68. data/examples/app_color_picker/palette.rb +55 -26
  69. data/examples/app_login_form/README.md +49 -0
  70. data/examples/app_login_form/app.rb +2 -3
  71. data/examples/app_stateful_interaction/README.md +33 -0
  72. data/examples/app_stateful_interaction/app.rb +272 -0
  73. data/examples/timeout_demo.rb +43 -0
  74. data/examples/verify_quickstart_dsl/README.md +49 -0
  75. data/examples/verify_quickstart_dsl/app.rb +2 -0
  76. data/examples/verify_quickstart_layout/README.md +71 -0
  77. data/examples/verify_quickstart_layout/app.rb +2 -0
  78. data/examples/verify_quickstart_lifecycle/README.md +56 -0
  79. data/examples/verify_quickstart_lifecycle/app.rb +10 -4
  80. data/examples/verify_readme_usage/README.md +43 -0
  81. data/examples/verify_readme_usage/app.rb +8 -2
  82. data/examples/widget_barchart_demo/README.md +50 -0
  83. data/examples/widget_barchart_demo/app.rb +5 -5
  84. data/examples/widget_block_demo/README.md +36 -0
  85. data/examples/widget_block_demo/app.rb +256 -0
  86. data/examples/widget_box_demo/README.md +45 -0
  87. data/examples/widget_calendar_demo/README.md +39 -0
  88. data/examples/widget_calendar_demo/app.rb +5 -1
  89. data/examples/widget_canvas_demo/README.md +27 -0
  90. data/examples/widget_canvas_demo/app.rb +123 -0
  91. data/examples/widget_cell_demo/README.md +36 -0
  92. data/examples/widget_cell_demo/app.rb +31 -24
  93. data/examples/widget_center_demo/README.md +29 -0
  94. data/examples/widget_center_demo/app.rb +116 -0
  95. data/examples/widget_chart_demo/README.md +41 -0
  96. data/examples/widget_chart_demo/app.rb +7 -2
  97. data/examples/widget_gauge_demo/README.md +41 -0
  98. data/examples/widget_layout_split/README.md +44 -0
  99. data/examples/widget_line_gauge_demo/README.md +41 -0
  100. data/examples/widget_list_demo/README.md +49 -0
  101. data/examples/widget_list_demo/app.rb +91 -107
  102. data/examples/widget_map_demo/README.md +39 -0
  103. data/examples/{app_map_demo → widget_map_demo}/app.rb +4 -4
  104. data/examples/widget_overlay_demo/README.md +36 -0
  105. data/examples/widget_overlay_demo/app.rb +248 -0
  106. data/examples/widget_popup_demo/README.md +36 -0
  107. data/examples/widget_ratatui_logo_demo/README.md +34 -0
  108. data/examples/widget_ratatui_logo_demo/app.rb +1 -1
  109. data/examples/widget_ratatui_mascot_demo/README.md +34 -0
  110. data/examples/widget_rect/README.md +38 -0
  111. data/examples/widget_render/README.md +37 -0
  112. data/examples/widget_render/app.rb +3 -3
  113. data/examples/widget_rich_text/README.md +35 -0
  114. data/examples/widget_rich_text/app.rb +62 -33
  115. data/examples/widget_scroll_text/README.md +37 -0
  116. data/examples/widget_scroll_text/app.rb +0 -1
  117. data/examples/widget_scrollbar_demo/README.md +37 -0
  118. data/examples/widget_sparkline_demo/README.md +42 -0
  119. data/examples/widget_sparkline_demo/app.rb +4 -3
  120. data/examples/widget_style_colors/README.md +34 -0
  121. data/examples/widget_table_demo/README.md +48 -0
  122. data/examples/{app_table_select → widget_table_demo}/app.rb +65 -12
  123. data/examples/widget_tabs_demo/README.md +41 -0
  124. data/examples/widget_tabs_demo/app.rb +15 -1
  125. data/examples/widget_text_width/README.md +35 -0
  126. data/examples/widget_text_width/app.rb +113 -0
  127. data/exe/.gitkeep +0 -0
  128. data/ext/ratatui_ruby/Cargo.lock +11 -4
  129. data/ext/ratatui_ruby/Cargo.toml +2 -1
  130. data/ext/ratatui_ruby/src/events.rs +238 -26
  131. data/ext/ratatui_ruby/src/frame.rs +116 -3
  132. data/ext/ratatui_ruby/src/lib.rs +37 -6
  133. data/ext/ratatui_ruby/src/rendering.rs +22 -21
  134. data/ext/ratatui_ruby/src/string_width.rs +101 -0
  135. data/ext/ratatui_ruby/src/terminal.rs +39 -15
  136. data/ext/ratatui_ruby/src/text.rs +13 -4
  137. data/ext/ratatui_ruby/src/widgets/barchart.rs +24 -6
  138. data/ext/ratatui_ruby/src/widgets/canvas.rs +5 -5
  139. data/ext/ratatui_ruby/src/widgets/gauge.rs +9 -2
  140. data/ext/ratatui_ruby/src/widgets/line_gauge.rs +9 -2
  141. data/ext/ratatui_ruby/src/widgets/list.rs +179 -3
  142. data/ext/ratatui_ruby/src/widgets/list_state.rs +137 -0
  143. data/ext/ratatui_ruby/src/widgets/mod.rs +3 -0
  144. data/ext/ratatui_ruby/src/widgets/scrollbar.rs +93 -1
  145. data/ext/ratatui_ruby/src/widgets/scrollbar_state.rs +169 -0
  146. data/ext/ratatui_ruby/src/widgets/table.rs +191 -34
  147. data/ext/ratatui_ruby/src/widgets/table_state.rs +121 -0
  148. data/lib/ratatui_ruby/buffer/cell.rb +168 -0
  149. data/lib/ratatui_ruby/buffer.rb +15 -0
  150. data/lib/ratatui_ruby/cell.rb +4 -4
  151. data/lib/ratatui_ruby/event/key/character.rb +35 -0
  152. data/lib/ratatui_ruby/event/key/media.rb +44 -0
  153. data/lib/ratatui_ruby/event/key/modifier.rb +95 -0
  154. data/lib/ratatui_ruby/event/key/navigation.rb +55 -0
  155. data/lib/ratatui_ruby/event/key/system.rb +45 -0
  156. data/lib/ratatui_ruby/event/key.rb +111 -51
  157. data/lib/ratatui_ruby/event/mouse.rb +3 -3
  158. data/lib/ratatui_ruby/event/paste.rb +1 -1
  159. data/lib/ratatui_ruby/frame.rb +100 -4
  160. data/lib/ratatui_ruby/layout/constraint.rb +95 -0
  161. data/lib/ratatui_ruby/layout/layout.rb +106 -0
  162. data/lib/ratatui_ruby/layout/rect.rb +118 -0
  163. data/lib/ratatui_ruby/layout.rb +19 -0
  164. data/lib/ratatui_ruby/list_state.rb +88 -0
  165. data/lib/ratatui_ruby/schema/bar_chart/bar.rb +2 -2
  166. data/lib/ratatui_ruby/schema/cursor.rb +5 -0
  167. data/lib/ratatui_ruby/schema/gauge.rb +3 -1
  168. data/lib/ratatui_ruby/schema/layout.rb +1 -1
  169. data/lib/ratatui_ruby/schema/line_gauge.rb +2 -2
  170. data/lib/ratatui_ruby/schema/list.rb +25 -4
  171. data/lib/ratatui_ruby/schema/list_item.rb +41 -0
  172. data/lib/ratatui_ruby/schema/rect.rb +43 -0
  173. data/lib/ratatui_ruby/schema/row.rb +66 -0
  174. data/lib/ratatui_ruby/schema/style.rb +24 -4
  175. data/lib/ratatui_ruby/schema/table.rb +29 -11
  176. data/lib/ratatui_ruby/schema/text.rb +96 -3
  177. data/lib/ratatui_ruby/scrollbar_state.rb +112 -0
  178. data/lib/ratatui_ruby/style/style.rb +81 -0
  179. data/lib/ratatui_ruby/style.rb +15 -0
  180. data/lib/ratatui_ruby/table_state.rb +90 -0
  181. data/lib/ratatui_ruby/test_helper/event_injection.rb +169 -0
  182. data/lib/ratatui_ruby/test_helper/snapshot.rb +414 -0
  183. data/lib/ratatui_ruby/test_helper/style_assertions.rb +351 -0
  184. data/lib/ratatui_ruby/test_helper/terminal.rb +127 -0
  185. data/lib/ratatui_ruby/test_helper/test_doubles.rb +68 -0
  186. data/lib/ratatui_ruby/test_helper.rb +65 -358
  187. data/lib/ratatui_ruby/tui/buffer_factories.rb +20 -0
  188. data/lib/ratatui_ruby/tui/canvas_factories.rb +44 -0
  189. data/lib/ratatui_ruby/tui/core.rb +38 -0
  190. data/lib/ratatui_ruby/tui/layout_factories.rb +74 -0
  191. data/lib/ratatui_ruby/tui/state_factories.rb +33 -0
  192. data/lib/ratatui_ruby/tui/style_factories.rb +20 -0
  193. data/lib/ratatui_ruby/tui/text_factories.rb +44 -0
  194. data/lib/ratatui_ruby/tui/widget_factories.rb +195 -0
  195. data/lib/ratatui_ruby/tui.rb +75 -0
  196. data/lib/ratatui_ruby/version.rb +1 -1
  197. data/lib/ratatui_ruby/widgets/bar_chart/bar.rb +47 -0
  198. data/lib/ratatui_ruby/widgets/bar_chart/bar_group.rb +25 -0
  199. data/lib/ratatui_ruby/widgets/bar_chart.rb +239 -0
  200. data/lib/ratatui_ruby/widgets/block.rb +192 -0
  201. data/lib/ratatui_ruby/widgets/calendar.rb +84 -0
  202. data/lib/ratatui_ruby/widgets/canvas.rb +231 -0
  203. data/lib/ratatui_ruby/widgets/cell.rb +47 -0
  204. data/lib/ratatui_ruby/widgets/center.rb +59 -0
  205. data/lib/ratatui_ruby/widgets/chart.rb +185 -0
  206. data/lib/ratatui_ruby/widgets/clear.rb +54 -0
  207. data/lib/ratatui_ruby/widgets/cursor.rb +42 -0
  208. data/lib/ratatui_ruby/widgets/gauge.rb +72 -0
  209. data/lib/ratatui_ruby/widgets/line_gauge.rb +80 -0
  210. data/lib/ratatui_ruby/widgets/list.rb +127 -0
  211. data/lib/ratatui_ruby/widgets/list_item.rb +43 -0
  212. data/lib/ratatui_ruby/widgets/overlay.rb +43 -0
  213. data/lib/ratatui_ruby/widgets/paragraph.rb +99 -0
  214. data/lib/ratatui_ruby/widgets/ratatui_logo.rb +31 -0
  215. data/lib/ratatui_ruby/widgets/ratatui_mascot.rb +36 -0
  216. data/lib/ratatui_ruby/widgets/row.rb +68 -0
  217. data/lib/ratatui_ruby/widgets/scrollbar.rb +143 -0
  218. data/lib/ratatui_ruby/widgets/shape/label.rb +68 -0
  219. data/lib/ratatui_ruby/widgets/sparkline.rb +134 -0
  220. data/lib/ratatui_ruby/widgets/table.rb +141 -0
  221. data/lib/ratatui_ruby/widgets/tabs.rb +85 -0
  222. data/lib/ratatui_ruby/widgets.rb +40 -0
  223. data/lib/ratatui_ruby.rb +64 -57
  224. data/sig/examples/app_all_events/view.rbs +1 -1
  225. data/sig/examples/app_all_events/view_state.rbs +1 -1
  226. data/sig/examples/app_stateful_interaction/app.rbs +33 -0
  227. data/sig/examples/widget_block_demo/app.rbs +32 -0
  228. data/sig/examples/{app_map_demo → widget_map_demo}/app.rbs +2 -2
  229. data/sig/examples/{app_table_select → widget_table_demo}/app.rbs +2 -2
  230. data/sig/examples/{widget_table_flex → widget_text_width}/app.rbs +2 -3
  231. data/sig/ratatui_ruby/event.rbs +11 -1
  232. data/sig/ratatui_ruby/frame.rbs +2 -0
  233. data/sig/ratatui_ruby/list_state.rbs +13 -0
  234. data/sig/ratatui_ruby/ratatui_ruby.rbs +2 -2
  235. data/sig/ratatui_ruby/schema/bar_chart/bar.rbs +3 -3
  236. data/sig/ratatui_ruby/schema/gauge.rbs +2 -2
  237. data/sig/ratatui_ruby/schema/line_gauge.rbs +2 -2
  238. data/sig/ratatui_ruby/schema/list.rbs +4 -2
  239. data/sig/ratatui_ruby/schema/list_item.rbs +10 -0
  240. data/sig/ratatui_ruby/schema/rect.rbs +3 -0
  241. data/sig/ratatui_ruby/schema/row.rbs +22 -0
  242. data/sig/ratatui_ruby/schema/style.rbs +3 -3
  243. data/sig/ratatui_ruby/schema/table.rbs +3 -1
  244. data/sig/ratatui_ruby/schema/text.rbs +9 -6
  245. data/sig/ratatui_ruby/scrollbar_state.rbs +18 -0
  246. data/sig/ratatui_ruby/session.rbs +41 -48
  247. data/sig/ratatui_ruby/table_state.rbs +15 -0
  248. data/sig/ratatui_ruby/test_helper/event_injection.rbs +16 -0
  249. data/sig/ratatui_ruby/test_helper/snapshot.rbs +12 -0
  250. data/sig/ratatui_ruby/test_helper/style_assertions.rbs +64 -0
  251. data/sig/ratatui_ruby/test_helper/terminal.rbs +14 -0
  252. data/sig/ratatui_ruby/test_helper/test_doubles.rbs +22 -0
  253. data/sig/ratatui_ruby/test_helper.rbs +5 -4
  254. data/sig/ratatui_ruby/tui/buffer_factories.rbs +10 -0
  255. data/sig/ratatui_ruby/tui/canvas_factories.rbs +14 -0
  256. data/sig/ratatui_ruby/tui/core.rbs +14 -0
  257. data/sig/ratatui_ruby/tui/layout_factories.rbs +19 -0
  258. data/sig/ratatui_ruby/tui/state_factories.rbs +12 -0
  259. data/sig/ratatui_ruby/tui/style_factories.rbs +10 -0
  260. data/sig/ratatui_ruby/tui/text_factories.rbs +14 -0
  261. data/sig/ratatui_ruby/tui/widget_factories.rbs +39 -0
  262. data/sig/ratatui_ruby/tui.rbs +19 -0
  263. data/tasks/autodoc/examples.rb +79 -0
  264. data/tasks/autodoc.rake +7 -35
  265. data/tasks/bump/changelog.rb +3 -3
  266. data/tasks/bump/links.rb +67 -0
  267. data/tasks/sourcehut.rake +64 -21
  268. data/tasks/terminal_preview/app_screenshot.rb +13 -3
  269. data/tasks/terminal_preview/saved_screenshot.rb +4 -3
  270. metadata +169 -48
  271. data/doc/contributors/dwim_dx.md +0 -366
  272. data/doc/images/app_analytics.png +0 -0
  273. data/doc/images/app_custom_widget.png +0 -0
  274. data/doc/images/app_mouse_events.png +0 -0
  275. data/doc/images/app_table_select.png +0 -0
  276. data/doc/images/widget_block_padding.png +0 -0
  277. data/doc/images/widget_block_titles.png +0 -0
  278. data/doc/images/widget_list_styles.png +0 -0
  279. data/doc/images/widget_table_flex.png +0 -0
  280. data/examples/app_all_events/model/events.rb +0 -180
  281. data/examples/app_all_events/model/highlight.rb +0 -57
  282. data/examples/app_all_events/test/snapshots/after_focus_lost.txt +0 -24
  283. data/examples/app_all_events/test/snapshots/after_focus_regained.txt +0 -24
  284. data/examples/app_all_events/test/snapshots/after_horizontal_resize.txt +0 -24
  285. data/examples/app_all_events/test/snapshots/after_key_a.txt +0 -24
  286. data/examples/app_all_events/test/snapshots/after_key_ctrl_x.txt +0 -24
  287. data/examples/app_all_events/test/snapshots/after_mouse_click.txt +0 -24
  288. data/examples/app_all_events/test/snapshots/after_mouse_drag.txt +0 -24
  289. data/examples/app_all_events/test/snapshots/after_multiple_events.txt +0 -24
  290. data/examples/app_all_events/test/snapshots/after_paste.txt +0 -24
  291. data/examples/app_all_events/test/snapshots/after_resize.txt +0 -24
  292. data/examples/app_all_events/test/snapshots/after_right_click.txt +0 -24
  293. data/examples/app_all_events/test/snapshots/after_vertical_resize.txt +0 -24
  294. data/examples/app_all_events/test/snapshots/initial_state.txt +0 -24
  295. data/examples/app_all_events/view_state.rb +0 -42
  296. data/examples/app_color_picker/scene.rb +0 -201
  297. data/examples/widget_block_padding/app.rb +0 -67
  298. data/examples/widget_block_titles/app.rb +0 -69
  299. data/examples/widget_list_styles/app.rb +0 -141
  300. data/examples/widget_table_flex/app.rb +0 -95
  301. data/lib/ratatui_ruby/session/autodoc.rb +0 -417
  302. data/lib/ratatui_ruby/session.rb +0 -163
  303. data/sig/examples/widget_block_padding/app.rbs +0 -11
  304. data/sig/examples/widget_block_titles/app.rbs +0 -11
  305. data/sig/examples/widget_list_styles/app.rbs +0 -11
  306. data/tasks/autodoc/inventory.rb +0 -61
  307. data/tasks/autodoc/notice.rb +0 -26
  308. data/tasks/autodoc/rbs.rb +0 -38
  309. data/tasks/autodoc/rdoc.rb +0 -45
  310. data/tasks/bump/comparison_links.rb +0 -41
  311. /data/doc/images/{app_map_demo.png → widget_map_demo.png} +0 -0
@@ -5,57 +5,352 @@
5
5
 
6
6
  # Rust Backend Design (`ratatui_ruby` extension)
7
7
 
8
- This document describes the internal architecture of the `ratatui_ruby` Rust extension.
9
-
10
- ## Architecture Guidelines
11
-
12
- The project follows a **Structured Design** approach, separating concerns into modules to improve cohesiveness and testability.
13
-
14
- ### Core Principles
15
-
16
- 1. **Single Generic Renderer**: The backend implements a single generic renderer that accepts a Ruby `Value` representing the root of the view tree.
17
- 2. **No Custom Rust Structs for UI**: Do not define custom Rust structs that mirror Ruby UI components. Instead, extract data directly from Ruby objects using `funcall`.
18
- 3. **Dynamic Dispatch**: Use `value.class().name()` (e.g., `"RatatuiRuby::Paragraph"`) to dynamically dispatch rendering logic to the appropriate widget module.
19
- 4. **Immediate Mode**: The renderer traverses the Ruby object tree every frame and rebuilds the Ratatui widget tree on the fly.
20
-
21
- ### Module Structure
22
-
23
- The Rust extension is located in `ext/ratatui_ruby/src/` and is organized as follows:
24
-
25
- * **`lib.rs`**: The entry point for the compiled extension. It defines the Ruby module structure using `magnus` and exports public functions (`init_terminal`, `draw`, `poll_event`). It wires together the submodules.
26
- * **`terminal.rs`**: Encapsulates the global `TERMINAL` state (mutex-wrapped `CrosstermBackend`). It provides functions to initialize and restore the terminal to raw mode.
27
- * **`events.rs`**: Handles keyboard input polling and mapping Crossterm events to Ruby hashes.
28
- * **`style.rs`**: Provides pure functions for parsing styling information (Colors, Styles, Blocks) from Ruby values.
29
- * **`rendering.rs`**: The central dispatcher for the render loop. It takes the top-level Ruby View Tree node and recursively delegates to specific widget implementations based on the Ruby class name.
30
- * **`widgets/`**: A directory containing individual modules for each Ratatui widget (e.g., `paragraph.rs`, `list.rs`).
31
-
32
- ### Adding a New Widget
33
-
34
- To add a new widget:
35
-
36
- 1. Create a new file `src/widgets/my_widget.rs`.
37
- 2. Implement a public `render` function:
38
- ```rust
39
- /// Renders the widget to the given area.
40
- ///
41
- /// # Arguments
42
- ///
43
- /// * `frame` - The Ratatui frame to render to.
44
- /// * `area` - The rectangular area within the frame to draw the widget.
45
- /// * `node` - The Ruby object (Value) containing the widget's properties.
46
- pub fn render(frame: &mut Frame, area: Rect, node: Value) -> Result<(), Error>
47
- ```
48
- 3. Inside `render`:
49
- * Extract properties from the `node` (Ruby value) using `.funcall("method_name", ())?`.
50
- * Construct the Ratatui widget.
51
- * Render it using `frame.render_widget`.
52
- 4. Register the module in `src/widgets/mod.rs`.
53
- 5. Add a dispatch arm in `src/rendering.rs` matching the Ruby class name (e.g., `RatatuiRuby::MyWidget`).
54
-
55
- ### Testing Strategy
56
-
57
- * **Unit Tests (`cargo test`)**:
58
- * **Logic**: Test pure logic like `parse_color` in `style.rs` without needing a terminal or Ruby VM if possible (though `magnus::Value` usually requires it).
59
- * **Rendering**: Verify that widgets render *something* to a buffer. Ratatui's `TestBackend` or `Buffer` can be used to assert that cells are filled.
60
- * **Integration Tests (`rake test`)**:
61
- * Run Ruby scripts that exercise the full stack. Verify no crashes and expected return values.
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
+ ```rust
45
+ // rendering.rs
46
+ pub fn render_widget(frame: &mut Frame, area: Rect, node: Value) -> Result<(), Error> {
47
+ let class_name: String = node.class().name()?.into_owned();
48
+
49
+ match class_name.as_str() {
50
+ "RatatuiRuby::Widgets::Paragraph" => paragraph::render(frame, area, node),
51
+ "RatatuiRuby::Widgets::Block" => block::render(frame, area, node),
52
+ "RatatuiRuby::Widgets::Table" => table::render(frame, area, node),
53
+ // ... etc
54
+ _ => Err(Error::new(
55
+ magnus::exception::type_error(),
56
+ format!("Unknown widget type: {}", class_name)
57
+ ))
58
+ }
59
+ }
60
+ ```
61
+
62
+ ### 3. No Custom Rust Structs for UI
63
+
64
+ Do not define Rust structs that mirror Ruby UI components. This would create synchronization problems when Ruby classes change.
65
+
66
+ **What We Do:**
67
+ ```rust
68
+ // Extract directly from Ruby object
69
+ let text: String = node.funcall("text", ())?;
70
+ let style_val: Value = node.funcall("style", ())?;
71
+ let style = parse_style(style_val)?;
72
+ ```
73
+
74
+ **What We Don't Do:**
75
+ ```rust
76
+ // NO: Rust struct mirroring Ruby
77
+ struct Paragraph {
78
+ text: String,
79
+ style: Option<Style>,
80
+ block: Option<Block>,
81
+ }
82
+ ```
83
+
84
+ ### 4. Immediate Mode Rendering
85
+
86
+ 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.
87
+
88
+ This mirrors Ratatui's own immediate mode paradigm. The Rust backend is stateless (except for terminal state).
89
+
90
+ ### 5. Memory Safety via Value Extraction
91
+
92
+ Ruby's GC can move or collect objects at any time. All data extracted from Ruby must be owned (copied) before use, never borrowed.
93
+
94
+ ```rust
95
+ // SAFE: Convert to owned String immediately
96
+ let text: String = node.funcall::<_, String>("text", ())?.into_owned();
97
+
98
+ // UNSAFE: Holding reference across GC-safe point
99
+ let text_ref: &str = node.funcall("text", ())?; // DON'T
100
+ do_something_that_might_gc();
101
+ use(text_ref); // CRASH: text_ref may be invalid
102
+ ```
103
+
104
+ ---
105
+
106
+ ## Directory Structure
107
+
108
+ ```
109
+ ext/ratatui_ruby/src/
110
+ ├── lib.rs # Entry point, Ruby module registration
111
+ ├── terminal.rs # Global TERMINAL state, init/restore
112
+ ├── frame.rs # Frame wrapper for render_widget, area access
113
+ ├── events.rs # Event polling, crossterm -> Ruby conversion
114
+ ├── style.rs # Style/Color parsing from Ruby values
115
+ ├── text.rs # Span/Line parsing
116
+ ├── rendering.rs # Central dispatcher, class name -> widget module
117
+ └── widgets/ # Per-widget rendering modules
118
+ ├── mod.rs # Re-exports all widget modules
119
+ ├── paragraph.rs
120
+ ├── block.rs
121
+ ├── table.rs
122
+ ├── list.rs
123
+ ├── canvas.rs
124
+ └── ...
125
+ ```
126
+
127
+ ---
128
+
129
+ ## Module Responsibilities
130
+
131
+ ### `lib.rs` — Entry Point
132
+
133
+ Defines the Ruby module hierarchy using `magnus` and exports public functions (`init_terminal`, `restore_terminal`, `draw`, `poll_event`, `get_cell_at`).
134
+
135
+ ### `terminal.rs` — Terminal State
136
+
137
+ Manages the global `TERMINAL` singleton (mutex-wrapped `CrosstermBackend<Stdout>`).
138
+
139
+ Key functions:
140
+ - `init()` — Enter raw mode, enable mouse capture, switch to alternate screen
141
+ - `restore()` — Disable raw mode, restore main screen
142
+ - `get_cell_at(x, y)` — Return buffer cell as Ruby `Buffer::Cell` object
143
+
144
+ **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.
145
+
146
+ ### `frame.rs` — Frame Wrapper
147
+
148
+ 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.
149
+
150
+ ### `events.rs` — Event Conversion
151
+
152
+ Polls crossterm events and converts them to Ruby `Event::*` objects. Handles key, mouse, resize, paste, and focus events.
153
+
154
+ ### `style.rs` — Style Parsing
155
+
156
+ Pure functions for extracting style information from Ruby values. Handles `parse_style`, `parse_color` (symbols, integers 0-255, hex strings), and `parse_modifiers`.
157
+
158
+ ### `rendering.rs` — Central Dispatcher
159
+
160
+ The routing layer that maps Ruby class names to widget renderers:
161
+
162
+ ```rust
163
+ pub fn render_widget(frame: &mut Frame, area: Rect, node: Value) -> Result<(), Error> {
164
+ let class_name: String = node.class().name()?.into_owned();
165
+
166
+ match class_name.as_str() {
167
+ // Widgets module
168
+ "RatatuiRuby::Widgets::Paragraph" => widgets::paragraph::render(frame, area, node),
169
+ "RatatuiRuby::Widgets::Block" => widgets::block::render(frame, area, node),
170
+ "RatatuiRuby::Widgets::Table" => widgets::table::render(frame, area, node),
171
+ "RatatuiRuby::Widgets::List" => widgets::list::render(frame, area, node),
172
+ "RatatuiRuby::Widgets::Tabs" => widgets::tabs::render(frame, area, node),
173
+ "RatatuiRuby::Widgets::Gauge" => widgets::gauge::render(frame, area, node),
174
+ "RatatuiRuby::Widgets::Chart" => widgets::chart::render(frame, area, node),
175
+ "RatatuiRuby::Widgets::Canvas" => widgets::canvas::render(frame, area, node),
176
+ "RatatuiRuby::Widgets::Scrollbar" => widgets::scrollbar::render(frame, area, node),
177
+ "RatatuiRuby::Widgets::Calendar" => widgets::calendar::render(frame, area, node),
178
+ // ... all widgets
179
+
180
+ // Special widgets
181
+ "RatatuiRuby::Widgets::Clear" => widgets::clear::render(frame, area, node),
182
+ "RatatuiRuby::Widgets::Cursor" => widgets::cursor::render(frame, area, node),
183
+
184
+ // Custom widgets (Ruby escape hatch)
185
+ _ if has_render_method(node) => widgets::custom::render(frame, area, node),
186
+
187
+ _ => Err(Error::new(
188
+ magnus::exception::type_error(),
189
+ format!("Unknown widget type: {}", class_name)
190
+ ))
191
+ }
192
+ }
193
+ ```
194
+
195
+ **Namespace Pattern:** All built-in widgets use the `RatatuiRuby::Widgets::*` namespace. The dispatcher matches on full class names, not prefixes.
196
+
197
+ ### `widgets/*.rs` — Widget Renderers
198
+
199
+ Each widget has its own module with a standard interface:
200
+
201
+ ```rust
202
+ // widgets/paragraph.rs
203
+ pub fn render(frame: &mut Frame, area: Rect, node: Value) -> Result<(), Error> {
204
+ // 1. Extract properties from Ruby object
205
+ let text = parse_text(node.funcall("text", ())?)?;
206
+ let style = parse_style(node.funcall("style", ())?)?;
207
+ let alignment = parse_alignment(node.funcall("alignment", ())?)?;
208
+ let block_val: Value = node.funcall("block", ())?;
209
+
210
+ // 2. Build Ratatui widget
211
+ let mut paragraph = Paragraph::new(text)
212
+ .style(style)
213
+ .alignment(alignment);
214
+
215
+ // 3. Handle optional block wrapper
216
+ if !block_val.is_nil() {
217
+ paragraph = paragraph.block(parse_block(block_val)?);
218
+ }
219
+
220
+ // 4. Render
221
+ frame.render_widget(paragraph, area);
222
+ Ok(())
223
+ }
224
+ ```
225
+
226
+ ---
227
+
228
+ ## Adding a New Widget
229
+
230
+ ### Step 1: Create the Widget Module
231
+
232
+ ```rust
233
+ // src/widgets/my_widget.rs
234
+
235
+ use magnus::{Error, Value};
236
+ use ratatui::prelude::*;
237
+
238
+ use crate::style::parse_style;
239
+
240
+ pub fn render(frame: &mut Frame, area: Rect, node: Value) -> Result<(), Error> {
241
+ // Extract properties
242
+ let content: String = node.funcall::<_, String>("content", ())?.into_owned();
243
+ let style = parse_style(node.funcall("style", ())?)?;
244
+
245
+ // Build and render
246
+ let widget = MyWidget::new(content).style(style);
247
+ frame.render_widget(widget, area);
248
+
249
+ Ok(())
250
+ }
251
+ ```
252
+
253
+ ### Step 2: Register in `widgets/mod.rs`
254
+
255
+ ```rust
256
+ pub mod my_widget;
257
+ ```
258
+
259
+ ### Step 3: Add Dispatch Arm in `rendering.rs`
260
+
261
+ ```rust
262
+ "RatatuiRuby::Widgets::MyWidget" => widgets::my_widget::render(frame, area, node),
263
+ ```
264
+
265
+ ### Step 4: Test
266
+
267
+ Run `cargo test` for Rust unit tests, then `rake test` for Ruby integration tests.
268
+
269
+ ---
270
+
271
+ ## Stateful Widget Rendering
272
+
273
+ Some widgets (List, Table, Scrollbar) support stateful rendering where a mutable State object tracks scroll position and selection.
274
+
275
+ ### The Pattern
276
+
277
+ ```rust
278
+ pub fn render_stateful_widget(
279
+ frame: &mut Frame,
280
+ area: Rect,
281
+ widget_node: Value,
282
+ state_node: Value
283
+ ) -> Result<(), Error> {
284
+ // 1. Build the widget (immutable configuration)
285
+ let list = build_list(widget_node)?;
286
+
287
+ // 2. Extract mutable state
288
+ let mut state = ListState::default();
289
+ if let Ok(selected) = state_node.funcall::<_, Option<i64>>("selected", ()) {
290
+ state.select(selected.map(|i| i as usize));
291
+ }
292
+
293
+ // 3. Render with state (Ratatui may mutate offset)
294
+ frame.render_stateful_widget(list, area, &mut state);
295
+
296
+ // 4. Write computed values back to Ruby state object
297
+ state_node.funcall::<_, Value>("set_offset", (state.offset() as i64,))?;
298
+
299
+ Ok(())
300
+ }
301
+ ```
302
+
303
+ **State Precedence:** When using stateful rendering, the State object's values take precedence over Widget properties. This is documented in Ruby.
304
+
305
+ ---
306
+
307
+ ## Custom Widget Escape Hatch
308
+
309
+ 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.
310
+
311
+ ---
312
+
313
+ ## Error Handling
314
+
315
+ All Rust functions that can fail return `Result<T, magnus::Error>`. Magnus automatically converts these to Ruby exceptions.
316
+
317
+ **Error Types:**
318
+
319
+ | Scenario | Ruby Exception | Notes |
320
+ |----------|---------------|-------|
321
+ | Invalid argument | `ArgumentError` | Wrong type, out of range |
322
+ | Unknown widget | `TypeError` | Class name not in dispatch table |
323
+ | Terminal not initialized | `RatatuiRuby::Error::Terminal` | Custom exception class |
324
+ | Frame used after draw block | `RatatuiRuby::Error::Safety` | Memory safety violation |
325
+
326
+ ---
327
+
328
+ ## Testing Strategy
329
+
330
+ ### Rust Unit Tests (`cargo test`)
331
+
332
+ 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.
333
+
334
+ ### Ruby Integration Tests (`rake test`)
335
+
336
+ The primary testing strategy. Ruby tests exercise the full stack and verify end-to-end behavior without testing Rust internals.
337
+
338
+ ### Buffer Verification
339
+
340
+ For Rust-level rendering tests, use Ratatui's `TestBackend` or `Buffer` to assert cells are filled correctly.
341
+
342
+ ---
343
+
344
+ ## Performance Considerations
345
+
346
+ ### Avoid Repeated `funcall`
347
+
348
+ Each `funcall` crosses the Ruby/Rust boundary. Cache results when accessing the same property multiple times rather than calling `funcall` repeatedly.
349
+
350
+ ### String Ownership
351
+
352
+ Always convert to owned `String` immediately via `into_owned()` to avoid GC-related memory safety issues.
353
+
354
+ ### Batch Collection Iteration
355
+
356
+ When processing Ruby arrays, collect all values into a `Vec<Value>` before processing to avoid holding references across iterations.
@@ -84,9 +84,11 @@ All interactive examples must fit within an **80×24 terminal** (standard VT100
84
84
  - **Style hotkeys visually:** Use `modifiers: [:bold, :underlined]` on hotkey letters to make them stand out from descriptions. Example: `i` (bold, underlined) followed by `Items`.
85
85
  - Test early by running the example at 80×24 and verifying all content is visible without wrapping, scrolling, or clipping.
86
86
 
87
- Every example must also have an RBS file documenting its public methods:
87
+ ## Type Signatures
88
88
 
89
- `examples/my_example/app.rbs`:
89
+ Every example must also have an RBS file documenting its public methods. Type signatures live in a centralized location:
90
+
91
+ `sig/examples/my_example/app.rbs`:
90
92
  ```rbs
91
93
  class MyExampleApp
92
94
  # @public
@@ -97,20 +99,41 @@ class MyExampleApp
97
99
  end
98
100
  ```
99
101
 
102
+ ## Directory Structure
103
+
104
+ Examples are organized across three locations:
105
+
106
+ ```
107
+ examples/
108
+ my_example/
109
+ app.rb ← REQUIRED: The runnable example code
110
+ README.md ← REQUIRED: Purpose, architecture, hotkeys, usage
111
+
112
+ test/examples/
113
+ my_example/
114
+ test_app.rb ← REQUIRED: Tests (centralized, not local to example)
115
+ snapshots/ ← Auto-created by assert_snapshot
116
+ initial_render.txt
117
+
118
+ sig/examples/
119
+ my_example/
120
+ app.rbs ← REQUIRED: Type signatures (centralized, not local to example)
121
+ ```
122
+
100
123
  ### Key Requirements
101
124
 
102
125
  1. **Only `run` should be public.** All other methods (`render`, `handle_input`, helper methods) must be private. This prevents tests from calling internal methods directly.
103
126
 
104
127
  2. **Use `RatatuiRuby.run` for terminal management.** Never call `init_terminal` or `restore_terminal` directly. The `run` block handles terminal setup/teardown automatically and safely, even if an exception occurs.
105
128
 
106
- 3. **Use the Session API (`tui`) for cleaner code.** Accept the `tui` block parameter from `RatatuiRuby.run` and use it throughout your app:
129
+ 3. **Use the TUI API (`tui`) for cleaner code.** Accept the `tui` block parameter from `RatatuiRuby.run` and use it throughout your app:
107
130
  - `@tui.draw { |frame| ... }` instead of `RatatuiRuby.draw`
108
131
  - `@tui.poll_event` instead of `RatatuiRuby.poll_event`
109
- - `@tui.style(...)` instead of `RatatuiRuby::Style.new(...)`
110
- - `@tui.paragraph(...)` instead of `RatatuiRuby::Paragraph.new(...)`
111
- - `@tui.block(...)` instead of `RatatuiRuby::Block.new(...)`
132
+ - `@tui.style(...)` instead of `RatatuiRuby::Style::Style.new(...)`
133
+ - `@tui.paragraph(...)` instead of `RatatuiRuby::Widgets::Paragraph.new(...)`
134
+ - `@tui.block(...)` instead of `RatatuiRuby::Widgets::Block.new(...)`
112
135
  - `@tui.layout_split(...)` instead of `RatatuiRuby::Layout.split(...)`
113
- - `@tui.constraint_fill(...)` instead of `RatatuiRuby::Constraint.fill(...)`
136
+ - `@tui.constraint_fill(...)` instead of `RatatuiRuby::Layout::Constraint.fill(...)`
114
137
  - `@tui.text_line(...)` instead of `RatatuiRuby::Text::Line.new(...)`
115
138
  - `@tui.text_span(...)` instead of `RatatuiRuby::Text::Span.new(...)`
116
139
 
@@ -127,7 +150,6 @@ end
127
150
  # Ignore other events
128
151
  end
129
152
  end
130
- ```
131
153
 
132
154
  5. **Use keyboard keys to cycle through widget attributes.** Users should be able to interactively explore all widget options. Common patterns:
133
155
  - Arrow keys: Navigate or adjust values
@@ -135,7 +157,17 @@ end
135
157
  - Space: Toggle or select
136
158
  - `q` or Ctrl+C: Quit
137
159
 
138
- 5. **Naming Conventions for Controls**
160
+ 6. **All examples must include a README.md** explaining:
161
+ - What problem the example solves
162
+ - Architecture (if applicable)
163
+ - Hotkeys (if interactive): Document all keyboard/mouse controls
164
+ - Key concepts demonstrated
165
+ - Usage instructions
166
+ - Learning outcomes
167
+
168
+ See examples/app_color_picker/README.md and examples/app_all_events/README.md for patterns. Adhere to docs/contributors/documentation_style.md.
169
+
170
+ 7. **Naming Conventions for Controls**
139
171
 
140
172
  When documenting hotkeys and cycling options in the UI, use consistent naming:
141
173
 
@@ -158,7 +190,7 @@ When documenting hotkeys and cycling options in the UI, use consistent naming:
158
190
 
159
191
  This keeps the UI self-documenting and users can see exact parameter names when they read the hotkey help.
160
192
 
161
- 7. **Hit Testing**
193
+ 8. **Hit Testing**
162
194
 
163
195
  Examples with mouse interaction should use the **Frame API**. By calling `@tui.layout_split` inside `@tui.draw`, you obtain the exact `Rect`s used for rendering. Store these rects in instance variables (e.g., `@sidebar_rect`) to use them in your `handle_input` method for hit testing:
164
196
 
@@ -170,11 +202,9 @@ end
170
202
 
171
203
  ## Testing Examples
172
204
 
173
- Example tests live alongside examples as `test_app.rb` files in the same directory.
174
-
175
- ### Testing Pattern
205
+ Example tests live in a centralized test tree:
176
206
 
177
- `examples/my_example/test_app.rb`:
207
+ `test/examples/my_example/test_app.rb`:
178
208
  ```ruby
179
209
  $LOAD_PATH.unshift File.expand_path("../../lib", __dir__)
180
210
  require "ratatui_ruby"
@@ -191,56 +221,75 @@ class TestMyExampleApp < Minitest::Test
191
221
 
192
222
  def test_initial_render
193
223
  with_test_terminal do
194
- inject_key(:q) # Queue quit event
195
- @app.run # Run the app loop
196
-
197
- content = buffer_content.join("\n")
198
- assert_includes content, "Expected Text"
224
+ inject_key(:q)
225
+ @app.run
226
+ assert_snapshot("initial_render")
199
227
  end
200
228
  end
229
+ end
230
+ ```
201
231
 
202
- def test_keyboard_interaction
203
- with_test_terminal do
204
- inject_key("s") # Press 's' to cycle something
205
- inject_key(:q) # Then quit
206
- @app.run
232
+ ## Snapshot Testing Pattern (REQUIRED)
207
233
 
208
- content = buffer_content.join("\n")
209
- assert_includes content, "Changed State"
210
- end
211
- end
234
+ All example tests MUST use snapshot testing via the `assert_snapshot` API, not manual content assertions.
212
235
 
213
- def test_mouse_interaction
214
- with_test_terminal do
215
- # Click at (10, 5)
216
- inject_click(x: 10, y: 5)
217
- inject_key(:q)
218
- @app.run
236
+ ### Why Snapshots
219
237
 
220
- content = buffer_content.join("\n")
221
- assert_includes content, "Clicked at (10, 5)"
222
- end
238
+ - **Exact verification:** Captures complete screen state, character-by-character
239
+ - **Auto-update:** `UPDATE_SNAPSHOTS=1 bin/agent_rake test` regenerates all snapshots
240
+ - **Auto-managed:** Snapshots live in `test/examples/{name}/snapshots/{test_name}.txt`
241
+ - **Maintainable:** No tedious manual string checks
242
+ - **Self-documenting:** Snapshots show exactly what output is expected
243
+
244
+ ### Basic Pattern
245
+
246
+ ```ruby
247
+ def test_initial_render
248
+ with_test_terminal do
249
+ inject_key(:q)
250
+ @app.run
251
+
252
+ assert_snapshot("initial_render")
223
253
  end
224
254
  end
225
255
  ```
226
256
 
227
- ### Testing Guidelines
257
+ Snapshot auto-saved to: `test/examples/widget_foo_demo/snapshots/initial_render.txt`
228
258
 
229
- 1. **Inject events, observe buffer.** Tests should only interact through:
230
- - `inject_key`, `inject_click`, `inject_event`, etc. for input
231
- - `buffer_content` for output verification
259
+ ### With Normalization (for dynamic content)
232
260
 
233
- 2. **Never call internal methods.** Don't call `render`, `handle_input`, `__send__`, or access instance variables with `instance_variable_get`. Tests verify behavior through the public `run` method.
261
+ For examples with timestamps, random data, or other non-deterministic output:
234
262
 
235
- 3. **Use `inject_key(:q)` to exit.** All examples should support quitting with `q`, so inject this as the final event to terminate the loop.
263
+ ```ruby
264
+ private def assert_normalized_snapshot(snapshot_name)
265
+ assert_snapshot(snapshot_name) do |actual|
266
+ actual.map do |line|
267
+ line.gsub(/\d{2}:\d{2}:\d{2}/, "XX:XX:XX") # Mask timestamps
268
+ .gsub(/Random ID: \d+/, "Random ID: XXX") # Mask random values
269
+ end
270
+ end
271
+ end
236
272
 
237
- 4. **Assert and refute.** When testing which item was clicked/selected, also verify the opposite didn't happen:
238
- ```ruby
239
- assert_includes content, "Left Panel clicked"
240
- refute_includes content, "Right Panel clicked"
241
- ```
273
+ def test_after_event
274
+ with_test_terminal do
275
+ inject_key("a")
276
+ inject_key(:q)
277
+ @app.run
278
+
279
+ assert_normalized_snapshot("after_event")
280
+ end
281
+ end
282
+ ```
283
+
284
+ See `test/examples/app_all_events/test_app.rb` for a complete example.
242
285
 
243
- 5. **Test state cycling.** If an example cycles through options (styles, modes, etc.), test that pressing the key actually changes the rendered output.
286
+ ### Regenerating Snapshots
287
+
288
+ When UI changes are intentional, regenerate all snapshots:
289
+
290
+ ```bash
291
+ UPDATE_SNAPSHOTS=1 bin/agent_rake test
292
+ ```
244
293
 
245
294
  ## Widget Attribute Cycling
246
295
 
@@ -259,3 +308,39 @@ Examples should demonstrate widget configurability by allowing interactive cycli
259
308
  | Scrollbar | theme | s |
260
309
 
261
310
  Display the current state in the UI (e.g., in a title or status bar or paragraph) so users can see what changed. Display the hotkey in the UI as well, so users can see how to change it; the hotkey should not disappear as app state changes.
311
+
312
+ ## Data Quality
313
+
314
+ Examples must use **realistic, meaningful data**—not dummy placeholder text. This ensures examples:
315
+ - Demonstrate the widget's real-world usage
316
+ - Provide educational value to users reading the code
317
+ - Look professional and polished
318
+
319
+ ### Guidelines
320
+
321
+ **For small datasets (< 10 items):**
322
+ Use hardcoded realistic data. Examples:
323
+ - Geographic coordinates with city names (see `widget_map_demo/app.rb`)
324
+ - Real product names or person names
325
+ - Meaningful status values ("Completed", "Pending", "Failed")
326
+
327
+ **For large datasets (≥ 10 items):**
328
+ Use the [Faker](https://github.com/faker-ruby/faker) gem with **deterministic seeding** so data is consistent across runs:
329
+
330
+ ```ruby
331
+ require "faker"
332
+
333
+ # Seed Faker for reproducible output
334
+ Faker::Config.random = Random.new(12345)
335
+
336
+ # Generate realistic data
337
+ users = Array.new(50) { Faker::Name.name }
338
+ emails = Array.new(50) { Faker::Internet.email }
339
+ ```
340
+
341
+ In tests, set the same seed before each test to ensure snapshot consistency.
342
+
343
+ **Avoid:**
344
+ - Repeated placeholder text like "Lorem ipsum" or "Background Layer" × 20
345
+ - Generic numbered items like "Item 1", "Item 2" (unless the context demands it)
346
+ - Nonsensical or visually jarring content