use crate::{config, formatting::table::AsciiTable};
pub fn format_task(task: toado::Task, config: &config::Config) -> String {
let mut lines: Vec<String> = Vec::new();
if let Some(name) = task.name {
let name_l = name.len();
if let Some(id) = task.id {
let id = id.to_string();
let id_l = id.len();
lines.push(format!("{} {} {}", name, config.table.vertical, id));
lines.push(format!(
"{}{}{}",
config.table.horizontal.to_string().repeat(name_l + 1),
config.table.up_horizontal,
config.table.horizontal.to_string().repeat(id_l + 1)
))
} else {
lines.push(name);
lines.push(config.table.horizontal.to_string().repeat(name_l))
}
}
if let Some(priority) = task.priority {
lines.push(format!("Priority: {priority}"));
}
if let Some(status) = task.status {
lines.push(format!("Status: {}", status.to_string().to_uppercase()));
}
if let Some(start_time) = task.start_time {
lines.push(format!("Start: {start_time}"));
if let Some(end_time) = task.end_time {
lines.push(format!(" End: {end_time}"));
}
} else if let Some(end_time) = task.end_time {
lines.push(format!("End: {end_time}"));
}
if let Some(repeat) = task.repeat {
lines.push(format!("Repeats: {repeat}"));
}
if let Some(notes) = task.notes {
lines.push(format!("Notes: {notes}"))
}
lines.join("\n")
}
pub fn format_task_list(
tasks: Vec<toado::Task>,
verbose: bool,
config: &config::TableConfig,
) -> String {
let table = AsciiTable::new(
tasks
.into_iter()
.map(|task| {
let mut cols = vec![
task.id.map_or_else(|| "-".to_string(), |v| v.to_string()),
task.name.unwrap_or("-".to_string()),
task.priority
.map_or_else(|| "-".to_string(), |v| v.to_string()),
task.status
.map_or_else(|| "-".to_string(), |v| v.to_string().to_uppercase()),
];
if verbose {
cols.push(task.start_time.unwrap_or("-".to_string()));
cols.push(task.end_time.unwrap_or("-".to_string()));
cols.push(task.repeat.unwrap_or("-".to_string()));
cols.push(task.notes.unwrap_or("-".to_string()));
}
cols
})
.collect::<Vec<Vec<String>>>(),
config,
);
table
.seperate_cols(config.seperate_cols)
.seperate_rows(config.seperate_rows)
.to_string()
}