返回 CodeWhale
mod.rs
1 //! Every component with fixture data, for `examples/gallery.rs` and the
2 //! snapshot tests. The fixtures double as usage examples: the mode and
3 //! status-line pickers here are the engine's `/mode` and `/statusline`
4 //! pickers rebuilt from kit parts.
5 //!
6 //! One file per area, each exposing `pub(crate) fn entries() -> Vec<Entry>`.
7 //! [`entries`] concatenates them, so a package edits only its own file:
8 //!
9 //! | File | Holds |
10 //! |---|---|
11 //! | `hints`, `status`, `surface`, `picker`, `toast`, `icons`, `spinner`, `whale` | the components that exist |
12 //! | `input` | package Input: text input and form |
13 //! | `lists` | package Lists: list, fuzzy picker, empty state |
14 //! | `chrome` | package Chrome: heading, tabs, toggle, segmented, keymap |
15 //! | `display` | package Display: receipt, diff, tree, progress |
16 //! | `approval` | package Approval: approval card |
17 //! | `motion` | package Motion |
18 //! | `workspace` | messages, composer, tool and agent cards, fleets |
19 //! | `settings` | values, source, locks, apply timing and defaults |
20 //! | `atmosphere` | one workspace scene in each of the five ombre palettes |
21
22 use crossterm::event::KeyCode;
23 use ratatui::{buffer::Buffer, layout::Rect};
24
25 use crate::{
26 Role, Theme,
27 keys::{Platform, pair_label},
28 };
29
30 mod approval;
31 mod artifacts;
32 mod atmosphere;
33 mod attention;
34 mod chart;
35 mod chrome;
36 mod craft;
37 mod display;
38 mod habitat;
39 mod hints;
40 mod icons;
41 mod input;
42 mod lists;
43 mod motion;
44 mod native_chrome;
45 mod native_views;
46 mod ocean;
47 mod pending_input;
48 mod picker;
49 mod posture;
50 mod primitives;
51 mod scenes;
52 mod settings;
53 pub mod showcase;
54 mod spinner;
55 mod status;
56 mod surface;
57 mod toast;
58 mod transcript;
59 mod tui_palettes;
60 mod verification;
61 mod whale;
62 mod workbar;
63 mod workbench;
64 mod workspace;
65
66 /// One gallery entry: a name, the size it is drawn at by default, and a
67 /// function that paints it. `draw` must paint inside the `Rect` it is given
68 /// (the gallery draws it at other widths too) and use only the `Theme`.
69 pub struct Entry {
70 pub name: &'static str,
71 pub width: u16,
72 pub height: u16,
73 pub draw: fn(Rect, &mut Buffer, &Theme),
74 }
75
76 /// `↑↓`, or `Up/Down` where marks are ASCII-safe: one spelling for the
77 /// gallery's key hints.
78 fn arrows(theme: &Theme) -> String {
79 pair_label(KeyCode::Up, KeyCode::Down, Platform::current(theme.ascii()))
80 }
81
82 /// Every entry, in gallery order.
83 #[must_use]
84 pub fn entries() -> Vec<Entry> {
85 [
86 native_views::entries(),
87 native_chrome::entries(),
88 posture::entries(),
89 workbar::entries(),
90 tui_palettes::entries(),
91 scenes::entries(),
92 showcase::entries(),
93 workbench::entries(),
94 attention::entries(),
95 artifacts::entries(),
96 habitat::entries(),
97 hints::entries(),
98 status::entries(),
99 surface::entries(),
100 picker::entries(),
101 toast::entries(),
102 icons::entries(),
103 spinner::entries(),
104 verification::entries(),
105 whale::entries(),
106 input::entries(),
107 lists::entries(),
108 chrome::entries(),
109 display::entries(),
110 chart::entries(),
111 approval::entries(),
112 motion::entries(),
113 workspace::entries(),
114 settings::entries(),
115 atmosphere::entries(),
116 ocean::entries(),
117 pending_input::entries(),
118 primitives::entries(),
119 craft::entries(),
120 transcript::entries(),
121 ]
122 .into_iter()
123 .flatten()
124 .collect()
125 }
126
127 /// Resolve the source palette used by a native preview. Named presets keep
128 /// their own background; terminal fallbacks still follow the input capabilities.
129 #[must_use]
130 pub fn theme_for(entry: &Entry, theme: &Theme) -> Theme {
131 if let Some(palette) = tui_palettes::palette_for_name(entry.name) {
132 return tui_palettes::theme_for(palette, theme);
133 }
134 if entry.name.starts_with("view-")
135 || entry.name == "instrument-surface"
136 || entry.name == "session-list"
137 || entry.name.starts_with("native-")
138 || entry.name.starts_with("workbar-")
139 || entry.name.starts_with("workflow-")
140 || entry.name.starts_with("posture-")
141 || entry.name.starts_with("metrics-")
142 || entry.name.starts_with("showcase-")
143 {
144 return theme.tui();
145 }
146 *theme
147 }
148
149 /// Render one entry for one theme at its own size.
150 #[must_use]
151 pub fn render(entry: &Entry, theme: &Theme) -> Buffer {
152 render_at(entry, theme, entry.width, entry.height)
153 }
154
155 /// Render one entry for one theme at another size, on the theme's
156 /// `Background` as the gallery paints it.
157 #[must_use]
158 pub fn render_at(entry: &Entry, theme: &Theme, width: u16, height: u16) -> Buffer {
159 let theme = theme_for(entry, theme);
160 crate::testing::render(width, height, |area, buf| {
161 buf.set_style(area, theme.bg(Role::Background));
162 (entry.draw)(area, buf, &theme);
163 })
164 }
165
165 lines RUST