gitlab_time_report_cli/
print_table.rs1#![cfg(not(tarpaulin_include))]
4
5use cli_table::format::Justify;
6use cli_table::{Cell, CellStruct, Style, Table, TableStruct, print_stdout};
7use gitlab_time_report::model::{Label, TimeLog};
8use gitlab_time_report::tables;
9use std::collections::HashSet;
10
11fn create_table(table_data: Vec<Vec<CellStruct>>, column_titles: &[&str]) -> TableStruct {
13 let titles: Vec<_> = column_titles
14 .iter()
15 .map(|title| title.cell().bold(true))
16 .collect();
17 table_data.table().title(titles)
18}
19
20fn print_table(table: TableStruct) {
22 print_stdout(table).unwrap_or_else(|err| {
23 eprintln!("Error printing table: {err}");
24 std::process::exit(4);
25 });
26}
27
28fn to_cell_vec(table: Vec<Vec<String>>) -> Vec<Vec<CellStruct>> {
31 table
32 .into_iter()
33 .map(|row| {
34 row.into_iter()
35 .enumerate()
36 .map(|(i, cell)| match i {
37 0 => cell.cell(),
38 _ => cell.cell().justify(Justify::Right),
39 })
40 .collect()
41 })
42 .collect()
43}
44
45pub fn print_total_time_by_label(
47 time_logs: &[TimeLog],
48 label_filter: Option<&HashSet<String>>,
49 label_others: Option<&Label>,
50) {
51 let (table_data, table_header) =
52 tables::populate_table_timelogs_by_label(time_logs, label_filter, label_others);
53 let table_data_cells = to_cell_vec(table_data);
54
55 let table = create_table(table_data_cells, table_header);
56 println!("\nTime Spent per Label:");
57 print_table(table);
58}
59
60pub fn print_total_time_by_milestone(time_logs: &[TimeLog]) {
62 let (table_data, table_header) = tables::populate_table_timelogs_by_milestone(time_logs);
63 let table_data_cells = to_cell_vec(table_data);
64
65 let table = create_table(table_data_cells, table_header);
66 println!("\nTime Spent per Milestone:");
67 print_table(table);
68}
69
70pub fn print_timelogs_in_timeframes_by_user(time_logs: &[TimeLog]) {
72 let (table_data, table_header) =
73 tables::populate_table_timelogs_in_timeframes_by_user(time_logs);
74
75 let table_data_cells = to_cell_vec(table_data);
77
78 let table = create_table(table_data_cells, table_header);
79 println!("\nTime Spent per User:");
80 print_table(table);
81}