1use standout_render::{render_with_output, OutputMode, Renderer, Theme};
8
9use crate::Result;
10
11const THEME_YAML: &str = r#"
16pack-name:
17 bold: true
18 fg: blue
19
20filename:
21 fg: white
22
23handler-symbol:
24 bold: true
25 fg: yellow
26
27description:
28 dim: true
29
30deployed:
31 fg: green
32
33pending:
34 fg: magenta
35
36error:
37 fg: red
38 bold: true
39
40broken:
41 fg: red
42
43stale:
44 fg: yellow
45
46warning:
47 fg: yellow
48
49message:
50 fg: cyan
51
52dim:
53 dim: true
54
55header:
56 bold: true
57
58dry-run:
59 fg: yellow
60 italic: true
61
62conflict-banner:
63 fg: white
64 bg: red
65 bold: true
66
67conflict-header:
68 fg: white
69 bg: red
70 bold: true
71
72conflict-target:
73 fg: red
74 bold: true
75
76conflict-pack:
77 fg: red
78
79conflict-hint:
80 dim: true
81
82ignored-pack:
83 dim: true
84 italic: true
85
86group-banner-deployed:
87 fg: green
88 bold: true
89
90group-banner-pending:
91 fg: yellow
92 bold: true
93
94group-banner-error:
95 fg: red
96 bold: true
97
98group-banner-ignored:
99 dim: true
100 bold: true
101
102# Tutorial prompt question text. The interactive `dodot tutorial`
103# uses inquire for the prompt UI; this style is mirrored by hand into
104# its `RenderConfig` (see `tutorial.rs::tutorial_render_config`). Keep
105# attributes here in sync with that function so users have one place
106# to change the look.
107tutorial-prompt:
108 italic: true
109
110# CLI help tags. The hand-written --help text in `dodot-cli/src/help/`
111# uses these alongside the semantic tags above. Mirror standout's
112# default help theme so the look matches the rest of dodot's output:
113# item — bold (command names, option flags)
114# desc — plain (descriptions next to items)
115# usage — plain (the usage line)
116# example — plain (example blocks)
117# about — plain (intro / about text)
118item:
119 bold: true
120desc: {}
121usage: {}
122example: {}
123about: {}
124"#;
125
126pub const TEMPLATE_PACK_STATUS: &str = include_str!("../templates/pack-status.jinja");
135
136pub const TEMPLATE_LIST: &str = include_str!("../templates/list.jinja");
138
139pub const TEMPLATE_MESSAGE: &str = include_str!("../templates/message.jinja");
141
142pub const TEMPLATE_PROBE: &str = include_str!("../templates/probe.jinja");
145
146pub const TEMPLATE_GIT_FILTERS: &str = include_str!("../templates/git-filters.jinja");
148
149pub const TEMPLATE_PROMPTS_LIST: &str = include_str!("../templates/prompts-list.jinja");
151
152pub const TEMPLATE_TRANSFORM_CHECK: &str = include_str!("../templates/transform-check.jinja");
155
156pub const TEMPLATE_TRANSFORM_INSTALL_HOOK: &str =
159 include_str!("../templates/transform-install-hook.jinja");
160
161pub const TEMPLATE_REFRESH: &str = include_str!("../templates/refresh.jinja");
163
164pub const TEMPLATE_TEMPLATE_INSTALL_FILTER: &str =
166 include_str!("../templates/template-install-filter.jinja");
167
168pub const TEMPLATE_TRANSFORM_STATUS: &str = include_str!("../templates/transform-status.jinja");
170
171pub const TEMPLATE_GIT_SHOW_ALIAS: &str = include_str!("../templates/git-show-alias.jinja");
173
174pub const TEMPLATE_GIT_INSTALL_ALIAS: &str = include_str!("../templates/git-install-alias.jinja");
176
177pub const TEMPLATE_TUTORIAL_INTRO: &str = include_str!("../templates/tutorial/intro.jinja");
183pub const TEMPLATE_TUTORIAL_CHECK_ROOT: &str =
184 include_str!("../templates/tutorial/check_root.jinja");
185pub const TEMPLATE_TUTORIAL_PICK_PACK: &str = include_str!("../templates/tutorial/pick_pack.jinja");
186pub const TEMPLATE_TUTORIAL_NO_PACKS: &str = include_str!("../templates/tutorial/no_packs.jinja");
187pub const TEMPLATE_TUTORIAL_SHOW_STATUS: &str =
188 include_str!("../templates/tutorial/show_status.jinja");
189pub const TEMPLATE_TUTORIAL_ANNOTATE_STATUS: &str =
190 include_str!("../templates/tutorial/annotate_status.jinja");
191pub const TEMPLATE_TUTORIAL_CONCEPT_TARGETS: &str =
192 include_str!("../templates/tutorial/concept_targets.jinja");
193pub const TEMPLATE_TUTORIAL_CONCEPT_SHELL: &str =
194 include_str!("../templates/tutorial/concept_shell.jinja");
195pub const TEMPLATE_TUTORIAL_DRY_RUN: &str = include_str!("../templates/tutorial/dry_run.jinja");
196pub const TEMPLATE_TUTORIAL_REAL_UP: &str = include_str!("../templates/tutorial/real_up.jinja");
197pub const TEMPLATE_TUTORIAL_OUTRO: &str = include_str!("../templates/tutorial/outro.jinja");
198
199pub const TUTORIAL_STEP_TEMPLATES: &[(&str, &str)] = &[
206 ("tutorial.intro", TEMPLATE_TUTORIAL_INTRO),
207 ("tutorial.check_root", TEMPLATE_TUTORIAL_CHECK_ROOT),
208 ("tutorial.pick_pack", TEMPLATE_TUTORIAL_PICK_PACK),
209 ("tutorial.no_packs", TEMPLATE_TUTORIAL_NO_PACKS),
210 ("tutorial.show_status", TEMPLATE_TUTORIAL_SHOW_STATUS),
211 (
212 "tutorial.annotate_status",
213 TEMPLATE_TUTORIAL_ANNOTATE_STATUS,
214 ),
215 (
216 "tutorial.concept_targets",
217 TEMPLATE_TUTORIAL_CONCEPT_TARGETS,
218 ),
219 ("tutorial.concept_shell", TEMPLATE_TUTORIAL_CONCEPT_SHELL),
220 ("tutorial.dry_run", TEMPLATE_TUTORIAL_DRY_RUN),
221 ("tutorial.real_up", TEMPLATE_TUTORIAL_REAL_UP),
222 ("tutorial.outro", TEMPLATE_TUTORIAL_OUTRO),
223];
224
225pub fn render_tutorial_step<T: serde::Serialize>(
230 step: &str,
231 data: &T,
232 mode: OutputMode,
233) -> Result<String> {
234 let body = TUTORIAL_STEP_TEMPLATES
235 .iter()
236 .find_map(|(name, body)| (*name == step).then_some(*body))
237 .ok_or_else(|| crate::DodotError::Other(format!("unknown tutorial template: {step}")))?;
238
239 let theme = create_theme();
240 render_with_output(body, data, &theme, mode)
241 .map_err(|e| crate::DodotError::Other(format!("tutorial render: {e}")))
242}
243
244pub fn create_theme() -> Theme {
248 Theme::from_yaml(THEME_YAML).expect("built-in theme YAML must be valid")
249}
250
251pub fn create_renderer() -> Renderer {
253 let theme = create_theme();
254 let mut renderer = Renderer::new(theme).expect("renderer creation must succeed");
255 renderer
256 .add_template("pack-status", TEMPLATE_PACK_STATUS)
257 .unwrap();
258 renderer.add_template("list", TEMPLATE_LIST).unwrap();
259 renderer.add_template("message", TEMPLATE_MESSAGE).unwrap();
260 renderer.add_template("probe", TEMPLATE_PROBE).unwrap();
261 renderer
262 .add_template("git-filters", TEMPLATE_GIT_FILTERS)
263 .unwrap();
264 renderer
265 .add_template("prompts-list", TEMPLATE_PROMPTS_LIST)
266 .unwrap();
267 renderer
268}
269
270pub fn render<T: serde::Serialize>(
275 template_name: &str,
276 data: &T,
277 mode: OutputMode,
278) -> Result<String> {
279 if matches!(mode, OutputMode::Json) {
280 return serde_json::to_string_pretty(data)
281 .map_err(|e| crate::DodotError::Other(format!("JSON serialization failed: {e}")));
282 }
283
284 let theme = create_theme();
285 let template = match template_name {
286 "pack-status" => TEMPLATE_PACK_STATUS,
287 "list" => TEMPLATE_LIST,
288 "message" => TEMPLATE_MESSAGE,
289 "probe" => TEMPLATE_PROBE,
290 "git-filters" => TEMPLATE_GIT_FILTERS,
291 "prompts-list" => TEMPLATE_PROMPTS_LIST,
292 other => {
293 return Err(crate::DodotError::Other(format!(
294 "unknown template: {other}"
295 )))
296 }
297 };
298
299 render_with_output(template, data, &theme, mode)
300 .map_err(|e| crate::DodotError::Other(format!("render failed: {e}")))
301}
302
303#[cfg(test)]
304mod tests {
305 use super::*;
306
307 #[test]
308 fn theme_parses_without_error() {
309 let _theme = create_theme();
310 }
311
312 #[test]
313 fn renderer_creates_with_all_templates() {
314 let _renderer = create_renderer();
315 }
316
317 #[test]
318 fn render_pack_status_text_mode() {
319 use serde::Serialize;
320
321 #[derive(Serialize)]
322 struct Data {
323 message: Option<String>,
324 dry_run: bool,
325 packs: Vec<Pack>,
326 }
327 #[derive(Serialize)]
328 struct Pack {
329 name: String,
330 files: Vec<File>,
331 }
332 #[derive(Serialize)]
333 struct File {
334 name: String,
335 symbol: String,
336 description: String,
337 status: String,
338 status_label: String,
339 }
340
341 let data = Data {
342 message: None,
343 dry_run: false,
344 packs: vec![Pack {
345 name: "vim".into(),
346 files: vec![File {
347 name: "vimrc".into(),
348 symbol: "➞".into(),
349 description: "~/.vimrc".into(),
350 status: "deployed".into(),
351 status_label: "deployed".into(),
352 }],
353 }],
354 };
355
356 let output = render("pack-status", &data, OutputMode::Text).unwrap();
357 assert!(output.contains("vim"));
358 assert!(output.contains("vimrc"));
359 assert!(output.contains("deployed"));
360 }
361
362 #[test]
363 fn all_tutorial_templates_render_in_text_mode() {
364 use crate::commands::tutorial::{TutorialCtx, TutorialPack};
368
369 let ctx = TutorialCtx {
370 dotfiles_root: "/home/example/dotfiles".into(),
371 via: "DOTFILES_ROOT env var".into(),
372 packs: vec![
373 TutorialPack {
374 name: "vim".into(),
375 kind: "config only".into(),
376 recommended: true,
377 },
378 TutorialPack {
379 name: "zsh".into(),
380 kind: "config + shell".into(),
381 recommended: false,
382 },
383 ],
384 chosen_pack: Some("vim".into()),
385 chosen_pack_kind: Some("config only".into()),
386 status_output: Some("(rendered status would go here)".into()),
387 dry_run_output: Some("(dry-run output)".into()),
388 up_output: Some("(up output)".into()),
389 shell_integration: Some(crate::commands::tutorial::ShellIntegration {
390 shell_kind: "zsh".into(),
391 rc_path: "~/.zshrc".into(),
392 rc_path_abs: std::path::PathBuf::new(),
393 line_present: false,
394 eval_line: r#"eval "$(dodot init-sh)""#.into(),
395 }),
396 eval_line: r#"eval "$(dodot init-sh)""#.into(),
397 ..Default::default()
398 };
399
400 for (name, _) in TUTORIAL_STEP_TEMPLATES {
401 let out = render_tutorial_step(name, &ctx, OutputMode::Text)
402 .unwrap_or_else(|e| panic!("template {name} failed: {e}"));
403 assert!(!out.is_empty(), "template {name} produced empty output");
404 }
405 }
406
407 #[test]
408 fn json_mode_produces_json() {
409 use serde::Serialize;
410
411 #[derive(Serialize)]
412 struct Data {
413 name: String,
414 }
415
416 let data = Data {
417 name: "test".into(),
418 };
419
420 let output = render("list", &data, OutputMode::Json).unwrap();
421 assert!(output.contains("\"name\""));
422 assert!(output.contains("\"test\""));
423 }
424}