Skip to main content

dodot_lib/render/
mod.rs

1//! Rendering infrastructure for dodot output.
2//!
3//! Wraps standout-render to provide a consistent rendering pipeline
4//! across all commands. The theme and templates are defined here;
5//! the CLI layer just picks an [`OutputMode`].
6
7use standout_render::{render_with_output, OutputMode, Renderer, Theme};
8
9use crate::Result;
10
11/// The dodot colour theme, defined in YAML for readability.
12///
13/// Style names are semantic — templates reference them by name,
14/// and the theme adapts to terminal capabilities automatically.
15const 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
103// ── Templates ───────────────────────────────────────────────────
104
105/// Status / up / down — pack-level output with file listings.
106///
107/// Per-item errors are surfaced as `[N]` markers next to the status label;
108/// their bodies render in a dedicated `Errors:` section at the bottom so
109/// the per-file columns stay single-line and aligned regardless of how
110/// long an individual error message is.
111pub const TEMPLATE_PACK_STATUS: &str = include_str!("../templates/pack-status.jinja");
112
113/// List — just pack names.
114pub const TEMPLATE_LIST: &str = include_str!("../templates/list.jinja");
115
116/// Simple message output (init, fill, adopt, addignore).
117pub const TEMPLATE_MESSAGE: &str = include_str!("../templates/message.jinja");
118
119// ── Renderer ────────────────────────────────────────────────────
120
121/// Create the dodot theme from the embedded YAML definition.
122pub fn create_theme() -> Theme {
123    Theme::from_yaml(THEME_YAML).expect("built-in theme YAML must be valid")
124}
125
126/// Create a pre-compiled renderer with all dodot templates registered.
127pub fn create_renderer() -> Renderer {
128    let theme = create_theme();
129    let mut renderer = Renderer::new(theme).expect("renderer creation must succeed");
130    renderer
131        .add_template("pack-status", TEMPLATE_PACK_STATUS)
132        .unwrap();
133    renderer.add_template("list", TEMPLATE_LIST).unwrap();
134    renderer.add_template("message", TEMPLATE_MESSAGE).unwrap();
135    renderer
136}
137
138/// Render a template with the given data and output mode.
139///
140/// For JSON mode, serializes the data directly (not through the
141/// template) to produce machine-readable output.
142pub fn render<T: serde::Serialize>(
143    template_name: &str,
144    data: &T,
145    mode: OutputMode,
146) -> Result<String> {
147    if matches!(mode, OutputMode::Json) {
148        return serde_json::to_string_pretty(data)
149            .map_err(|e| crate::DodotError::Other(format!("JSON serialization failed: {e}")));
150    }
151
152    let theme = create_theme();
153    let template = match template_name {
154        "pack-status" => TEMPLATE_PACK_STATUS,
155        "list" => TEMPLATE_LIST,
156        "message" => TEMPLATE_MESSAGE,
157        other => {
158            return Err(crate::DodotError::Other(format!(
159                "unknown template: {other}"
160            )))
161        }
162    };
163
164    render_with_output(template, data, &theme, mode)
165        .map_err(|e| crate::DodotError::Other(format!("render failed: {e}")))
166}
167
168#[cfg(test)]
169mod tests {
170    use super::*;
171
172    #[test]
173    fn theme_parses_without_error() {
174        let _theme = create_theme();
175    }
176
177    #[test]
178    fn renderer_creates_with_all_templates() {
179        let _renderer = create_renderer();
180    }
181
182    #[test]
183    fn render_pack_status_text_mode() {
184        use serde::Serialize;
185
186        #[derive(Serialize)]
187        struct Data {
188            message: Option<String>,
189            dry_run: bool,
190            packs: Vec<Pack>,
191        }
192        #[derive(Serialize)]
193        struct Pack {
194            name: String,
195            files: Vec<File>,
196        }
197        #[derive(Serialize)]
198        struct File {
199            name: String,
200            symbol: String,
201            description: String,
202            status: String,
203            status_label: String,
204        }
205
206        let data = Data {
207            message: None,
208            dry_run: false,
209            packs: vec![Pack {
210                name: "vim".into(),
211                files: vec![File {
212                    name: "vimrc".into(),
213                    symbol: "➞".into(),
214                    description: "~/.vimrc".into(),
215                    status: "deployed".into(),
216                    status_label: "deployed".into(),
217                }],
218            }],
219        };
220
221        let output = render("pack-status", &data, OutputMode::Text).unwrap();
222        assert!(output.contains("vim"));
223        assert!(output.contains("vimrc"));
224        assert!(output.contains("deployed"));
225    }
226
227    #[test]
228    fn json_mode_produces_json() {
229        use serde::Serialize;
230
231        #[derive(Serialize)]
232        struct Data {
233            name: String,
234        }
235
236        let data = Data {
237            name: "test".into(),
238        };
239
240        let output = render("list", &data, OutputMode::Json).unwrap();
241        assert!(output.contains("\"name\""));
242        assert!(output.contains("\"test\""));
243    }
244}