forked from KSXGitHub/parallel-disk-usage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.rs
More file actions
112 lines (94 loc) · 3.82 KB
/
methods.rs
File metadata and controls
112 lines (94 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
mod bar_table;
mod constants;
mod initial_table;
mod node_info;
mod table;
mod tree_table;
use bar_table::*;
use constants::*;
use initial_table::*;
use node_info::*;
use table::*;
use tree_table::*;
use super::{coloring::maybe_color_slice, ColumnWidthDistribution, Visualizer};
use crate::size;
use std::{cmp::min, ffi::OsStr, fmt::Display};
use zero_copy_pads::{align_left, align_right};
impl<'a, Name, Size> Visualizer<'a, Name, Size>
where
Name: Display + AsRef<OsStr>,
Size: size::Size + Into<u64>,
{
/// Create ASCII rows that visualize the [tree](crate::data_tree::DataTree), such rows
/// are meant to be printed to a terminal screen.
pub fn rows(mut self) -> Vec<String> {
let initial_table = render_initial(self);
let min_width = initial_table.column_width.total_max_width();
let (tree_table, bar_width) = match self.column_width_distribution {
ColumnWidthDistribution::Total { width } => {
let extra_cols = 3; // make space for tree_column to minimize second-time re-rendering.
if width <= min_width {
self.column_width_distribution
.set_components(min_width, extra_cols);
return self.rows();
}
if width <= MIN_OVERALL_WIDTH {
self.column_width_distribution
.set_components(min_width, MIN_OVERALL_WIDTH + extra_cols);
return self.rows();
}
let tree_max_width = min(width - min_width, width - MIN_OVERALL_WIDTH);
let tree_table = render_tree(self, initial_table, tree_max_width);
let min_width = tree_table.column_width.total_max_width();
if width <= min_width {
self.column_width_distribution.set_components(min_width, 1);
return self.rows();
}
let bar_width = width - min_width;
(tree_table, bar_width)
}
ColumnWidthDistribution::Components {
tree_column_max_width,
bar_column_width,
} => {
if bar_column_width < 1 {
self.column_width_distribution
.set_components(tree_column_max_width, 1);
return self.rows();
}
let tree_table = render_tree(self, initial_table, tree_column_max_width);
(tree_table, bar_column_width)
}
};
let size_width = tree_table.column_width.size_column_width;
let tree_width = tree_table.column_width.tree_column_width;
let bar_table = render_bars(tree_table, self.data_tree.size().into(), bar_width);
bar_table
.into_iter()
.map(|row| {
let BarRow {
tree_row,
proportion_bar,
} = row;
let TreeRow {
initial_row,
tree_horizontal_slice,
} = tree_row;
let tree = maybe_color_slice(
self.coloring,
initial_row.ancestors.iter().map(|node| node.name.as_ref()),
initial_row.node_info.name.as_ref(),
initial_row.node_info.children_count > 0,
tree_horizontal_slice,
);
format!(
"{size} {tree}│{bar}│{ratio}",
size = align_right(&initial_row.size, size_width),
tree = align_left(tree, tree_width),
bar = proportion_bar.display(self.bar_alignment),
ratio = align_right(&initial_row.percentage, PERCENTAGE_COLUMN_MAX_WIDTH),
)
})
.collect()
}
}