Skip to main content

auto_commit_rs/
ui.rs

1use inquire::Select;
2
3/// Replacement for `inquire::Confirm` — presents a Select with "Yes" / "No" choices.
4/// Returns `default_val` on cancellation (Esc/Ctrl-C).
5pub fn confirm(prompt: &str, default_val: bool) -> bool {
6    let choices = if default_val {
7        vec!["Yes", "No"]
8    } else {
9        vec!["No", "Yes"]
10    };
11    match Select::new(prompt, choices).prompt() {
12        Ok("Yes") => true,
13        Ok("No") => false,
14        _ => default_val,
15    }
16}
17
18/// Strip tree-drawing Unicode characters from a string for cleaner display.
19pub fn strip_tree_chars(s: &str) -> String {
20    s.chars()
21        .filter(|c| {
22            !matches!(
23                c,
24                '\u{2500}'
25                    ..='\u{257F}' | // Box Drawing block
26                '\u{25B6}' | '\u{25BC}' // Arrows ▶ ▼
27            )
28        })
29        .collect::<String>()
30        .split_whitespace()
31        .collect::<Vec<_>>()
32        .join(" ")
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn test_strip_tree_chars_branch() {
41        let input = "  ├── Provider              groq";
42        let result = strip_tree_chars(input);
43        assert_eq!(result, "Provider groq");
44    }
45
46    #[test]
47    fn test_strip_tree_chars_last_branch() {
48        let input = "  └── API Key               ****";
49        let result = strip_tree_chars(input);
50        assert_eq!(result, "API Key ****");
51    }
52
53    #[test]
54    fn test_strip_tree_chars_nested() {
55        let input = "  │   ├── Locale              en";
56        let result = strip_tree_chars(input);
57        assert_eq!(result, "Locale en");
58    }
59
60    #[test]
61    fn test_strip_tree_chars_arrow() {
62        let input = "▼ Basic";
63        let result = strip_tree_chars(input);
64        assert_eq!(result, "Basic");
65    }
66
67    #[test]
68    fn test_strip_tree_chars_no_change() {
69        let input = "Save & Exit";
70        let result = strip_tree_chars(input);
71        assert_eq!(result, "Save & Exit");
72    }
73}