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
//! Command `source`
use crate::{registry::Registry, result::Result};
use std::cmp::Ordering;

const MAX_PACKAGE_NAME_LEN: usize = 50;

fn cap(mut name: String) -> String {
    name.push_str(&" ".repeat(MAX_PACKAGE_NAME_LEN - name.len()));
    name
}

/// Exec command `source`
pub fn exec(query: String, tag: String, version: bool) -> Result<()> {
    let registry = Registry::new()?;
    let mut should_checkout = false;
    let mut source = registry.source()?;
    source.sort_by(|(np, _), (nq, _)| np.partial_cmp(nq).unwrap_or(Ordering::Greater));

    // tags
    if !tag.is_empty() {
        should_checkout = true;
        registry.checkout(&tag)?;
    }

    println!(
        "{}",
        if query.is_empty() {
            source
                .iter()
                .map(|(name, ver)| {
                    if version {
                        name.to_string()
                    } else {
                        format!("{} - {}", cap(name.to_string()), ver)
                    }
                })
                .collect::<Vec<String>>()
        } else {
            source
                .iter()
                .filter(|(name, _)| name.contains(&query))
                .map(|(name, ver)| {
                    if version {
                        name.to_string()
                    } else {
                        format!("{} - {}", cap(name.to_string()), ver)
                    }
                })
                .collect::<Vec<String>>()
        }
        .join("\n")
    );

    // Checkout back to the latest commit
    if should_checkout {
        registry.checkout("master")?;
    }
    Ok(())
}