parallel_disk_usage/visualizer/
methods.rs

1mod bar_table;
2mod constants;
3mod initial_table;
4mod node_info;
5mod table;
6mod tree_table;
7
8use bar_table::*;
9use constants::*;
10use initial_table::*;
11use node_info::*;
12use table::*;
13use tree_table::*;
14
15use super::{ColumnWidthDistribution, Visualizer};
16use crate::size;
17use std::{cmp::min, fmt::Display};
18use zero_copy_pads::{align_left, align_right};
19
20impl<'a, Name, Size> Visualizer<'a, Name, Size>
21where
22    Name: Display,
23    Size: size::Size + Into<u64>,
24{
25    /// Create ASCII rows that visualize of the [tree](crate::data_tree::DataTree), such rows
26    /// are meant to be printed to a terminal screen.
27    pub fn rows(mut self) -> Vec<String> {
28        let initial_table = render_initial(self);
29        let min_width = initial_table.column_width.total_max_width();
30
31        let (tree_table, bar_width) = match self.column_width_distribution {
32            ColumnWidthDistribution::Total { width } => {
33                let extra_cols = 3; // make space for tree_column to minimize second-time re-rendering.
34
35                if width <= min_width {
36                    self.column_width_distribution
37                        .set_components(min_width, extra_cols);
38                    return self.rows();
39                }
40
41                if width <= MIN_OVERALL_WIDTH {
42                    self.column_width_distribution
43                        .set_components(min_width, MIN_OVERALL_WIDTH + extra_cols);
44                    return self.rows();
45                }
46
47                let tree_max_width = min(width - min_width, width - MIN_OVERALL_WIDTH);
48                let tree_table = render_tree(self, initial_table, tree_max_width);
49
50                let min_width = tree_table.column_width.total_max_width();
51                if width <= min_width {
52                    self.column_width_distribution.set_components(min_width, 1);
53                    return self.rows();
54                }
55
56                let bar_width = width - min_width;
57
58                (tree_table, bar_width)
59            }
60
61            ColumnWidthDistribution::Components {
62                tree_column_max_width,
63                bar_column_width,
64            } => {
65                if bar_column_width < 1 {
66                    self.column_width_distribution
67                        .set_components(tree_column_max_width, 1);
68                    return self.rows();
69                }
70
71                let tree_table = render_tree(self, initial_table, tree_column_max_width);
72
73                (tree_table, bar_column_width)
74            }
75        };
76
77        let size_width = tree_table.column_width.size_column_width;
78        let tree_width = tree_table.column_width.tree_column_width;
79
80        let bar_table = render_bars(tree_table, self.data_tree.size().into(), bar_width);
81
82        bar_table
83            .into_iter()
84            .map(|row| {
85                format!(
86                    "{size} {tree}│{bar}│{ratio}",
87                    size = align_right(&row.size, size_width),
88                    tree = align_left(&row.tree_horizontal_slice, tree_width),
89                    bar = row.proportion_bar.display(self.bar_alignment),
90                    ratio = align_right(&row.percentage, PERCENTAGE_COLUMN_MAX_WIDTH),
91                )
92            })
93            .collect()
94    }
95}