flavours/operations/
list.rs

1use anyhow::{anyhow, Result};
2use std::path::Path;
3
4use crate::find::find_schemes;
5
6/// List subcommand
7///
8/// * `patterns` - Vector with patterns
9/// * `base_dir` - flavours' base data dir
10/// * `config_dir` - flavours' config dir
11/// * `verbose` - Should we be verbose? (unused)
12/// * `lines` - Should we print each scheme on its own line?
13pub fn list(
14    patterns: Vec<&str>,
15    base_dir: &Path,
16    config_dir: &Path,
17    _verbose: bool,
18    lines: bool,
19) -> Result<()> {
20    let mut schemes = Vec::new();
21    for pattern in patterns {
22        let found_schemes = find_schemes(pattern, base_dir, config_dir)?;
23
24        for found_scheme in found_schemes {
25            schemes.push(String::from(
26                found_scheme
27                    .file_stem()
28                    .ok_or_else(|| anyhow!("Couldn't get scheme name"))?
29                    .to_str()
30                    .ok_or_else(|| anyhow!("Couldn't convert name"))?,
31            ));
32        }
33    }
34    schemes.sort();
35    schemes.dedup();
36
37    if schemes.is_empty() {
38        return Err(anyhow!("No matching scheme found"));
39    };
40
41    for scheme in &schemes {
42        // Print scheme
43        print!("{}", scheme);
44        if lines {
45            // Print newline
46            println!();
47        } else {
48            // Print space
49            print!(" ");
50        }
51    }
52    // If we separated by spaces, print an ending newline
53    if !lines {
54        println!();
55    }
56
57    Ok(())
58}