use crate::config::YamlConfig;
use crate::constants::DEFAULT_DISPLAY_SECTIONS;
use crate::md;
use crate::util::log::capitalize_first_letter;
pub fn handle_list(part: Option<&str>, config: &YamlConfig) {
let mut md_text = String::new();
match part {
None => {
for s in DEFAULT_DISPLAY_SECTIONS {
build_section_md(s, config, &mut md_text);
}
}
Some("all") => {
for section in config.all_section_names() {
build_section_md(section, config, &mut md_text);
}
}
Some(section) => {
build_section_md(section, config, &mut md_text);
}
}
if md_text.is_empty() {
crate::info!("无可展示的内容");
} else {
md!("{}", md_text);
}
}
fn build_section_md(section: &str, config: &YamlConfig, md_text: &mut String) {
if let Some(map) = config.get_section(section) {
if map.is_empty() {
return;
}
md_text.push_str(&format!("## {}\n", capitalize_first_letter(section)));
let max_key_len = map.keys().map(|k| k.len()).max().unwrap_or(0);
for (key, value) in map {
md_text.push_str(&format!(
"- `{:width$}` → {}\n",
key,
value,
width = max_key_len
));
}
md_text.push('\n');
} else {
crate::error!("该 section 不存在: {}", section);
}
}