git_revise/revise/prompts/
commit_scope.rs

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
use inquire::{Select, Text};

use super::Inquire;
use crate::{
    config::{self},
    error::ReviseResult,
};

#[derive(Debug, Clone)]
pub struct Part {
    pub msg: String,
    pub ans: Option<String>,
    pub options: Vec<String>,
}

impl Part {
    pub fn new() -> Self {
        let cfg = config::get_config();
        let mut options: Vec<String> = cfg.get_scopes();
        // Prepend "empty" if not present
        if !options.contains(&"empty".to_string()) {
            options.insert(0, "empty".to_string());
        }

        // Append "custom" if not present
        if !options.contains(&"custom".to_string()) {
            options.push("custom".to_string());
        }

        Self {
            msg: "Denote the SCOPE of this change (optional):".to_string(),
            ans: None,
            options,
        }
    }
}

impl Default for Part {
    fn default() -> Self {
        Self::new()
    }
}

impl Inquire for Part {
    fn inquire(&mut self) -> ReviseResult<()> {
        let mut ans = Select::new(&self.msg, self.options.clone()).prompt()?;

        if ans == "custom" {
            ans = Text::new("Denote the SCOPE of this change:").prompt()?;
            self.ans = Some(ans).filter(|a| !a.is_empty());
        } else if ans == "empty" {
            self.ans = None;
        } else {
            self.ans = Some(ans);
        };
        Ok(())
    }
}