mi6_cli/display/
table.rs

1//! Table rendering utilities for column widths and headers.
2
3use super::terminal::get_terminal_width;
4
5// Column widths for table formatting
6pub const COL_TIME: usize = 19;
7pub const COL_APP: usize = 3;
8pub const COL_EVENT: usize = 18;
9pub const COL_SESSION: usize = 12;
10pub const COL_PID: usize = 7;
11pub const COL_SOURCE: usize = 4;
12pub const COL_DETAILS_MIN: usize = 10;
13
14/// Calculate the details column width based on terminal width and whether APP column is shown.
15pub fn calculate_details_width(show_app: bool) -> usize {
16    let term_width = get_terminal_width();
17    let fixed_width = if show_app {
18        COL_TIME + 2 + COL_APP + 2 + COL_EVENT + 2 + COL_SESSION + 2 + COL_PID + 2 + COL_SOURCE + 2
19    } else {
20        COL_TIME + 2 + COL_EVENT + 2 + COL_SESSION + 2 + COL_PID + 2 + COL_SOURCE + 2
21    };
22    let available = term_width.saturating_sub(fixed_width);
23    available.max(COL_DETAILS_MIN)
24}
25
26/// Print the table header.
27pub fn print_table_header(show_app: bool, details_width: usize) {
28    if show_app {
29        println!(
30            "{:<COL_TIME$}  {:<COL_APP$}  {:<COL_EVENT$}  {:<COL_SESSION$}  {:<COL_PID$}  {:<COL_SOURCE$}  DETAILS",
31            "TIME", "APP", "EVENT", "SESSION", "PID", "SRC"
32        );
33        let total_width = COL_TIME
34            + 2
35            + COL_APP
36            + 2
37            + COL_EVENT
38            + 2
39            + COL_SESSION
40            + 2
41            + COL_PID
42            + 2
43            + COL_SOURCE
44            + 2
45            + details_width;
46        println!("{}", "-".repeat(total_width));
47    } else {
48        println!(
49            "{:<COL_TIME$}  {:<COL_EVENT$}  {:<COL_SESSION$}  {:<COL_PID$}  {:<COL_SOURCE$}  DETAILS",
50            "TIME", "EVENT", "SESSION", "PID", "SRC"
51        );
52        let total_width = COL_TIME
53            + 2
54            + COL_EVENT
55            + 2
56            + COL_SESSION
57            + 2
58            + COL_PID
59            + 2
60            + COL_SOURCE
61            + 2
62            + details_width;
63        println!("{}", "-".repeat(total_width));
64    }
65}