use anyhow::{anyhow, Result};
use std::path::Path;
use crate::find::find_templates;
pub fn list(
patterns: Vec<&str>,
base_dir: &Path,
config_dir: &Path,
_verbose: bool,
lines: bool,
) -> Result<()> {
let mut templates = Vec::new();
for pattern in patterns {
let found_templates = find_templates(pattern, base_dir, config_dir)?;
for found_template in found_templates {
templates.push(found_template
.strip_prefix(base_dir)
.map_or_else(
|_| found_template.strip_prefix(config_dir),
|path| path.strip_prefix("base16/"),
)
.map_err(|_| anyhow!("Couldn't get template name"))?
.to_str()
.ok_or_else(|| anyhow!("Couldn't convert name"))?
.replacen("templates/", "", 2)
.replace(".mustache","")
);
}
}
templates.sort();
templates.dedup();
if templates.is_empty() {
return Err(anyhow!("No matching template found"));
};
for template in &templates {
print!("{}", template);
if lines {
println!();
} else {
print!(" ");
}
}
if !lines {
println!();
}
Ok(())
}