cryosnap 0.2.11

CLI for cryosnap.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use crate::parse::{parse_box, parse_font_dirs, parse_font_fallbacks};
use cryosnap_core::{CjkRegion, Config, FontSystemFallback};
use dialoguer::{Confirm, Input, Select};
use std::error::Error;

pub(crate) fn run_interactive(
    config: &mut Config,
    input: &mut Option<String>,
    execute: &mut Option<String>,
) -> Result<(), Box<dyn Error>> {
    let prompter = DialoguerPrompter;
    run_interactive_with(&prompter, config, input, execute)
}

pub(crate) trait Prompter {
    fn select(&self, prompt: &str, items: &[&str], default: usize)
        -> Result<usize, Box<dyn Error>>;
    fn input_string(
        &self,
        prompt: &str,
        default: Option<&str>,
        allow_empty: bool,
    ) -> Result<String, Box<dyn Error>>;
    fn input_f32(&self, prompt: &str, default: f32) -> Result<f32, Box<dyn Error>>;
    fn confirm(&self, prompt: &str, default: bool) -> Result<bool, Box<dyn Error>>;
}

struct DialoguerPrompter;

impl Prompter for DialoguerPrompter {
    fn select(
        &self,
        prompt: &str,
        items: &[&str],
        default: usize,
    ) -> Result<usize, Box<dyn Error>> {
        Ok(Select::new()
            .with_prompt(prompt)
            .items(items)
            .default(default)
            .interact()?)
    }

    fn input_string(
        &self,
        prompt: &str,
        default: Option<&str>,
        allow_empty: bool,
    ) -> Result<String, Box<dyn Error>> {
        let mut input = Input::new().with_prompt(prompt).allow_empty(allow_empty);
        if let Some(value) = default {
            input = input.default(value.to_string());
        }
        Ok(input.interact_text()?)
    }

    fn input_f32(&self, prompt: &str, default: f32) -> Result<f32, Box<dyn Error>> {
        Ok(Input::new()
            .with_prompt(prompt)
            .default(default)
            .interact_text()?)
    }

    fn confirm(&self, prompt: &str, default: bool) -> Result<bool, Box<dyn Error>> {
        Ok(Confirm::new()
            .with_prompt(prompt)
            .default(default)
            .interact()?)
    }
}

