ratatui_ruby 0.3.1 → 0.5.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 +14 -12
- data/.builds/ruby-3.3.yml +14 -12
- data/.builds/ruby-3.4.yml +14 -12
- data/.builds/ruby-4.0.0.yml +14 -12
- data/AGENTS.md +89 -132
- data/CHANGELOG.md +223 -1
- data/README.md +23 -16
- data/REUSE.toml +20 -0
- data/doc/application_architecture.md +176 -0
- data/doc/application_testing.md +17 -10
- data/doc/contributors/design/ruby_frontend.md +11 -7
- data/doc/contributors/developing_examples.md +261 -0
- data/doc/contributors/documentation_style.md +104 -0
- data/doc/contributors/dwim_dx.md +366 -0
- data/doc/contributors/index.md +2 -0
- data/doc/custom.css +14 -0
- data/doc/event_handling.md +125 -0
- data/doc/images/app_all_events.png +0 -0
- data/doc/images/app_analytics.png +0 -0
- data/doc/images/app_color_picker.png +0 -0
- data/doc/images/app_custom_widget.png +0 -0
- data/doc/images/app_login_form.png +0 -0
- data/doc/images/app_map_demo.png +0 -0
- data/doc/images/app_mouse_events.png +0 -0
- data/doc/images/app_table_select.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_padding.png +0 -0
- data/doc/images/widget_block_titles.png +0 -0
- data/doc/images/widget_box_demo.png +0 -0
- data/doc/images/widget_calendar_demo.png +0 -0
- data/doc/images/widget_cell_demo.png +0 -0
- data/doc/images/widget_chart_demo.png +0 -0
- data/doc/images/widget_gauge_demo.png +0 -0
- data/doc/images/widget_layout_split.png +0 -0
- data/doc/images/widget_line_gauge_demo.png +0 -0
- data/doc/images/widget_list_demo.png +0 -0
- data/doc/images/widget_list_styles.png +0 -0
- data/doc/images/widget_popup_demo.png +0 -0
- data/doc/images/widget_ratatui_logo_demo.png +0 -0
- data/doc/images/widget_ratatui_mascot_demo.png +0 -0
- data/doc/images/widget_rect.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_scrollbar_demo.png +0 -0
- data/doc/images/widget_sparkline_demo.png +0 -0
- data/doc/images/widget_style_colors.png +0 -0
- data/doc/images/widget_table_flex.png +0 -0
- data/doc/images/widget_tabs_demo.png +0 -0
- data/doc/index.md +1 -0
- data/doc/interactive_design.md +116 -0
- data/doc/quickstart.md +186 -84
- data/examples/app_all_events/README.md +81 -0
- data/examples/app_all_events/app.rb +93 -0
- data/examples/app_all_events/model/event_color_cycle.rb +41 -0
- data/examples/app_all_events/model/event_entry.rb +75 -0
- data/examples/app_all_events/model/events.rb +180 -0
- data/examples/app_all_events/model/highlight.rb +57 -0
- data/examples/app_all_events/model/timestamp.rb +54 -0
- data/examples/app_all_events/test/snapshots/after_focus_lost.txt +24 -0
- data/examples/app_all_events/test/snapshots/after_focus_regained.txt +24 -0
- data/examples/app_all_events/test/snapshots/after_horizontal_resize.txt +24 -0
- data/examples/app_all_events/test/snapshots/after_key_a.txt +24 -0
- data/examples/app_all_events/test/snapshots/after_key_ctrl_x.txt +24 -0
- data/examples/app_all_events/test/snapshots/after_mouse_click.txt +24 -0
- data/examples/app_all_events/test/snapshots/after_mouse_drag.txt +24 -0
- data/examples/app_all_events/test/snapshots/after_multiple_events.txt +24 -0
- data/examples/app_all_events/test/snapshots/after_paste.txt +24 -0
- data/examples/app_all_events/test/snapshots/after_resize.txt +24 -0
- data/examples/app_all_events/test/snapshots/after_right_click.txt +24 -0
- data/examples/app_all_events/test/snapshots/after_vertical_resize.txt +24 -0
- data/examples/app_all_events/test/snapshots/initial_state.txt +24 -0
- data/examples/app_all_events/view/app_view.rb +78 -0
- data/examples/app_all_events/view/controls_view.rb +50 -0
- data/examples/app_all_events/view/counts_view.rb +55 -0
- data/examples/app_all_events/view/live_view.rb +69 -0
- data/examples/app_all_events/view/log_view.rb +60 -0
- data/{lib/ratatui_ruby/output.rb → examples/app_all_events/view.rb} +1 -1
- data/examples/app_all_events/view_state.rb +42 -0
- data/examples/app_color_picker/README.md +94 -0
- data/examples/app_color_picker/app.rb +112 -0
- data/examples/app_color_picker/clipboard.rb +84 -0
- data/examples/app_color_picker/color.rb +191 -0
- data/examples/app_color_picker/copy_dialog.rb +170 -0
- data/examples/app_color_picker/harmony.rb +56 -0
- data/examples/app_color_picker/input.rb +142 -0
- data/examples/app_color_picker/palette.rb +80 -0
- data/examples/app_color_picker/scene.rb +201 -0
- data/examples/app_login_form/app.rb +108 -0
- data/examples/app_map_demo/app.rb +93 -0
- data/examples/app_table_select/app.rb +201 -0
- data/examples/verify_quickstart_dsl/app.rb +45 -0
- data/examples/verify_quickstart_layout/app.rb +69 -0
- data/examples/verify_quickstart_lifecycle/app.rb +48 -0
- data/examples/verify_readme_usage/app.rb +34 -0
- data/examples/widget_barchart_demo/app.rb +238 -0
- data/examples/widget_block_padding/app.rb +67 -0
- data/examples/widget_block_titles/app.rb +69 -0
- data/examples/widget_box_demo/app.rb +250 -0
- data/examples/widget_calendar_demo/app.rb +109 -0
- data/examples/widget_cell_demo/app.rb +104 -0
- data/examples/widget_chart_demo/app.rb +213 -0
- data/examples/widget_gauge_demo/app.rb +212 -0
- data/examples/widget_layout_split/app.rb +246 -0
- data/examples/widget_line_gauge_demo/app.rb +217 -0
- data/examples/widget_list_demo/app.rb +382 -0
- data/examples/widget_list_styles/app.rb +141 -0
- data/examples/widget_popup_demo/app.rb +104 -0
- data/examples/widget_ratatui_logo_demo/app.rb +103 -0
- data/examples/widget_ratatui_mascot_demo/app.rb +93 -0
- data/examples/widget_rect/app.rb +205 -0
- data/examples/widget_render/app.rb +184 -0
- data/examples/widget_rich_text/app.rb +137 -0
- data/examples/widget_scroll_text/app.rb +108 -0
- data/examples/widget_scrollbar_demo/app.rb +153 -0
- data/examples/widget_sparkline_demo/app.rb +274 -0
- data/examples/widget_style_colors/app.rb +102 -0
- data/examples/widget_table_flex/app.rb +95 -0
- data/examples/widget_tabs_demo/app.rb +167 -0
- data/ext/ratatui_ruby/Cargo.lock +889 -115
- data/ext/ratatui_ruby/Cargo.toml +4 -3
- data/ext/ratatui_ruby/clippy.toml +7 -0
- data/ext/ratatui_ruby/extconf.rb +7 -0
- data/ext/ratatui_ruby/src/events.rs +293 -219
- data/ext/ratatui_ruby/src/frame.rs +115 -0
- data/ext/ratatui_ruby/src/lib.rs +105 -24
- data/ext/ratatui_ruby/src/rendering.rs +94 -10
- data/ext/ratatui_ruby/src/style.rs +357 -93
- data/ext/ratatui_ruby/src/terminal.rs +121 -31
- data/ext/ratatui_ruby/src/text.rs +178 -0
- data/ext/ratatui_ruby/src/widgets/barchart.rs +99 -24
- data/ext/ratatui_ruby/src/widgets/block.rs +32 -3
- data/ext/ratatui_ruby/src/widgets/calendar.rs +45 -44
- data/ext/ratatui_ruby/src/widgets/canvas.rs +44 -9
- data/ext/ratatui_ruby/src/widgets/chart.rs +79 -27
- data/ext/ratatui_ruby/src/widgets/clear.rs +3 -1
- data/ext/ratatui_ruby/src/widgets/gauge.rs +11 -4
- data/ext/ratatui_ruby/src/widgets/layout.rs +223 -15
- data/ext/ratatui_ruby/src/widgets/line_gauge.rs +92 -0
- data/ext/ratatui_ruby/src/widgets/list.rs +114 -11
- data/ext/ratatui_ruby/src/widgets/mod.rs +3 -0
- data/ext/ratatui_ruby/src/widgets/overlay.rs +4 -2
- data/ext/ratatui_ruby/src/widgets/paragraph.rs +35 -13
- data/ext/ratatui_ruby/src/widgets/ratatui_logo.rs +40 -0
- data/ext/ratatui_ruby/src/widgets/ratatui_mascot.rs +51 -0
- data/ext/ratatui_ruby/src/widgets/scrollbar.rs +61 -7
- data/ext/ratatui_ruby/src/widgets/sparkline.rs +73 -6
- data/ext/ratatui_ruby/src/widgets/table.rs +177 -64
- data/ext/ratatui_ruby/src/widgets/tabs.rs +105 -5
- data/lib/ratatui_ruby/cell.rb +166 -0
- data/lib/ratatui_ruby/event/focus_gained.rb +49 -0
- data/lib/ratatui_ruby/event/focus_lost.rb +50 -0
- data/lib/ratatui_ruby/event/key.rb +211 -0
- data/lib/ratatui_ruby/event/mouse.rb +124 -0
- data/lib/ratatui_ruby/event/none.rb +43 -0
- data/lib/ratatui_ruby/event/paste.rb +71 -0
- data/lib/ratatui_ruby/event/resize.rb +80 -0
- data/lib/ratatui_ruby/event.rb +131 -0
- data/lib/ratatui_ruby/frame.rb +87 -0
- data/lib/ratatui_ruby/schema/bar_chart/bar.rb +45 -0
- data/lib/ratatui_ruby/schema/bar_chart/bar_group.rb +23 -0
- data/lib/ratatui_ruby/schema/bar_chart.rb +226 -17
- data/lib/ratatui_ruby/schema/block.rb +178 -11
- data/lib/ratatui_ruby/schema/calendar.rb +70 -14
- data/lib/ratatui_ruby/schema/canvas.rb +213 -46
- data/lib/ratatui_ruby/schema/center.rb +46 -8
- data/lib/ratatui_ruby/schema/chart.rb +134 -32
- data/lib/ratatui_ruby/schema/clear.rb +22 -53
- data/lib/ratatui_ruby/schema/constraint.rb +72 -12
- data/lib/ratatui_ruby/schema/cursor.rb +23 -5
- data/lib/ratatui_ruby/schema/draw.rb +53 -0
- data/lib/ratatui_ruby/schema/gauge.rb +56 -12
- data/lib/ratatui_ruby/schema/layout.rb +91 -9
- data/lib/ratatui_ruby/schema/line_gauge.rb +78 -0
- data/lib/ratatui_ruby/schema/list.rb +92 -16
- data/lib/ratatui_ruby/schema/overlay.rb +29 -3
- data/lib/ratatui_ruby/schema/paragraph.rb +82 -25
- data/lib/ratatui_ruby/schema/ratatui_logo.rb +29 -0
- data/lib/ratatui_ruby/schema/ratatui_mascot.rb +34 -0
- data/lib/ratatui_ruby/schema/rect.rb +59 -10
- data/lib/ratatui_ruby/schema/scrollbar.rb +127 -19
- data/lib/ratatui_ruby/schema/shape/label.rb +66 -0
- data/lib/ratatui_ruby/schema/sparkline.rb +120 -12
- data/lib/ratatui_ruby/schema/style.rb +39 -11
- data/lib/ratatui_ruby/schema/table.rb +109 -18
- data/lib/ratatui_ruby/schema/tabs.rb +71 -10
- data/lib/ratatui_ruby/schema/text.rb +90 -0
- data/lib/ratatui_ruby/session/autodoc.rb +417 -0
- data/lib/ratatui_ruby/session.rb +163 -0
- data/lib/ratatui_ruby/test_helper.rb +322 -13
- data/lib/ratatui_ruby/version.rb +1 -1
- data/lib/ratatui_ruby.rb +184 -38
- data/sig/examples/app_all_events/app.rbs +11 -0
- data/sig/examples/app_all_events/model/event_entry.rbs +16 -0
- data/sig/examples/app_all_events/model/events.rbs +15 -0
- data/sig/examples/app_all_events/model/timestamp.rbs +11 -0
- data/sig/examples/app_all_events/view/app_view.rbs +8 -0
- data/sig/examples/app_all_events/view/controls_view.rbs +6 -0
- data/sig/examples/app_all_events/view/counts_view.rbs +6 -0
- data/sig/examples/app_all_events/view/live_view.rbs +6 -0
- data/sig/examples/app_all_events/view/log_view.rbs +6 -0
- data/sig/examples/app_all_events/view.rbs +8 -0
- data/sig/examples/app_all_events/view_state.rbs +15 -0
- data/sig/examples/app_color_picker/app.rbs +12 -0
- data/sig/examples/app_login_form/app.rbs +11 -0
- data/sig/examples/app_map_demo/app.rbs +11 -0
- data/sig/examples/app_table_select/app.rbs +11 -0
- data/sig/examples/verify_quickstart_dsl/app.rbs +11 -0
- data/sig/examples/verify_quickstart_lifecycle/app.rbs +11 -0
- data/sig/examples/verify_readme_usage/app.rbs +11 -0
- data/sig/examples/widget_block_padding/app.rbs +11 -0
- data/sig/examples/widget_block_titles/app.rbs +11 -0
- data/sig/examples/widget_box_demo/app.rbs +11 -0
- data/sig/examples/widget_calendar_demo/app.rbs +11 -0
- data/sig/examples/widget_cell_demo/app.rbs +11 -0
- data/sig/examples/widget_chart_demo/app.rbs +11 -0
- data/sig/examples/widget_gauge_demo/app.rbs +11 -0
- data/sig/examples/widget_layout_split/app.rbs +10 -0
- data/sig/examples/widget_line_gauge_demo/app.rbs +11 -0
- data/sig/examples/widget_list_demo/app.rbs +12 -0
- data/sig/examples/widget_list_styles/app.rbs +11 -0
- data/sig/examples/widget_popup_demo/app.rbs +11 -0
- data/sig/examples/widget_ratatui_logo_demo/app.rbs +11 -0
- data/sig/examples/widget_ratatui_mascot_demo/app.rbs +11 -0
- data/sig/examples/widget_rect/app.rbs +12 -0
- data/sig/examples/widget_render/app.rbs +10 -0
- data/sig/examples/widget_rich_text/app.rbs +11 -0
- data/sig/examples/widget_scroll_text/app.rbs +11 -0
- data/sig/examples/widget_scrollbar_demo/app.rbs +11 -0
- data/sig/examples/widget_sparkline_demo/app.rbs +10 -0
- data/sig/examples/widget_style_colors/app.rbs +14 -0
- data/sig/examples/widget_table_flex/app.rbs +11 -0
- data/sig/ratatui_ruby/event.rbs +69 -0
- data/sig/ratatui_ruby/frame.rbs +9 -0
- data/sig/ratatui_ruby/ratatui_ruby.rbs +5 -3
- data/sig/ratatui_ruby/schema/bar_chart/bar.rbs +16 -0
- data/sig/ratatui_ruby/schema/bar_chart/bar_group.rbs +13 -0
- data/sig/ratatui_ruby/schema/bar_chart.rbs +20 -2
- data/sig/ratatui_ruby/schema/block.rbs +5 -4
- data/sig/ratatui_ruby/schema/calendar.rbs +6 -2
- data/sig/ratatui_ruby/schema/canvas.rbs +52 -39
- data/sig/ratatui_ruby/schema/center.rbs +3 -3
- data/sig/ratatui_ruby/schema/chart.rbs +8 -5
- data/sig/ratatui_ruby/schema/constraint.rbs +8 -5
- data/sig/ratatui_ruby/schema/cursor.rbs +1 -1
- data/sig/ratatui_ruby/schema/draw.rbs +27 -0
- data/sig/ratatui_ruby/schema/gauge.rbs +4 -2
- data/sig/ratatui_ruby/schema/layout.rbs +11 -1
- data/sig/ratatui_ruby/schema/line_gauge.rbs +16 -0
- data/sig/ratatui_ruby/schema/list.rbs +5 -1
- data/sig/ratatui_ruby/schema/paragraph.rbs +4 -1
- data/sig/ratatui_ruby/schema/ratatui_logo.rbs +8 -0
- data/sig/ratatui_ruby/{buffer.rbs → schema/ratatui_mascot.rbs} +4 -3
- data/sig/ratatui_ruby/schema/rect.rbs +2 -1
- data/sig/ratatui_ruby/schema/scrollbar.rbs +18 -2
- data/sig/ratatui_ruby/schema/sparkline.rbs +6 -2
- data/sig/ratatui_ruby/schema/table.rbs +8 -1
- data/sig/ratatui_ruby/schema/tabs.rbs +5 -1
- data/sig/ratatui_ruby/schema/text.rbs +22 -0
- data/sig/ratatui_ruby/session.rbs +94 -0
- data/tasks/autodoc/inventory.rb +61 -0
- data/tasks/autodoc/member.rb +56 -0
- data/tasks/autodoc/name.rb +19 -0
- data/tasks/autodoc/notice.rb +26 -0
- data/tasks/autodoc/rbs.rb +38 -0
- data/tasks/autodoc/rdoc.rb +45 -0
- data/tasks/autodoc.rake +47 -0
- data/tasks/bump/history.rb +2 -2
- data/tasks/doc.rake +600 -6
- data/tasks/example_viewer.html.erb +172 -0
- data/tasks/lint.rake +8 -4
- data/tasks/resources/build.yml.erb +13 -11
- data/tasks/resources/index.html.erb +6 -0
- data/tasks/sourcehut.rake +4 -4
- data/tasks/terminal_preview/app_screenshot.rb +33 -0
- data/tasks/terminal_preview/crash_report.rb +52 -0
- data/tasks/terminal_preview/example_app.rb +25 -0
- data/tasks/terminal_preview/launcher_script.rb +46 -0
- data/tasks/terminal_preview/preview_collection.rb +58 -0
- data/tasks/terminal_preview/preview_timing.rb +22 -0
- data/tasks/terminal_preview/safety_confirmation.rb +56 -0
- data/tasks/terminal_preview/saved_screenshot.rb +53 -0
- data/tasks/terminal_preview/system_appearance.rb +11 -0
- data/tasks/terminal_preview/terminal_window.rb +136 -0
- data/tasks/terminal_preview/window_id.rb +14 -0
- data/tasks/terminal_preview.rake +28 -0
- data/tasks/test.rake +2 -2
- data/tasks/website/index_page.rb +3 -3
- data/tasks/website/version.rb +10 -10
- data/tasks/website/version_menu.rb +10 -12
- data/tasks/website/versioned_documentation.rb +49 -17
- data/tasks/website/website.rb +6 -8
- data/tasks/website.rake +4 -4
- metadata +206 -54
- data/LICENSES/BSD-2-Clause.txt +0 -9
- data/doc/images/examples-analytics.rb.png +0 -0
- data/doc/images/examples-box_demo.rb.png +0 -0
- data/doc/images/examples-calendar_demo.rb.png +0 -0
- data/doc/images/examples-chart_demo.rb.png +0 -0
- data/doc/images/examples-custom_widget.rb.png +0 -0
- data/doc/images/examples-dashboard.rb.png +0 -0
- data/doc/images/examples-list_styles.rb.png +0 -0
- data/doc/images/examples-login_form.rb.png +0 -0
- data/doc/images/examples-map_demo.rb.png +0 -0
- data/doc/images/examples-mouse_events.rb.png +0 -0
- data/doc/images/examples-popup_demo.rb.gif +0 -0
- data/doc/images/examples-quickstart_lifecycle.rb.png +0 -0
- data/doc/images/examples-scroll_text.rb.png +0 -0
- data/doc/images/examples-scrollbar_demo.rb.png +0 -0
- data/doc/images/examples-stock_ticker.rb.png +0 -0
- data/doc/images/examples-system_monitor.rb.png +0 -0
- data/doc/images/examples-table_select.rb.png +0 -0
- data/examples/analytics.rb +0 -88
- data/examples/box_demo.rb +0 -71
- data/examples/calendar_demo.rb +0 -55
- data/examples/chart_demo.rb +0 -84
- data/examples/custom_widget.rb +0 -43
- data/examples/dashboard.rb +0 -72
- data/examples/list_styles.rb +0 -66
- data/examples/login_form.rb +0 -115
- data/examples/map_demo.rb +0 -58
- data/examples/mouse_events.rb +0 -95
- data/examples/popup_demo.rb +0 -105
- data/examples/quickstart_dsl.rb +0 -30
- data/examples/quickstart_lifecycle.rb +0 -40
- data/examples/readme_usage.rb +0 -21
- data/examples/scroll_text.rb +0 -74
- data/examples/scrollbar_demo.rb +0 -75
- data/examples/stock_ticker.rb +0 -93
- data/examples/system_monitor.rb +0 -94
- data/examples/table_select.rb +0 -70
- data/examples/test_analytics.rb +0 -65
- data/examples/test_box_demo.rb +0 -38
- data/examples/test_calendar_demo.rb +0 -66
- data/examples/test_dashboard.rb +0 -38
- data/examples/test_list_styles.rb +0 -61
- data/examples/test_login_form.rb +0 -63
- data/examples/test_map_demo.rb +0 -100
- data/examples/test_popup_demo.rb +0 -62
- data/examples/test_scroll_text.rb +0 -130
- data/examples/test_stock_ticker.rb +0 -39
- data/examples/test_system_monitor.rb +0 -40
- data/examples/test_table_select.rb +0 -37
- data/ext/ratatui_ruby/src/buffer.rs +0 -54
- data/lib/ratatui_ruby/dsl.rb +0 -64
|
@@ -4,37 +4,94 @@
|
|
|
4
4
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
5
5
|
|
|
6
6
|
module RatatuiRuby
|
|
7
|
-
#
|
|
7
|
+
# Displays a block of text.
|
|
8
8
|
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
|
|
9
|
+
# Raw strings are insufficient for UIs. They overflow constraints. They don't respect alignment (left, center, right).
|
|
10
|
+
#
|
|
11
|
+
# This widget creates a smart text container. It wraps content to fit the area. It aligns text as requested. It supports scrolling.
|
|
12
|
+
#
|
|
13
|
+
# Use it for everything from simple labels to complex, multi-paragraph documents.
|
|
14
|
+
#
|
|
15
|
+
# === Examples
|
|
16
|
+
#
|
|
17
|
+
# # Basic Text
|
|
18
|
+
# Paragraph.new(text: "Hello, World!")
|
|
19
|
+
#
|
|
20
|
+
# # Styled container with wrapping
|
|
21
|
+
# Paragraph.new(
|
|
22
|
+
# text: "This is a long line that will wrap automatically.",
|
|
23
|
+
# style: Style.new(fg: :green),
|
|
24
|
+
# wrap: true,
|
|
25
|
+
# block: Block.new(title: "Output", borders: [:all])
|
|
26
|
+
# )
|
|
27
|
+
#
|
|
28
|
+
# # Scrolling mechanism
|
|
29
|
+
# Paragraph.new(text: large_text, scroll: [scroll_y, 0])
|
|
30
|
+
class Paragraph < Data.define(:text, :style, :block, :wrap, :alignment, :scroll)
|
|
31
|
+
##
|
|
32
|
+
# :attr_reader: text
|
|
33
|
+
# The content to display.
|
|
34
|
+
|
|
35
|
+
##
|
|
36
|
+
# :attr_reader: style
|
|
37
|
+
# Base style for the text.
|
|
38
|
+
|
|
39
|
+
##
|
|
40
|
+
# :attr_reader: block
|
|
41
|
+
# Optional wrapping block.
|
|
42
|
+
|
|
43
|
+
##
|
|
44
|
+
# :attr_reader: wrap
|
|
45
|
+
# Whether to wrap text at the edge of the container (Boolean).
|
|
46
|
+
|
|
47
|
+
##
|
|
48
|
+
# :attr_reader: alignment
|
|
49
|
+
# Text alignment.
|
|
50
|
+
#
|
|
51
|
+
# <tt>:left</tt>, <tt>:center</tt>, or <tt>:right</tt>.
|
|
52
|
+
|
|
53
|
+
##
|
|
54
|
+
# :attr_reader: scroll
|
|
55
|
+
# Scroll offset [y, x].
|
|
56
|
+
|
|
14
57
|
# Creates a new Paragraph.
|
|
15
58
|
#
|
|
16
|
-
# [text]
|
|
17
|
-
# [style]
|
|
18
|
-
# [block]
|
|
19
|
-
# [wrap]
|
|
20
|
-
# [
|
|
21
|
-
# [scroll]
|
|
22
|
-
def initialize(text:, style: Style.default, block: nil, wrap: false,
|
|
23
|
-
super
|
|
59
|
+
# [text] String or Text::Line array.
|
|
60
|
+
# [style] Style object.
|
|
61
|
+
# [block] Block object.
|
|
62
|
+
# [wrap] Boolean (default: false).
|
|
63
|
+
# [alignment] Symbol (default: <tt>:left</tt>).
|
|
64
|
+
# [scroll] Array of [y, x] integers (duck-typed via +to_int+).
|
|
65
|
+
def initialize(text:, style: Style.default, block: nil, wrap: false, alignment: :left, scroll: [0, 0])
|
|
66
|
+
super(
|
|
67
|
+
text:,
|
|
68
|
+
style:,
|
|
69
|
+
block:,
|
|
70
|
+
wrap:,
|
|
71
|
+
alignment:,
|
|
72
|
+
scroll: [Integer(scroll[0]), Integer(scroll[1])]
|
|
73
|
+
)
|
|
24
74
|
end
|
|
25
75
|
|
|
26
|
-
#
|
|
27
|
-
|
|
28
|
-
# [style] the style to apply.
|
|
29
|
-
# [fg] legacy foreground color.
|
|
30
|
-
# [bg] legacy background color.
|
|
31
|
-
# [block] the block to wrap the paragraph.
|
|
32
|
-
# [wrap] whether to wrap text at width.
|
|
33
|
-
# [align] alignment (:left, :center, :right).
|
|
34
|
-
# [scroll] scroll offset as (y, x) array (default: [0, 0]).
|
|
35
|
-
def self.new(text:, style: nil, fg: nil, bg: nil, block: nil, wrap: false, align: :left, scroll: [0, 0])
|
|
76
|
+
# Legacy constructor support.
|
|
77
|
+
def self.new(text:, style: nil, fg: nil, bg: nil, block: nil, wrap: false, alignment: :left, scroll: [0, 0])
|
|
36
78
|
style ||= Style.new(fg:, bg:)
|
|
37
|
-
|
|
79
|
+
coerced_scroll = [Integer(scroll[0]), Integer(scroll[1])]
|
|
80
|
+
super(text:, style:, block:, wrap:, alignment:, scroll: coerced_scroll)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Returns the number of lines the paragraph would take up if rendered with the given width.
|
|
84
|
+
#
|
|
85
|
+
# [width] Integer (max width).
|
|
86
|
+
def line_count(width)
|
|
87
|
+
RatatuiRuby.warn_experimental_feature("Paragraph#line_count")
|
|
88
|
+
RatatuiRuby._paragraph_line_count(self, Integer(width))
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Returns the minimum width needed to not wrap any text.
|
|
92
|
+
def line_width
|
|
93
|
+
RatatuiRuby.warn_experimental_feature("Paragraph#line_width")
|
|
94
|
+
RatatuiRuby._paragraph_line_width(self)
|
|
38
95
|
end
|
|
39
96
|
end
|
|
40
97
|
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-FileCopyrightText: 2025 Kerrick Long <me@kerricklong.com>
|
|
4
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
5
|
+
|
|
6
|
+
module RatatuiRuby
|
|
7
|
+
# Displays the Ratatui logo.
|
|
8
|
+
#
|
|
9
|
+
# Branding is important for identity. Users need to recognize the tools they use.
|
|
10
|
+
#
|
|
11
|
+
# This widget renders the official Ratatui logo.
|
|
12
|
+
#
|
|
13
|
+
# Use it for splash screens, about sections, or terminal dashboards.
|
|
14
|
+
#
|
|
15
|
+
# {rdoc-image:/doc/images/widget_ratatui_logo_demo.png}[link:/examples/widget_ratatui_logo_demo/app_rb.html]
|
|
16
|
+
#
|
|
17
|
+
# === Example
|
|
18
|
+
#
|
|
19
|
+
# Run the interactive demo from the terminal:
|
|
20
|
+
#
|
|
21
|
+
# ruby examples/widget_ratatui_logo_demo/app.rb
|
|
22
|
+
class RatatuiLogo < Data.define
|
|
23
|
+
##
|
|
24
|
+
# :method: new
|
|
25
|
+
# :call-seq: new -> RatatuiLogo
|
|
26
|
+
#
|
|
27
|
+
# Creates a new RatatuiLogo.
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-FileCopyrightText: 2025 Kerrick Long <me@kerricklong.com>
|
|
4
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
5
|
+
|
|
6
|
+
module RatatuiRuby
|
|
7
|
+
# Displays the Ratatui mascot.
|
|
8
|
+
#
|
|
9
|
+
# Interfaces without personality feel clinical and dry. Users appreciate a friendly face in their terminal.
|
|
10
|
+
#
|
|
11
|
+
# This widget renders the Ratatui mascot (a mouse).
|
|
12
|
+
#
|
|
13
|
+
# Use it to add charm to your application, greet users on startup, or as a decorative element in sidebars.
|
|
14
|
+
#
|
|
15
|
+
# {rdoc-image:/doc/images/widget_ratatui_mascot_demo.png}[link:/examples/widget_ratatui_mascot_demo/app_rb.html]
|
|
16
|
+
#
|
|
17
|
+
# === Example
|
|
18
|
+
#
|
|
19
|
+
# Run the interactive demo from the terminal:
|
|
20
|
+
#
|
|
21
|
+
# ruby examples/widget_ratatui_mascot_demo/app.rb
|
|
22
|
+
class RatatuiMascot < Data.define(:block)
|
|
23
|
+
##
|
|
24
|
+
# :method: new
|
|
25
|
+
# :call-seq: new(block: nil) -> RatatuiMascot
|
|
26
|
+
#
|
|
27
|
+
# Creates a new RatatuiMascot.
|
|
28
|
+
#
|
|
29
|
+
# @param block [Block, nil] A block to wrap the widget in.
|
|
30
|
+
def initialize(block: nil)
|
|
31
|
+
super
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -4,21 +4,70 @@
|
|
|
4
4
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
5
5
|
|
|
6
6
|
module RatatuiRuby
|
|
7
|
-
#
|
|
7
|
+
# Defines a rectangular area in the terminal grid.
|
|
8
8
|
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
9
|
+
# Geometry management involves passing groups of four integers (`x, y, width, height`) repeatedly.
|
|
10
|
+
# This is verbose and prone to parameter mismatch errors.
|
|
11
|
+
#
|
|
12
|
+
# This class encapsulates the geometry. It provides a standard primitive for passing area definitions
|
|
13
|
+
# between layout engines and rendering functions.
|
|
14
|
+
#
|
|
15
|
+
# Use it when manual positioning is required or when querying layout results.
|
|
16
|
+
#
|
|
17
|
+
# === Examples
|
|
18
|
+
#
|
|
19
|
+
# area = Rect.new(x: 0, y: 0, width: 80, height: 24)
|
|
20
|
+
# puts area.width # => 80
|
|
13
21
|
class Rect < Data.define(:x, :y, :width, :height)
|
|
22
|
+
##
|
|
23
|
+
# :attr_reader: x
|
|
24
|
+
# X coordinate (column) of the top-left corner (Integer, coerced via +to_int+ or +to_i+).
|
|
25
|
+
|
|
26
|
+
##
|
|
27
|
+
# :attr_reader: y
|
|
28
|
+
# Y coordinate (row) of the top-left corner (Integer, coerced via +to_int+ or +to_i+).
|
|
29
|
+
|
|
30
|
+
##
|
|
31
|
+
# :attr_reader: width
|
|
32
|
+
# Width in characters (Integer, coerced via +to_int+ or +to_i+).
|
|
33
|
+
|
|
34
|
+
##
|
|
35
|
+
# :attr_reader: height
|
|
36
|
+
# Height in characters (Integer, coerced via +to_int+ or +to_i+).
|
|
37
|
+
|
|
14
38
|
# Creates a new Rect.
|
|
15
39
|
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
# [
|
|
19
|
-
# [
|
|
40
|
+
# All parameters accept any object responding to +to_int+ or +to_i+ (duck-typed).
|
|
41
|
+
#
|
|
42
|
+
# [x] Column index (Numeric).
|
|
43
|
+
# [y] Row index (Numeric).
|
|
44
|
+
# [width] Width in columns (Numeric).
|
|
45
|
+
# [height] Height in rows (Numeric).
|
|
20
46
|
def initialize(x: 0, y: 0, width: 0, height: 0)
|
|
21
|
-
super
|
|
47
|
+
super(
|
|
48
|
+
x: Integer(x),
|
|
49
|
+
y: Integer(y),
|
|
50
|
+
width: Integer(width),
|
|
51
|
+
height: Integer(height)
|
|
52
|
+
)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Tests whether a point is inside this rectangle.
|
|
56
|
+
#
|
|
57
|
+
# Essential for hit testing mouse clicks against layout regions.
|
|
58
|
+
#
|
|
59
|
+
# area = Rect.new(x: 10, y: 5, width: 20, height: 10)
|
|
60
|
+
# area.contains?(15, 8) # => true
|
|
61
|
+
# area.contains?(5, 8) # => false
|
|
62
|
+
#
|
|
63
|
+
# [px]
|
|
64
|
+
# X coordinate to test (column).
|
|
65
|
+
# [py]
|
|
66
|
+
# Y coordinate to test (row).
|
|
67
|
+
#
|
|
68
|
+
# Returns true if the point (px, py) is within the rectangle bounds.
|
|
69
|
+
def contains?(px, py)
|
|
70
|
+
px >= x && px < x + width && py >= y && py < y + height
|
|
22
71
|
end
|
|
23
72
|
end
|
|
24
73
|
end
|
|
@@ -4,30 +4,138 @@
|
|
|
4
4
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
5
5
|
|
|
6
6
|
module RatatuiRuby
|
|
7
|
-
|
|
8
|
-
# Visual scroll indicator.
|
|
7
|
+
# Visualizes the scroll state of a viewport.
|
|
9
8
|
#
|
|
10
|
-
#
|
|
11
|
-
# It displays a track and a thumb to indicate the current scroll position
|
|
12
|
-
# relative to the total content length.
|
|
9
|
+
# Content overflows. Users get lost in long lists without landmarks. They need to know where they are and how much is left.
|
|
13
10
|
#
|
|
14
|
-
#
|
|
11
|
+
# This widget maps your context. It draws a track and a thumb, representing your current position relative to the total length.
|
|
15
12
|
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
# [
|
|
19
|
-
#
|
|
20
|
-
#
|
|
21
|
-
|
|
13
|
+
# Overlay it on top of lists, paragraphs, or tables to provide spatial awareness.
|
|
14
|
+
#
|
|
15
|
+
# {rdoc-image:/doc/images/widget_scrollbar_demo.png}[link:/examples/widget_scrollbar_demo/app_rb.html]
|
|
16
|
+
#
|
|
17
|
+
# === Example
|
|
18
|
+
#
|
|
19
|
+
# Run the interactive demo from the terminal:
|
|
20
|
+
#
|
|
21
|
+
# ruby examples/widget_scrollbar_demo/app.rb
|
|
22
|
+
class Scrollbar < Data.define(
|
|
23
|
+
:content_length,
|
|
24
|
+
:position,
|
|
25
|
+
:orientation,
|
|
26
|
+
:thumb_symbol,
|
|
27
|
+
:thumb_style,
|
|
28
|
+
:track_symbol,
|
|
29
|
+
:track_style,
|
|
30
|
+
:begin_symbol,
|
|
31
|
+
:begin_style,
|
|
32
|
+
:end_symbol,
|
|
33
|
+
:end_style,
|
|
34
|
+
:style,
|
|
35
|
+
:block
|
|
36
|
+
)
|
|
37
|
+
##
|
|
38
|
+
# :attr_reader: content_length
|
|
39
|
+
# Total items or lines in the content.
|
|
40
|
+
|
|
41
|
+
##
|
|
42
|
+
# :attr_reader: position
|
|
43
|
+
# Current scroll offset (index).
|
|
44
|
+
#
|
|
45
|
+
# Maps to Ratatui's <tt>ScrollbarState::get_position()</tt> (new in 0.30.0).
|
|
46
|
+
|
|
47
|
+
##
|
|
48
|
+
# :attr_reader: orientation
|
|
49
|
+
# Direction of the scrollbar.
|
|
50
|
+
#
|
|
51
|
+
# <tt>:vertical</tt> (default, alias for <tt>:vertical_right</tt>), <tt>:horizontal</tt> (alias for <tt>:horizontal_bottom</tt>),
|
|
52
|
+
# <tt>:vertical_left</tt>, <tt>:vertical_right</tt>, <tt>:horizontal_top</tt>, or <tt>:horizontal_bottom</tt>.
|
|
53
|
+
|
|
54
|
+
##
|
|
55
|
+
# :attr_reader: thumb_symbol
|
|
56
|
+
# Symbol used to represent the current position indicator.
|
|
57
|
+
|
|
58
|
+
##
|
|
59
|
+
# :attr_reader: thumb_style
|
|
60
|
+
# Style of the position indicator (thumb).
|
|
61
|
+
|
|
62
|
+
##
|
|
63
|
+
# :attr_reader: track_symbol
|
|
64
|
+
# Symbol used to represent the empty space of the scrollbar.
|
|
65
|
+
|
|
66
|
+
##
|
|
67
|
+
# :attr_reader: track_style
|
|
68
|
+
# Style of the filled track area.
|
|
69
|
+
|
|
70
|
+
##
|
|
71
|
+
# :attr_reader: begin_symbol
|
|
72
|
+
# Symbol rendered at the start of the track (e.g., arrow).
|
|
73
|
+
|
|
74
|
+
##
|
|
75
|
+
# :attr_reader: begin_style
|
|
76
|
+
# Style of the start symbol.
|
|
77
|
+
|
|
78
|
+
##
|
|
79
|
+
# :attr_reader: end_symbol
|
|
80
|
+
# Symbol rendered at the end of the track (e.g., arrow).
|
|
81
|
+
|
|
82
|
+
##
|
|
83
|
+
# :attr_reader: end_style
|
|
84
|
+
# Style of the end symbol.
|
|
85
|
+
|
|
86
|
+
##
|
|
87
|
+
# :attr_reader: style
|
|
88
|
+
# Base style applied to the entire widget.
|
|
89
|
+
|
|
90
|
+
##
|
|
91
|
+
# :attr_reader: block
|
|
92
|
+
# Optional wrapping block.
|
|
93
|
+
|
|
22
94
|
# Creates a new Scrollbar.
|
|
23
95
|
#
|
|
24
|
-
# [
|
|
25
|
-
# [
|
|
26
|
-
# [
|
|
27
|
-
# [
|
|
28
|
-
# [
|
|
29
|
-
|
|
30
|
-
|
|
96
|
+
# [content_length] Integer.
|
|
97
|
+
# [position] Integer.
|
|
98
|
+
# [orientation] Symbol (default: <tt>:vertical</tt>).
|
|
99
|
+
# [thumb_symbol] String (default: <tt>"█"</tt>).
|
|
100
|
+
# [thumb_style] Style (optional).
|
|
101
|
+
# [track_symbol] String (optional).
|
|
102
|
+
# [track_style] Style (optional).
|
|
103
|
+
# [begin_symbol] String (optional).
|
|
104
|
+
# [begin_style] Style (optional).
|
|
105
|
+
# [end_symbol] String (optional).
|
|
106
|
+
# [end_style] Style (optional).
|
|
107
|
+
# [style] Style (optional).
|
|
108
|
+
# [block] Block (optional).
|
|
109
|
+
def initialize(
|
|
110
|
+
content_length:,
|
|
111
|
+
position:,
|
|
112
|
+
orientation: :vertical,
|
|
113
|
+
thumb_symbol: "█",
|
|
114
|
+
thumb_style: nil,
|
|
115
|
+
track_symbol: nil,
|
|
116
|
+
track_style: nil,
|
|
117
|
+
begin_symbol: nil,
|
|
118
|
+
begin_style: nil,
|
|
119
|
+
end_symbol: nil,
|
|
120
|
+
end_style: nil,
|
|
121
|
+
style: nil,
|
|
122
|
+
block: nil
|
|
123
|
+
)
|
|
124
|
+
super(
|
|
125
|
+
content_length: Integer(content_length),
|
|
126
|
+
position: Integer(position),
|
|
127
|
+
orientation:,
|
|
128
|
+
thumb_symbol:,
|
|
129
|
+
thumb_style:,
|
|
130
|
+
track_symbol:,
|
|
131
|
+
track_style:,
|
|
132
|
+
begin_symbol:,
|
|
133
|
+
begin_style:,
|
|
134
|
+
end_symbol:,
|
|
135
|
+
end_style:,
|
|
136
|
+
style:,
|
|
137
|
+
block:
|
|
138
|
+
)
|
|
31
139
|
end
|
|
32
140
|
end
|
|
33
141
|
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-FileCopyrightText: 2025 Kerrick Long <me@kerricklong.com>
|
|
4
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
5
|
+
|
|
6
|
+
module RatatuiRuby
|
|
7
|
+
module Shape
|
|
8
|
+
# A text label on a canvas.
|
|
9
|
+
#
|
|
10
|
+
# Labels render text at specific coordinates in canvas space.
|
|
11
|
+
# Unlike shapes, labels are always rendered on top of all other canvas elements.
|
|
12
|
+
#
|
|
13
|
+
# [x] The x-coordinate in canvas space (Numeric).
|
|
14
|
+
# [y] The y-coordinate in canvas space (Numeric).
|
|
15
|
+
# [text] The text content (String or Text::Line).
|
|
16
|
+
# [style] Optional style for the text.
|
|
17
|
+
#
|
|
18
|
+
# === Examples
|
|
19
|
+
#
|
|
20
|
+
# # Simple label
|
|
21
|
+
# Shape::Label.new(x: 0, y: 0, text: "Origin")
|
|
22
|
+
#
|
|
23
|
+
# # Styled label
|
|
24
|
+
# Shape::Label.new(
|
|
25
|
+
# x: -122.4, y: 37.8,
|
|
26
|
+
# text: "San Francisco",
|
|
27
|
+
# style: Style.new(fg: :cyan, add_modifier: :bold)
|
|
28
|
+
# )
|
|
29
|
+
#
|
|
30
|
+
# # Label with Text::Line for rich formatting
|
|
31
|
+
# Shape::Label.new(
|
|
32
|
+
# x: 0.0, y: 0.0,
|
|
33
|
+
# text: Text::Line.new(spans: [
|
|
34
|
+
# Text::Span.new(content: "Hello ", style: Style.new(fg: :red)),
|
|
35
|
+
# Text::Span.new(content: "World", style: Style.new(fg: :blue))
|
|
36
|
+
# ])
|
|
37
|
+
# )
|
|
38
|
+
class Label < Data.define(:x, :y, :text, :style)
|
|
39
|
+
##
|
|
40
|
+
# :attr_reader: x
|
|
41
|
+
# X coordinate in canvas space (Float, duck-typed via +to_f+).
|
|
42
|
+
|
|
43
|
+
##
|
|
44
|
+
# :attr_reader: y
|
|
45
|
+
# Y coordinate in canvas space (Float, duck-typed via +to_f+).
|
|
46
|
+
|
|
47
|
+
##
|
|
48
|
+
# :attr_reader: text
|
|
49
|
+
# Text content (String or Text::Line).
|
|
50
|
+
|
|
51
|
+
##
|
|
52
|
+
# :attr_reader: style
|
|
53
|
+
# Optional style for the text.
|
|
54
|
+
|
|
55
|
+
# Creates a new Label.
|
|
56
|
+
#
|
|
57
|
+
# [x] X coordinate (Numeric).
|
|
58
|
+
# [y] Y coordinate (Numeric).
|
|
59
|
+
# [text] Text content (String or Text::Line).
|
|
60
|
+
# [style] Style (optional).
|
|
61
|
+
def initialize(x:, y:, text:, style: nil)
|
|
62
|
+
super(x: Float(x), y: Float(y), text:, style:)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -4,21 +4,129 @@
|
|
|
4
4
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
5
5
|
|
|
6
6
|
module RatatuiRuby
|
|
7
|
-
#
|
|
7
|
+
# Displays high-density data in a compact row.
|
|
8
8
|
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
|
|
9
|
+
# Users need context. A single value ("90% CPU") tells you current status, but not the trend.
|
|
10
|
+
# Full charts take up too much room.
|
|
11
|
+
#
|
|
12
|
+
# This widget solves the density problem. It condenses history into a single line of variable-height blocks.
|
|
13
|
+
#
|
|
14
|
+
# Use it in dashboards, headers, or list items to providing trending data at a glance.
|
|
15
|
+
#
|
|
16
|
+
# {rdoc-image:/doc/images/widget_sparkline_demo.png}[link:/examples/widget_sparkline_demo/app_rb.html]
|
|
17
|
+
#
|
|
18
|
+
# === Example
|
|
19
|
+
#
|
|
20
|
+
# Run the interactive demo from the terminal:
|
|
21
|
+
#
|
|
22
|
+
# ruby examples/widget_sparkline_demo/app.rb
|
|
23
|
+
class Sparkline < Data.define(:data, :max, :style, :block, :direction, :absent_value_symbol, :absent_value_style, :bar_set)
|
|
24
|
+
##
|
|
25
|
+
# :attr_reader: data
|
|
26
|
+
# Array of integer values to plot.
|
|
27
|
+
|
|
28
|
+
##
|
|
29
|
+
# :attr_reader: max
|
|
30
|
+
# Maximum value for scaling (optional).
|
|
31
|
+
#
|
|
32
|
+
# If nil, derived from data max.
|
|
33
|
+
|
|
34
|
+
##
|
|
35
|
+
# :attr_reader: style
|
|
36
|
+
# Style for the bars.
|
|
37
|
+
|
|
38
|
+
##
|
|
39
|
+
# :attr_reader: block
|
|
40
|
+
# Optional wrapping block.
|
|
41
|
+
|
|
42
|
+
##
|
|
43
|
+
# :attr_reader: direction
|
|
44
|
+
# Direction to render data.
|
|
45
|
+
#
|
|
46
|
+
# Accepts +:left_to_right+ (default) or +:right_to_left+.
|
|
47
|
+
# Use +:right_to_left+ when new data should appear on the left.
|
|
48
|
+
|
|
49
|
+
##
|
|
50
|
+
# :attr_reader: absent_value_symbol
|
|
51
|
+
# Character to render for absent (nil) values (optional).
|
|
52
|
+
#
|
|
53
|
+
# If nil, absent values are rendered with a space.
|
|
54
|
+
|
|
55
|
+
##
|
|
56
|
+
# :attr_reader: absent_value_style
|
|
57
|
+
# Style for absent (nil) values (optional).
|
|
58
|
+
|
|
59
|
+
##
|
|
60
|
+
# :attr_reader: bar_set
|
|
61
|
+
# Custom characters for the bars (optional).
|
|
62
|
+
#
|
|
63
|
+
# A Hash with keys defining the characters for the bars.
|
|
64
|
+
# Keys: <tt>:empty</tt>, <tt>:one_eighth</tt>, <tt>:one_quarter</tt>, <tt>:three_eighths</tt>, <tt>:half</tt>, <tt>:five_eighths</tt>, <tt>:three_quarters</tt>, <tt>:seven_eighths</tt>, <tt>:full</tt>.
|
|
65
|
+
#
|
|
66
|
+
# You can also use integers (0-8) as keys, where 0 is empty, 4 is half, and 8 is full.
|
|
67
|
+
#
|
|
68
|
+
# Alternatively, you can pass an Array of 9 strings, where index 0 is empty and index 8 is full.
|
|
69
|
+
#
|
|
70
|
+
# === Examples
|
|
71
|
+
#
|
|
72
|
+
# bar_set: {
|
|
73
|
+
# empty: " ",
|
|
74
|
+
# one_eighth: " ",
|
|
75
|
+
# one_quarter: "▂",
|
|
76
|
+
# three_eighths: "▃",
|
|
77
|
+
# half: "▄",
|
|
78
|
+
# five_eighths: "▅",
|
|
79
|
+
# three_quarters: "▆",
|
|
80
|
+
# seven_eighths: "▇",
|
|
81
|
+
# full: "█"
|
|
82
|
+
# }
|
|
83
|
+
#
|
|
84
|
+
# # Numeric keys (0-8)
|
|
85
|
+
# bar_set: {
|
|
86
|
+
# 0 => " ", 1 => " ", 2 => "▂", 3 => "▃", 4 => "▄", 5 => "▅", 6 => "▆", 7 => "▇", 8 => "█"
|
|
87
|
+
# }
|
|
88
|
+
#
|
|
89
|
+
# # Array (9 items)
|
|
90
|
+
# bar_set: [" ", " ", "▂", "▃", "▄", "▅", "▆", "▇", "█"]
|
|
91
|
+
|
|
92
|
+
BAR_KEYS = %i[empty one_eighth one_quarter three_eighths half five_eighths three_quarters seven_eighths full].freeze
|
|
93
|
+
|
|
14
94
|
# Creates a new Sparkline widget.
|
|
15
95
|
#
|
|
16
|
-
# [data] Array of Integers.
|
|
17
|
-
# [max]
|
|
18
|
-
# [style]
|
|
19
|
-
# [block]
|
|
20
|
-
|
|
21
|
-
|
|
96
|
+
# [data] Array of Integers or nil. nil marks an absent value (distinct from 0).
|
|
97
|
+
# [max] Max value (optional).
|
|
98
|
+
# [style] Style (optional).
|
|
99
|
+
# [block] Block (optional).
|
|
100
|
+
# [direction] +:left_to_right+ or +:right_to_left+ (default: +:left_to_right+).
|
|
101
|
+
# [absent_value_symbol] Character for absent (nil) values (optional).
|
|
102
|
+
# [absent_value_style] Style for absent (nil) values (optional).
|
|
103
|
+
# [bar_set] Hash or Array of custom characters (optional).
|
|
104
|
+
def initialize(data:, max: nil, style: nil, block: nil, direction: :left_to_right, absent_value_symbol: nil, absent_value_style: nil, bar_set: nil)
|
|
105
|
+
if bar_set
|
|
106
|
+
if bar_set.is_a?(Array) && bar_set.size == 9
|
|
107
|
+
# Convert Array to Hash using BAR_KEYS order
|
|
108
|
+
bar_set = BAR_KEYS.zip(bar_set).to_h
|
|
109
|
+
else
|
|
110
|
+
bar_set = bar_set.dup
|
|
111
|
+
# Normalize numeric keys (0-8) to symbolic keys
|
|
112
|
+
BAR_KEYS.each_with_index do |key, i|
|
|
113
|
+
if (val = bar_set.delete(i) || bar_set.delete(i.to_s))
|
|
114
|
+
bar_set[key] = val
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
coerced_data = data.map { |v| v.nil? ? nil : Integer(v) }
|
|
120
|
+
super(
|
|
121
|
+
data: coerced_data,
|
|
122
|
+
max: max.nil? ? nil : Integer(max),
|
|
123
|
+
style:,
|
|
124
|
+
block:,
|
|
125
|
+
direction:,
|
|
126
|
+
absent_value_symbol:,
|
|
127
|
+
absent_value_style:,
|
|
128
|
+
bar_set:
|
|
129
|
+
)
|
|
22
130
|
end
|
|
23
131
|
end
|
|
24
132
|
end
|