返回 CodeWhale
welcome.rs
根目录 / crates / tui / src / tui / onboarding / welcome.rs
1 //! Calm welcome and ready screens for first run.
2 //!
3 //! The welcome is unnumbered and asks nothing: one headline, one supporting
4 //! sentence, one primary action, one exit. The ready screen closes the flow
5 //! by handing the user the composer pre-seeded with a first task — Enter
6 //! opens the product, never another educational surface.
7
8 use ratatui::style::{Modifier, Style};
9 use ratatui::text::{Line, Span};
10
11 use crate::tui::app::App;
12 use codewhale_localization::MessageId;
13 use codewhale_palette as palette;
14
15 pub fn lines(app: &App, width: usize) -> Vec<Line<'static>> {
16 let mut out = Vec::new();
17 headline(&mut out, app, MessageId::OnboardWelcomeTitle, width);
18 accent_rule(&mut out, app, width);
19 body(&mut out, app, MessageId::OnboardWelcomeLead, width);
20 out
21 }
22
23 pub fn ready_lines(app: &App, width: usize) -> Vec<Line<'static>> {
24 let mut out = Vec::new();
25 headline(&mut out, app, MessageId::OnboardReadyTitle, width);
26 accent_rule(&mut out, app, width);
27 body(&mut out, app, MessageId::OnboardReadyLead, width);
28 let notice = if app.onboarding_explore_offline {
29 Some(app.tr(MessageId::OnboardOfflineNotice).into_owned())
30 } else {
31 app.setup_notice().map(|notice| notice.text.clone())
32 };
33 if let Some(message) = notice {
34 let warn_fg = super::theme_or(app.ui_theme.warning, palette::STATUS_WARNING);
35 out.push(Line::from(""));
36 for segment in super::wrap_words(&message, width) {
37 out.push(Line::from(Span::styled(
38 segment,
39 Style::default().fg(warn_fg),
40 )));
41 }
42 }
43 out
44 }
45
46 fn accent_rule(out: &mut Vec<Line<'static>>, app: &App, width: usize) {
47 let rule_width = 6.min(width);
48 if rule_width == 0 {
49 out.push(Line::from(""));
50 return;
51 }
52 let ch = if crate::tui::color_compat::ascii_safe_enabled() {
53 "-"
54 } else {
55 "─"
56 };
57 let color = super::theme_or(app.ui_theme.border, palette::BORDER_COLOR);
58 out.push(Line::from(Span::styled(
59 ch.repeat(rule_width),
60 Style::default().fg(color),
61 )));
62 }
63
64 fn headline(out: &mut Vec<Line<'static>>, app: &App, id: MessageId, width: usize) {
65 let fg = super::theme_or(app.ui_theme.accent_action, palette::WHALE_HUMAN);
66 for segment in super::wrap_words(&app.tr(id), width) {
67 out.push(Line::from(Span::styled(
68 segment,
69 Style::default().fg(fg).add_modifier(Modifier::BOLD),
70 )));
71 }
72 }
73
74 fn body(out: &mut Vec<Line<'static>>, app: &App, id: MessageId, width: usize) {
75 let fg = super::theme_or(app.ui_theme.text_body, palette::TEXT_PRIMARY);
76 for segment in super::wrap_words(&app.tr(id), width) {
77 out.push(Line::from(Span::styled(segment, Style::default().fg(fg))));
78 }
79 }
80
81 #[cfg(test)]
82 mod tests {
83 use super::*;
84 use crate::tui::app::TuiOptions;
85 use codewhale_localization::Locale;
86 use codewhale_runtime::config::Config;
87 use std::path::PathBuf;
88
89 fn test_app_with_locale(locale: Locale) -> App {
90 let options = TuiOptions {
91 ..crate::tui::test_support::test_tui_options(PathBuf::from("."))
92 };
93 let mut app = App::new(options, &Config::default());
94 app.ui_locale = locale;
95 app
96 }
97
98 fn body(_app: &App, lines: Vec<Line<'static>>) -> String {
99 lines
100 .into_iter()
101 .flat_map(|line| {
102 line.spans
103 .into_iter()
104 .map(|span| span.content.to_string())
105 .collect::<Vec<_>>()
106 })
107 .collect::<Vec<_>>()
108 .join("\n")
109 }
110
111 #[test]
112 fn ready_screen_reads_offline_truth_from_typed_state() {
113 let mut app = test_app_with_locale(Locale::En);
114 app.onboarding_explore_offline = true;
115 app.show_onboarding_notice(
116 "Workspace trust was not changed.",
117 crate::tui::app::StatusToastLevel::Warning,
118 );
119 let text = body(&app, ready_lines(&app, 70));
120 assert!(
121 text.contains(app.tr(MessageId::OnboardOfflineNotice).as_ref()),
122 "{text}"
123 );
124 assert!(!text.contains("Workspace trust was not changed."), "{text}");
125 }
126 }
127
128 #[cfg(test)]
129 mod narrow_locale_tests {
130 use super::*;
131 use crate::tui::app::TuiOptions;
132 use codewhale_localization::{Locale, MessageId, tr};
133 use codewhale_runtime::config::Config;
134 use std::path::PathBuf;
135 use unicode_width::UnicodeWidthStr;
136
137 fn probe_app() -> App {
138 let options = TuiOptions {
139 model: "test-model".to_string(),
140 ..crate::tui::test_support::test_tui_options(PathBuf::from("workspace-fixture"))
141 };
142 App::new(options, &Config::default())
143 }
144
145 fn flatten(lines: Vec<Line<'static>>) -> Vec<String> {
146 lines
147 .iter()
148 .map(|line| {
149 line.spans
150 .iter()
151 .map(|span| span.content.as_ref())
152 .collect::<String>()
153 })
154 .collect()
155 }
156
157 /// The welcome headline is a whole sentence in most locales. It used to be
158 /// emitted as one unwrapped line, so the first screen of first run read
159 /// "Codewhale arbeitet mit dir in diesem O" in German at 40 columns.
160 #[test]
161 fn the_welcome_headline_survives_a_small_terminal_in_every_locale() {
162 let mut app = probe_app();
163 for locale in Locale::shipped().iter().copied() {
164 app.ui_locale = locale;
165 for width in [40usize, 60, 80] {
166 let rendered = flatten(lines(&app, width));
167 for row in &rendered {
168 assert!(
169 row.width() <= width,
170 "{locale:?} at {width}: row overflows: {row:?}",
171 );
172 }
173 // Whitespace removed, not collapsed: Japanese and Chinese wrap
174 // between characters with no space between them in the source.
175 let squash = |s: &str| s.chars().filter(|c| !c.is_whitespace()).collect::<String>();
176 let joined = squash(&rendered.join(" "));
177 let title = tr(locale, MessageId::OnboardWelcomeTitle);
178 assert!(
179 joined.contains(&squash(title.as_ref())),
180 "{locale:?} at {width}: headline was cut.\nwanted: {title}\ngot: {rendered:?}",
181 );
182 }
183 }
184 }
185 }
186
186 lines RUST