pub(crate) fn run_interactive_with(
    prompter: &dyn Prompter,
    config: &mut Config,
    input: &mut Option<String>,
    execute: &mut Option<String>,
) -> Result<(), Box<dyn Error>> {
    let choice = prompter.select("Input source", &["file", "command", "stdin"], 0)?;

    match choice {
        0 => {
            let path = prompter.input_string("Input file path", None, true)?;
            if !path.trim().is_empty() {
                *input = Some(path);
                *execute = None;
            }
        }
        1 => {
            let cmd = prompter.input_string("Command to execute", None, true)?;
            if !cmd.trim().is_empty() {
                *execute = Some(cmd);
                *input = None;
            }
        }
        _ => {
            *input = Some("-".to_string());
            *execute = None;
        }
    }

    config.theme = prompter.input_string("Theme", Some(&config.theme), false)?;
    config.background = prompter.input_string("Background", Some(&config.background), false)?;

    let padding_default = config
        .padding
        .iter()
        .map(|v| v.to_string())
        .collect::<Vec<_>>()
        .join(" ");
    let padding = prompter.input_string("Padding (1,2,4 values)", Some(&padding_default), false)?;
    config.padding = parse_box(&padding)?;

    let margin_default = config
        .margin
        .iter()
        .map(|v| v.to_string())
        .collect::<Vec<_>>()
        .join(" ");
    let margin = prompter.input_string("Margin (1,2,4 values)", Some(&margin_default), false)?;
    config.margin = parse_box(&margin)?;

    config.window_controls = prompter.confirm("Show window controls?", config.window_controls)?;
    config.show_line_numbers = prompter.confirm("Show line numbers?", config.show_line_numbers)?;

    config.border.radius = prompter.input_f32("Border radius", config.border.radius)?;
    config.border.width = prompter.input_f32("Border width", config.border.width)?;
    config.border.color =
        prompter.input_string("Border color", Some(&config.border.color), false)?;

    config.shadow.blur = prompter.input_f32("Shadow blur", config.shadow.blur)?;
    config.shadow.x = prompter.input_f32("Shadow offset X", config.shadow.x)?;
    config.shadow.y = prompter.input_f32("Shadow offset Y", config.shadow.y)?;

    config.font.family = prompter.input_string("Font family", Some(&config.font.family), false)?;
    let fallbacks_default = if config.font.fallbacks.is_empty() {
        String::new()
    } else {
        config.font.fallbacks.join(", ")
    };
    let fallbacks = prompter.input_string(
        "Font fallbacks (comma-separated)",
        Some(&fallbacks_default),
        true,
    )?;
    config.font.fallbacks = parse_font_fallbacks(&fallbacks)?;
    let fallback_items = ["auto", "always", "never"];
    let fallback_default = match config.font.system_fallback {
        FontSystemFallback::Auto => 0,
        FontSystemFallback::Always => 1,
        FontSystemFallback::Never => 2,
    };
    let fallback_choice =
        prompter.select("System font fallback", &fallback_items, fallback_default)?;
    config.font.system_fallback = match fallback_choice {
        1 => FontSystemFallback::Always,
        2 => FontSystemFallback::Never,
        _ => FontSystemFallback::Auto,
    };
    config.font.auto_download =
        prompter.confirm("Auto-download missing fonts?", config.font.auto_download)?;
    let cjk_items = ["auto", "sc", "tc", "hk", "jp", "kr"];
    let cjk_default = match config.font.cjk_region {
        CjkRegion::Auto => 0,
        CjkRegion::Sc => 1,
        CjkRegion::Tc => 2,
        CjkRegion::Hk => 3,
        CjkRegion::Jp => 4,
        CjkRegion::Kr => 5,
    };
    let cjk_choice = prompter.select("CJK region", &cjk_items, cjk_default)?;
    config.font.cjk_region = match cjk_choice {
        1 => CjkRegion::Sc,
        2 => CjkRegion::Tc,
        3 => CjkRegion::Hk,
        4 => CjkRegion::Jp,
        5 => CjkRegion::Kr,
        _ => CjkRegion::Auto,
    };
    config.font.force_update =
        prompter.confirm("Force refresh downloaded fonts?", config.font.force_update)?;
    let dirs_default = if config.font.dirs.is_empty() {
        String::new()
    } else {
        config.font.dirs.join(", ")
    };
    let dirs = prompter.input_string(
        "Font dirs (comma-separated, empty for default)",
        Some(&dirs_default),
        true,
    )?;
    config.font.dirs = parse_font_dirs(&dirs)?;
    config.font.size = prompter.input_f32("Font size", config.font.size)?;
    config.font.ligatures = prompter.confirm("Enable ligatures?", config.font.ligatures)?;
    config.line_height = prompter.input_f32("Line height", config.line_height)?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::cell::RefCell;
    use std::collections::VecDeque;

    struct FakePrompter {
        selects: RefCell<VecDeque<usize>>,
        strings: RefCell<VecDeque<String>>,
        floats: RefCell<VecDeque<f32>>,
        bools: RefCell<VecDeque<bool>>,
    }

    impl FakePrompter {
        fn new() -> Self {
            Self {
                selects: RefCell::new(VecDeque::new()),
                strings: RefCell::new(VecDeque::new()),
                floats: RefCell::new(VecDeque::new()),
                bools: RefCell::new(VecDeque::new()),
            }
        }
    }

    impl Prompter for FakePrompter {
        fn select(
            &self,
            _prompt: &str,
            _items: &[&str],
            _default: usize,
        ) -> Result<usize, Box<dyn Error>> {
            self.selects
                .borrow_mut()
                .pop_front()
                .ok_or_else(|| "missing select".into())
        }

        fn input_string(
            &self,
            _prompt: &str,
            _default: Option<&str>,
            _allow_empty: bool,
        ) -> Result<String, Box<dyn Error>> {
            self.strings
                .borrow_mut()
                .pop_front()
                .ok_or_else(|| "missing string".into())
        }

        fn input_f32(&self, _prompt: &str, _default: f32) -> Result<f32, Box<dyn Error>> {
            self.floats
                .borrow_mut()
                .pop_front()
                .ok_or_else(|| "missing float".into())
        }

        fn confirm(&self, _prompt: &str, _default: bool) -> Result<bool, Box<dyn Error>> {
            self.bools
                .borrow_mut()
                .pop_front()
                .ok_or_else(|| "missing bool".into())
        }
    }

    #[test]
    fn run_interactive_updates_config() {
        let prompter = FakePrompter::new();
        prompter.selects.borrow_mut().push_back(0);
        prompter.selects.borrow_mut().push_back(0);
        prompter.selects.borrow_mut().push_back(0);
        prompter
            .strings
            .borrow_mut()
            .push_back("input.rs".to_string());
        prompter.strings.borrow_mut().push_back("charm".to_string());
        prompter
            .strings
            .borrow_mut()
            .push_back("#000000".to_string());
        prompter.strings.borrow_mut().push_back("10,20".to_string());
        prompter.strings.borrow_mut().push_back("5".to_string());
        prompter
            .strings
            .borrow_mut()
            .push_back("#333333".to_string());
        prompter.strings.borrow_mut().push_back("Test".to_string());
        prompter
            .strings
            .borrow_mut()
            .push_back("Symbols Nerd Font Mono, Noto Sans CJK SC".to_string());
        prompter.strings.borrow_mut().push_back(String::new());
        prompter.floats.borrow_mut().push_back(4.0);
        prompter.floats.borrow_mut().push_back(1.0);
        prompter.floats.borrow_mut().push_back(6.0);
        prompter.floats.borrow_mut().push_back(0.0);
        prompter.floats.borrow_mut().push_back(12.0);
        prompter.floats.borrow_mut().push_back(14.0);
        prompter.floats.borrow_mut().push_back(1.3);
        prompter.bools.borrow_mut().push_back(true);
        prompter.bools.borrow_mut().push_back(true);
        prompter.bools.borrow_mut().push_back(true);
        prompter.bools.borrow_mut().push_back(false);
        prompter.bools.borrow_mut().push_back(false);

        let mut cfg = Config::default();
        let mut input = None;
        let mut execute = None;
        run_interactive_with(&prompter, &mut cfg, &mut input, &mut execute).expect("interactive");

        assert_eq!(input.as_deref(), Some("input.rs"));
        assert_eq!(cfg.padding, vec![10.0, 20.0]);
        assert_eq!(cfg.margin, vec![5.0]);
        assert!(cfg.window_controls);
        assert!(cfg.show_line_numbers);
        assert_eq!(cfg.border.radius, 4.0);
        assert_eq!(cfg.border.width, 1.0);
        assert_eq!(cfg.border.color, "#333333");
        assert_eq!(cfg.shadow.blur, 6.0);
        assert_eq!(cfg.font.family, "Test");
        assert_eq!(
            cfg.font.fallbacks,
            vec!["Symbols Nerd Font Mono", "Noto Sans CJK SC"]
        );
        assert!(matches!(cfg.font.system_fallback, FontSystemFallback::Auto));
        assert!(cfg.font.auto_download);
        assert!(matches!(cfg.font.cjk_region, CjkRegion::Auto));
    }

    #[test]
    fn run_interactive_with_stdin_choice() {
        let prompter = FakePrompter::new();
        prompter.selects.borrow_mut().push_back(2);
        prompter.selects.borrow_mut().push_back(0);
        prompter.selects.borrow_mut().push_back(0);
        prompter.strings.borrow_mut().push_back("charm".to_string());
        prompter
            .strings
            .borrow_mut()
            .push_back("#000000".to_string());
        prompter.strings.borrow_mut().push_back("10".to_string());
        prompter.strings.borrow_mut().push_back("0".to_string());
        prompter
            .strings
            .borrow_mut()
            .push_back("#333333".to_string());
        prompter.strings.borrow_mut().push_back("Test".to_string());
        prompter.strings.borrow_mut().push_back(String::new());
        prompter.strings.borrow_mut().push_back(String::new());
        prompter.floats.borrow_mut().push_back(0.0);
        prompter.floats.borrow_mut().push_back(0.0);
        prompter.floats.borrow_mut().push_back(0.0);
        prompter.floats.borrow_mut().push_back(0.0);
        prompter.floats.borrow_mut().push_back(12.0);
        prompter.floats.borrow_mut().push_back(14.0);
        prompter.floats.borrow_mut().push_back(1.3);
        prompter.bools.borrow_mut().push_back(false);
        prompter.bools.borrow_mut().push_back(false);
        prompter.bools.borrow_mut().push_back(false);
        prompter.bools.borrow_mut().push_back(false);
        prompter.bools.borrow_mut().push_back(false);

        let mut cfg = Config::default();
        let mut input = None;
        let mut execute = None;
        run_interactive_with(&prompter, &mut cfg, &mut input, &mut execute).expect("interactive");
        assert_eq!(input.as_deref(), Some("-"));
    }

    #[test]
    fn run_interactive_selects_never_fallback_and_hk_region() {
        let prompter = FakePrompter::new();
        prompter.selects.borrow_mut().push_back(0);
        prompter.selects.borrow_mut().push_back(2);
        prompter.selects.borrow_mut().push_back(3);
        prompter
            .strings
            .borrow_mut()
            .push_back("input.rs".to_string());
        prompter.strings.borrow_mut().push_back("charm".to_string());
        prompter
            .strings
            .borrow_mut()
            .push_back("#000000".to_string());
        prompter.strings.borrow_mut().push_back("10".to_string());
        prompter.strings.borrow_mut().push_back("0".to_string());
        prompter
            .strings
            .borrow_mut()
            .push_back("#333333".to_string());
        prompter.strings.borrow_mut().push_back("Test".to_string());
        prompter.strings.borrow_mut().push_back(String::new());
        prompter.strings.borrow_mut().push_back(String::new());
        prompter.floats.borrow_mut().push_back(0.0);
        prompter.floats.borrow_mut().push_back(0.0);
        prompter.floats.borrow_mut().push_back(0.0);
        prompter.floats.borrow_mut().push_back(0.0);
        prompter.floats.borrow_mut().push_back(12.0);
        prompter.floats.borrow_mut().push_back(14.0);
        prompter.floats.borrow_mut().push_back(1.3);
        prompter.bools.borrow_mut().push_back(false);
        prompter.bools.borrow_mut().push_back(false);
        prompter.bools.borrow_mut().push_back(false);
        prompter.bools.borrow_mut().push_back(false);
        prompter.bools.borrow_mut().push_back(false);

        let mut cfg = Config::default();
        let mut input = None;
        let mut execute = None;
        run_interactive_with(&prompter, &mut cfg, &mut input, &mut execute).expect("interactive");

        assert!(matches!(
            cfg.font.system_fallback,
            FontSystemFallback::Never
        ));
        assert!(matches!(cfg.font.cjk_region, CjkRegion::Hk));
    }

    #[test]
    fn interactive_command_branch() {
        let prompter = FakePrompter::new();
        prompter.selects.borrow_mut().push_back(1);
        prompter.selects.borrow_mut().push_back(0);
        prompter.selects.borrow_mut().push_back(0);
        prompter
            .strings
            .borrow_mut()
            .push_back("echo hi".to_string());
        prompter.strings.borrow_mut().push_back("charm".to_string());
        prompter
            .strings
            .borrow_mut()
            .push_back("#000000".to_string());
        prompter.strings.borrow_mut().push_back("10".to_string());
        prompter.strings.borrow_mut().push_back("5".to_string());
        prompter
            .strings
            .borrow_mut()
            .push_back("#333333".to_string());
        prompter.strings.borrow_mut().push_back("Test".to_string());
        prompter
            .strings
            .borrow_mut()
            .push_back("Symbols Nerd Font Mono".to_string());
        prompter.strings.borrow_mut().push_back(String::new());
        prompter.floats.borrow_mut().push_back(4.0);
        prompter.floats.borrow_mut().push_back(1.0);
        prompter.floats.borrow_mut().push_back(6.0);
        prompter.floats.borrow_mut().push_back(0.0);
        prompter.floats.borrow_mut().push_back(12.0);
        prompter.floats.borrow_mut().push_back(14.0);
        prompter.floats.borrow_mut().push_back(1.3);
        prompter.bools.borrow_mut().push_back(true);
        prompter.bools.borrow_mut().push_back(true);
        prompter.bools.borrow_mut().push_back(true);
        prompter.bools.borrow_mut().push_back(false);
        prompter.bools.borrow_mut().push_back(false);

        let mut cfg = Config::default();
        let mut input = None;
        let mut execute = None;
        run_interactive_with(&prompter, &mut cfg, &mut input, &mut execute).expect("interactive");
        assert_eq!(execute.as_deref(), Some("echo hi"));
        assert!(input.is_none());
    }
}