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.
- checksums.yaml +4 -4
- data/.builds/ruby-3.2.yml +1 -1
- data/.builds/ruby-3.3.yml +1 -1
- data/.builds/ruby-3.4.yml +1 -1
- data/.builds/ruby-4.0.0.yml +1 -1
- data/AGENTS.md +10 -4
- data/CHANGELOG.md +79 -7
- data/README.md +37 -5
- data/REUSE.toml +2 -7
- data/doc/application_architecture.md +96 -22
- data/doc/application_testing.md +76 -30
- data/doc/contributors/architectural_overhaul/chat_conversations.md +4952 -0
- data/doc/contributors/architectural_overhaul/implementation_plan.md +60 -0
- data/doc/contributors/architectural_overhaul/task.md +37 -0
- data/doc/contributors/design/ruby_frontend.md +288 -56
- data/doc/contributors/design/rust_backend.md +349 -54
- data/doc/contributors/developing_examples.md +134 -49
- data/doc/contributors/index.md +7 -5
- data/doc/contributors/v1.0.0_blockers.md +1729 -0
- data/doc/event_handling.md +11 -3
- data/doc/images/app_all_events.png +0 -0
- data/doc/images/app_color_picker.png +0 -0
- data/doc/images/app_login_form.png +0 -0
- data/doc/images/app_stateful_interaction.png +0 -0
- data/doc/images/verify_quickstart_dsl.png +0 -0
- data/doc/images/verify_quickstart_layout.png +0 -0
- data/doc/images/verify_quickstart_lifecycle.png +0 -0
- data/doc/images/verify_readme_usage.png +0 -0
- data/doc/images/widget_barchart_demo.png +0 -0
- data/doc/images/widget_block_demo.png +0 -0
- data/doc/images/widget_canvas_demo.png +0 -0
- data/doc/images/widget_cell_demo.png +0 -0
- data/doc/images/widget_center_demo.png +0 -0
- data/doc/images/widget_chart_demo.png +0 -0
- data/doc/images/widget_list_demo.png +0 -0
- data/doc/images/widget_overlay_demo.png +0 -0
- data/doc/images/widget_render.png +0 -0
- data/doc/images/widget_rich_text.png +0 -0
- data/doc/images/widget_scroll_text.png +0 -0
- data/doc/images/widget_sparkline_demo.png +0 -0
- data/doc/images/widget_table_demo.png +0 -0
- data/doc/images/widget_tabs_demo.png +0 -0
- data/doc/images/widget_text_width.png +0 -0
- data/doc/index.md +11 -6
- data/doc/interactive_design.md +2 -2
- data/doc/quickstart.md +127 -165
- data/doc/terminal_limitations.md +92 -0
- data/doc/v0.7.0_migration.md +236 -0
- data/doc/why.md +93 -0
- data/examples/app_all_events/README.md +47 -27
- data/examples/app_all_events/app.rb +38 -35
- data/examples/app_all_events/model/app_model.rb +157 -0
- data/examples/app_all_events/model/event_entry.rb +17 -0
- data/examples/app_all_events/model/msg.rb +37 -0
- data/examples/app_all_events/update.rb +73 -0
- data/examples/app_all_events/view/app_view.rb +9 -9
- data/examples/app_all_events/view/controls_view.rb +9 -7
- data/examples/app_all_events/view/counts_view.rb +13 -9
- data/examples/app_all_events/view/live_view.rb +9 -8
- data/examples/app_all_events/view/log_view.rb +11 -16
- data/examples/app_color_picker/README.md +84 -42
- data/examples/app_color_picker/app.rb +24 -62
- data/examples/app_color_picker/controls.rb +90 -0
- data/examples/app_color_picker/copy_dialog.rb +45 -49
- data/examples/app_color_picker/export_pane.rb +126 -0
- data/examples/app_color_picker/input.rb +99 -67
- data/examples/app_color_picker/main_container.rb +178 -0
- data/examples/app_color_picker/palette.rb +55 -26
- data/examples/app_login_form/README.md +49 -0
- data/examples/app_login_form/app.rb +2 -3
- data/examples/app_stateful_interaction/README.md +33 -0
- data/examples/app_stateful_interaction/app.rb +272 -0
- data/examples/timeout_demo.rb +43 -0
- data/examples/verify_quickstart_dsl/README.md +49 -0
- data/examples/verify_quickstart_dsl/app.rb +2 -0
- data/examples/verify_quickstart_layout/README.md +71 -0
- data/examples/verify_quickstart_layout/app.rb +2 -0
- data/examples/verify_quickstart_lifecycle/README.md +56 -0
- data/examples/verify_quickstart_lifecycle/app.rb +10 -4
- data/examples/verify_readme_usage/README.md +43 -0
- data/examples/verify_readme_usage/app.rb +8 -2
- data/examples/widget_barchart_demo/README.md +50 -0
- data/examples/widget_barchart_demo/app.rb +5 -5
- data/examples/widget_block_demo/README.md +36 -0
- data/examples/widget_block_demo/app.rb +256 -0
- data/examples/widget_box_demo/README.md +45 -0
- data/examples/widget_calendar_demo/README.md +39 -0
- data/examples/widget_calendar_demo/app.rb +5 -1
- data/examples/widget_canvas_demo/README.md +27 -0
- data/examples/widget_canvas_demo/app.rb +123 -0
- data/examples/widget_cell_demo/README.md +36 -0
- data/examples/widget_cell_demo/app.rb +31 -24
- data/examples/widget_center_demo/README.md +29 -0
- data/examples/widget_center_demo/app.rb +116 -0
- data/examples/widget_chart_demo/README.md +41 -0
- data/examples/widget_chart_demo/app.rb +7 -2
- data/examples/widget_gauge_demo/README.md +41 -0
- data/examples/widget_layout_split/README.md +44 -0
- data/examples/widget_line_gauge_demo/README.md +41 -0
- data/examples/widget_list_demo/README.md +49 -0
- data/examples/widget_list_demo/app.rb +91 -107
- data/examples/widget_map_demo/README.md +39 -0
- data/examples/{app_map_demo → widget_map_demo}/app.rb +4 -4
- data/examples/widget_overlay_demo/README.md +36 -0
- data/examples/widget_overlay_demo/app.rb +248 -0
- data/examples/widget_popup_demo/README.md +36 -0
- data/examples/widget_ratatui_logo_demo/README.md +34 -0
- data/examples/widget_ratatui_logo_demo/app.rb +1 -1
- data/examples/widget_ratatui_mascot_demo/README.md +34 -0
- data/examples/widget_rect/README.md +38 -0
- data/examples/widget_render/README.md +37 -0
- data/examples/widget_render/app.rb +3 -3
- data/examples/widget_rich_text/README.md +35 -0
- data/examples/widget_rich_text/app.rb +62 -33
- data/examples/widget_scroll_text/README.md +37 -0
- data/examples/widget_scroll_text/app.rb +0 -1
- data/examples/widget_scrollbar_demo/README.md +37 -0
- data/examples/widget_sparkline_demo/README.md +42 -0
- data/examples/widget_sparkline_demo/app.rb +4 -3
- data/examples/widget_style_colors/README.md +34 -0
- data/examples/widget_table_demo/README.md +48 -0
- data/examples/{app_table_select → widget_table_demo}/app.rb +65 -12
- data/examples/widget_tabs_demo/README.md +41 -0
- data/examples/widget_tabs_demo/app.rb +15 -1
- data/examples/widget_text_width/README.md +35 -0
- data/examples/widget_text_width/app.rb +113 -0
- data/exe/.gitkeep +0 -0
- data/ext/ratatui_ruby/Cargo.lock +11 -4
- data/ext/ratatui_ruby/Cargo.toml +2 -1
- data/ext/ratatui_ruby/src/events.rs +238 -26
- data/ext/ratatui_ruby/src/frame.rs +116 -3
- data/ext/ratatui_ruby/src/lib.rs +37 -6
- data/ext/ratatui_ruby/src/rendering.rs +22 -21
- data/ext/ratatui_ruby/src/string_width.rs +101 -0
- data/ext/ratatui_ruby/src/terminal.rs +39 -15
- data/ext/ratatui_ruby/src/text.rs +13 -4
- data/ext/ratatui_ruby/src/widgets/barchart.rs +24 -6
- data/ext/ratatui_ruby/src/widgets/canvas.rs +5 -5
- data/ext/ratatui_ruby/src/widgets/gauge.rs +9 -2
- data/ext/ratatui_ruby/src/widgets/line_gauge.rs +9 -2
- data/ext/ratatui_ruby/src/widgets/list.rs +179 -3
- data/ext/ratatui_ruby/src/widgets/list_state.rs +137 -0
- data/ext/ratatui_ruby/src/widgets/mod.rs +3 -0
- data/ext/ratatui_ruby/src/widgets/scrollbar.rs +93 -1
- data/ext/ratatui_ruby/src/widgets/scrollbar_state.rs +169 -0
- data/ext/ratatui_ruby/src/widgets/table.rs +191 -34
- data/ext/ratatui_ruby/src/widgets/table_state.rs +121 -0
- data/lib/ratatui_ruby/buffer/cell.rb +168 -0
- data/lib/ratatui_ruby/buffer.rb +15 -0
- data/lib/ratatui_ruby/cell.rb +4 -4
- data/lib/ratatui_ruby/event/key/character.rb +35 -0
- data/lib/ratatui_ruby/event/key/media.rb +44 -0
- data/lib/ratatui_ruby/event/key/modifier.rb +95 -0
- data/lib/ratatui_ruby/event/key/navigation.rb +55 -0
- data/lib/ratatui_ruby/event/key/system.rb +45 -0
- data/lib/ratatui_ruby/event/key.rb +111 -51
- data/lib/ratatui_ruby/event/mouse.rb +3 -3
- data/lib/ratatui_ruby/event/paste.rb +1 -1
- data/lib/ratatui_ruby/frame.rb +100 -4
- data/lib/ratatui_ruby/layout/constraint.rb +95 -0
- data/lib/ratatui_ruby/layout/layout.rb +106 -0
- data/lib/ratatui_ruby/layout/rect.rb +118 -0
- data/lib/ratatui_ruby/layout.rb +19 -0
- data/lib/ratatui_ruby/list_state.rb +88 -0
- data/lib/ratatui_ruby/schema/bar_chart/bar.rb +2 -2
- data/lib/ratatui_ruby/schema/cursor.rb +5 -0
- data/lib/ratatui_ruby/schema/gauge.rb +3 -1
- data/lib/ratatui_ruby/schema/layout.rb +1 -1
- data/lib/ratatui_ruby/schema/line_gauge.rb +2 -2
- data/lib/ratatui_ruby/schema/list.rb +25 -4
- data/lib/ratatui_ruby/schema/list_item.rb +41 -0
- data/lib/ratatui_ruby/schema/rect.rb +43 -0
- data/lib/ratatui_ruby/schema/row.rb +66 -0
- data/lib/ratatui_ruby/schema/style.rb +24 -4
- data/lib/ratatui_ruby/schema/table.rb +29 -11
- data/lib/ratatui_ruby/schema/text.rb +96 -3
- data/lib/ratatui_ruby/scrollbar_state.rb +112 -0
- data/lib/ratatui_ruby/style/style.rb +81 -0
- data/lib/ratatui_ruby/style.rb +15 -0
- data/lib/ratatui_ruby/table_state.rb +90 -0
- data/lib/ratatui_ruby/test_helper/event_injection.rb +169 -0
- data/lib/ratatui_ruby/test_helper/snapshot.rb +414 -0
- data/lib/ratatui_ruby/test_helper/style_assertions.rb +351 -0
- data/lib/ratatui_ruby/test_helper/terminal.rb +127 -0
- data/lib/ratatui_ruby/test_helper/test_doubles.rb +68 -0
- data/lib/ratatui_ruby/test_helper.rb +65 -358
- data/lib/ratatui_ruby/tui/buffer_factories.rb +20 -0
- data/lib/ratatui_ruby/tui/canvas_factories.rb +44 -0
- data/lib/ratatui_ruby/tui/core.rb +38 -0
- data/lib/ratatui_ruby/tui/layout_factories.rb +74 -0
- data/lib/ratatui_ruby/tui/state_factories.rb +33 -0
- data/lib/ratatui_ruby/tui/style_factories.rb +20 -0
- data/lib/ratatui_ruby/tui/text_factories.rb +44 -0
- data/lib/ratatui_ruby/tui/widget_factories.rb +195 -0
- data/lib/ratatui_ruby/tui.rb +75 -0
- data/lib/ratatui_ruby/version.rb +1 -1
- data/lib/ratatui_ruby/widgets/bar_chart/bar.rb +47 -0
- data/lib/ratatui_ruby/widgets/bar_chart/bar_group.rb +25 -0
- data/lib/ratatui_ruby/widgets/bar_chart.rb +239 -0
- data/lib/ratatui_ruby/widgets/block.rb +192 -0
- data/lib/ratatui_ruby/widgets/calendar.rb +84 -0
- data/lib/ratatui_ruby/widgets/canvas.rb +231 -0
- data/lib/ratatui_ruby/widgets/cell.rb +47 -0
- data/lib/ratatui_ruby/widgets/center.rb +59 -0
- data/lib/ratatui_ruby/widgets/chart.rb +185 -0
- data/lib/ratatui_ruby/widgets/clear.rb +54 -0
- data/lib/ratatui_ruby/widgets/cursor.rb +42 -0
- data/lib/ratatui_ruby/widgets/gauge.rb +72 -0
- data/lib/ratatui_ruby/widgets/line_gauge.rb +80 -0
- data/lib/ratatui_ruby/widgets/list.rb +127 -0
- data/lib/ratatui_ruby/widgets/list_item.rb +43 -0
- data/lib/ratatui_ruby/widgets/overlay.rb +43 -0
- data/lib/ratatui_ruby/widgets/paragraph.rb +99 -0
- data/lib/ratatui_ruby/widgets/ratatui_logo.rb +31 -0
- data/lib/ratatui_ruby/widgets/ratatui_mascot.rb +36 -0
- data/lib/ratatui_ruby/widgets/row.rb +68 -0
- data/lib/ratatui_ruby/widgets/scrollbar.rb +143 -0
- data/lib/ratatui_ruby/widgets/shape/label.rb +68 -0
- data/lib/ratatui_ruby/widgets/sparkline.rb +134 -0
- data/lib/ratatui_ruby/widgets/table.rb +141 -0
- data/lib/ratatui_ruby/widgets/tabs.rb +85 -0
- data/lib/ratatui_ruby/widgets.rb +40 -0
- data/lib/ratatui_ruby.rb +64 -57
- data/sig/examples/app_all_events/view.rbs +1 -1
- data/sig/examples/app_all_events/view_state.rbs +1 -1
- data/sig/examples/app_stateful_interaction/app.rbs +33 -0
- data/sig/examples/widget_block_demo/app.rbs +32 -0
- data/sig/examples/{app_map_demo → widget_map_demo}/app.rbs +2 -2
- data/sig/examples/{app_table_select → widget_table_demo}/app.rbs +2 -2
- data/sig/examples/{widget_table_flex → widget_text_width}/app.rbs +2 -3
- data/sig/ratatui_ruby/event.rbs +11 -1
- data/sig/ratatui_ruby/frame.rbs +2 -0
- data/sig/ratatui_ruby/list_state.rbs +13 -0
- data/sig/ratatui_ruby/ratatui_ruby.rbs +2 -2
- data/sig/ratatui_ruby/schema/bar_chart/bar.rbs +3 -3
- data/sig/ratatui_ruby/schema/gauge.rbs +2 -2
- data/sig/ratatui_ruby/schema/line_gauge.rbs +2 -2
- data/sig/ratatui_ruby/schema/list.rbs +4 -2
- data/sig/ratatui_ruby/schema/list_item.rbs +10 -0
- data/sig/ratatui_ruby/schema/rect.rbs +3 -0
- data/sig/ratatui_ruby/schema/row.rbs +22 -0
- data/sig/ratatui_ruby/schema/style.rbs +3 -3
- data/sig/ratatui_ruby/schema/table.rbs +3 -1
- data/sig/ratatui_ruby/schema/text.rbs +9 -6
- data/sig/ratatui_ruby/scrollbar_state.rbs +18 -0
- data/sig/ratatui_ruby/session.rbs +41 -48
- data/sig/ratatui_ruby/table_state.rbs +15 -0
- data/sig/ratatui_ruby/test_helper/event_injection.rbs +16 -0
- data/sig/ratatui_ruby/test_helper/snapshot.rbs +12 -0
- data/sig/ratatui_ruby/test_helper/style_assertions.rbs +64 -0
- data/sig/ratatui_ruby/test_helper/terminal.rbs +14 -0
- data/sig/ratatui_ruby/test_helper/test_doubles.rbs +22 -0
- data/sig/ratatui_ruby/test_helper.rbs +5 -4
- data/sig/ratatui_ruby/tui/buffer_factories.rbs +10 -0
- data/sig/ratatui_ruby/tui/canvas_factories.rbs +14 -0
- data/sig/ratatui_ruby/tui/core.rbs +14 -0
- data/sig/ratatui_ruby/tui/layout_factories.rbs +19 -0
- data/sig/ratatui_ruby/tui/state_factories.rbs +12 -0
- data/sig/ratatui_ruby/tui/style_factories.rbs +10 -0
- data/sig/ratatui_ruby/tui/text_factories.rbs +14 -0
- data/sig/ratatui_ruby/tui/widget_factories.rbs +39 -0
- data/sig/ratatui_ruby/tui.rbs +19 -0
- data/tasks/autodoc/examples.rb +79 -0
- data/tasks/autodoc.rake +7 -35
- data/tasks/bump/changelog.rb +3 -3
- data/tasks/bump/links.rb +67 -0
- data/tasks/sourcehut.rake +64 -21
- data/tasks/terminal_preview/app_screenshot.rb +13 -3
- data/tasks/terminal_preview/saved_screenshot.rb +4 -3
- metadata +169 -48
- data/doc/contributors/dwim_dx.md +0 -366
- data/doc/images/app_analytics.png +0 -0
- data/doc/images/app_custom_widget.png +0 -0
- data/doc/images/app_mouse_events.png +0 -0
- data/doc/images/app_table_select.png +0 -0
- data/doc/images/widget_block_padding.png +0 -0
- data/doc/images/widget_block_titles.png +0 -0
- data/doc/images/widget_list_styles.png +0 -0
- data/doc/images/widget_table_flex.png +0 -0
- data/examples/app_all_events/model/events.rb +0 -180
- data/examples/app_all_events/model/highlight.rb +0 -57
- data/examples/app_all_events/test/snapshots/after_focus_lost.txt +0 -24
- data/examples/app_all_events/test/snapshots/after_focus_regained.txt +0 -24
- data/examples/app_all_events/test/snapshots/after_horizontal_resize.txt +0 -24
- data/examples/app_all_events/test/snapshots/after_key_a.txt +0 -24
- data/examples/app_all_events/test/snapshots/after_key_ctrl_x.txt +0 -24
- data/examples/app_all_events/test/snapshots/after_mouse_click.txt +0 -24
- data/examples/app_all_events/test/snapshots/after_mouse_drag.txt +0 -24
- data/examples/app_all_events/test/snapshots/after_multiple_events.txt +0 -24
- data/examples/app_all_events/test/snapshots/after_paste.txt +0 -24
- data/examples/app_all_events/test/snapshots/after_resize.txt +0 -24
- data/examples/app_all_events/test/snapshots/after_right_click.txt +0 -24
- data/examples/app_all_events/test/snapshots/after_vertical_resize.txt +0 -24
- data/examples/app_all_events/test/snapshots/initial_state.txt +0 -24
- data/examples/app_all_events/view_state.rb +0 -42
- data/examples/app_color_picker/scene.rb +0 -201
- data/examples/widget_block_padding/app.rb +0 -67
- data/examples/widget_block_titles/app.rb +0 -69
- data/examples/widget_list_styles/app.rb +0 -141
- data/examples/widget_table_flex/app.rb +0 -95
- data/lib/ratatui_ruby/session/autodoc.rb +0 -417
- data/lib/ratatui_ruby/session.rb +0 -163
- data/sig/examples/widget_block_padding/app.rbs +0 -11
- data/sig/examples/widget_block_titles/app.rbs +0 -11
- data/sig/examples/widget_list_styles/app.rbs +0 -11
- data/tasks/autodoc/inventory.rb +0 -61
- data/tasks/autodoc/notice.rb +0 -26
- data/tasks/autodoc/rbs.rb +0 -38
- data/tasks/autodoc/rdoc.rb +0 -45
- data/tasks/bump/comparison_links.rb +0 -41
- /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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
87
|
+
## Type Signatures
|
|
88
88
|
|
|
89
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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)
|
|
195
|
-
@app.run
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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
|
-
|
|
257
|
+
Snapshot auto-saved to: `test/examples/widget_foo_demo/snapshots/initial_render.txt`
|
|
228
258
|
|
|
229
|
-
|
|
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
|
-
|
|
261
|
+
For examples with timestamps, random data, or other non-deterministic output:
|
|
234
262
|
|
|
235
|
-
|
|
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
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
-
|
|
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
|