#![cfg(not(tarpaulin_include))]
use cli_table::format::Justify;
use cli_table::{Cell, CellStruct, Style, Table, TableStruct, print_stdout};
use gitlab_time_report::model::{Label, TimeLog};
use gitlab_time_report::tables;
use regex::Regex;
use std::collections::HashSet;
fn create_table(table_data: Vec<Vec<CellStruct>>, column_titles: &[&str]) -> TableStruct {
let titles: Vec<_> = column_titles
.iter()
.map(|title| title.cell().bold(true))
.collect();
table_data.table().title(titles)
}
fn print_table(table: TableStruct) {
print_stdout(table).unwrap_or_else(|err| {
eprintln!("Error printing table: {err}");
std::process::exit(4);
});
}
fn to_cell_vec(table: Vec<Vec<String>>) -> Vec<Vec<CellStruct>> {
let is_duration = Regex::new(r"^[0-9]{2,}h [0-9]{2}m$").expect("Regex pattern should compile");
table
.into_iter()
.map(|row| {
row.into_iter()
.map(|cell| match is_duration.is_match(&cell) {
true => cell.cell().justify(Justify::Right),
false => cell.cell(),
})
.collect()
})
.collect()
}
pub fn print_total_time_by_label(
time_logs: &[TimeLog],
label_filter: Option<&HashSet<String>>,
label_others: Option<&Label>,
) {
let (table_data, table_header) =
tables::populate_table_timelogs_by_label(time_logs, label_filter, label_others);
let table_data_cells = to_cell_vec(table_data);
let table = create_table(table_data_cells, table_header);
println!("\nTime Spent per Label:");
print_table(table);
}
pub fn print_total_time_by_milestone(time_logs: &[TimeLog]) {
let (table_data, table_header) = tables::populate_table_timelogs_by_milestone(time_logs);
let table_data_cells = to_cell_vec(table_data);
let table = create_table(table_data_cells, table_header);
println!("\nTime Spent per Milestone:");
print_table(table);
}
pub fn print_timelogs_in_timeframes_by_user(time_logs: &[TimeLog]) {
let (table_data, table_header) =
tables::populate_table_timelogs_in_timeframes_by_user(time_logs);
let table_data_cells = to_cell_vec(table_data);
let table = create_table(table_data_cells, table_header);
println!("\nTime Spent per User:");
print_table(table);
}
pub fn print_todays_timelogs(time_logs: &[TimeLog]) {
let (table_data, table_header) = tables::populate_table_todays_timelogs(time_logs);
println!("\nToday's Time Logs:");
if table_data.is_empty() {
println!("No time logs found for today.");
return;
}
let table_data_cells = to_cell_vec(table_data);
let table = create_table(table_data_cells, table_header);
print_table(table);
}