gitlab_time_report_cli/
print_table.rs

1//! Contains functions to print the generated tables from the library to the console.
2
3#![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
11/// Create a table from cells and column titles.
12fn 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
20/// Print a table and handle errors
21fn 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
28/// Transform a `Vec<Vec<String>>` from the Library to a `Vec<Vec<CellStruct>>`
29/// used by the `cli_table` crate. The first column is left-aligned, all others right-aligned.
30fn 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
45/// Print a table with the total time spent per selected label.
46pub 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
60/// Print a table with the total time spent per milestone.
61pub 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
70/// Print a table of today's, yesterday's, this week's and this month's time spent by user.
71pub 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    // Convert the returned data to the correct format used with cli_table.
76    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}