use crate::loader::models::{PlanDetail, PlanItem};
use crate::plan_text::constants::HINT_COLUMN;
pub(crate) fn marks(last: bool) -> (&'static str, &'static str) {
match last {
true => ("\u{2514}\u{2500}\u{2500} ", " "),
false => ("\u{251c}\u{2500}\u{2500} ", "\u{2502} "),
}
}
pub(crate) fn hinted(heading: &str, hint: &str) -> String {
let width = HINT_COLUMN.max(heading.chars().count() + 2);
format!("{heading:<width$}{hint}")
}
pub(crate) fn branch(line: &str, details: &[PlanDetail], last: bool, indent: &str) -> Vec<String> {
let (mark, below) = marks(last);
let mut lines: Vec<String> = vec![format!("{indent}{mark}{line}")];
for (index, detail) in details.iter().enumerate() {
let (sub, continued) = marks(index + 1 == details.len());
let label = match detail.label.is_empty() {
true => String::new(),
false => format!("{} ", detail.label),
};
let width = label.chars().count();
for (line_index, text) in detail.text.split('\n').enumerate() {
lines.push(match line_index {
0 => format!("{indent}{below}{sub}{label}{text}"),
_ => format!("{indent}{below}{continued}{:width$}{text}", ""),
});
}
}
lines
}
pub(crate) fn section(heading: &str, hint: &str, items: &[PlanItem]) -> Vec<String> {
if items.is_empty() {
return Vec::new();
}
let width = items
.iter()
.map(|item| item.name.chars().count())
.max()
.unwrap_or(0)
+ 2;
let mut lines: Vec<String> = vec![hinted(&format!("{heading} ({})", items.len()), hint)];
for (index, item) in items.iter().enumerate() {
let line = match item.action.is_empty() {
true => item.name.clone(),
false => format!("{:<width$}{}", item.name, item.action),
};
lines.extend(branch(&line, &item.details, index + 1 == items.len(), ""));
}
lines.push(String::new());
lines
}