mirror of
https://github.com/bootandy/dust.git
synced 2025-12-05 20:40:11 -08:00
refactor: pull out more methods, remove unused Option
This commit is contained in:
@@ -125,9 +125,9 @@ impl DrawData<'_> {
|
||||
|
||||
pub fn draw_it(
|
||||
idd: InitialDisplayData,
|
||||
root_node: &DisplayNode,
|
||||
no_percent_bars: bool,
|
||||
terminal_width: usize,
|
||||
root_node: &DisplayNode,
|
||||
skip_total: bool,
|
||||
) {
|
||||
let num_chars_needed_on_left_most = if idd.by_filecount {
|
||||
|
||||
@@ -25,49 +25,44 @@ pub fn get_biggest(
|
||||
display_data: AggregateData,
|
||||
by_filetime: &Option<FileTime>,
|
||||
keep_collapsed: HashSet<PathBuf>,
|
||||
) -> Option<DisplayNode> {
|
||||
if top_level_nodes.is_empty() {
|
||||
// perhaps change this, bring back Error object?
|
||||
return None;
|
||||
}
|
||||
) -> DisplayNode {
|
||||
let mut heap = BinaryHeap::new();
|
||||
let number_top_level_nodes = top_level_nodes.len();
|
||||
let root;
|
||||
|
||||
if number_top_level_nodes > 1 {
|
||||
let size = if by_filetime.is_some() {
|
||||
top_level_nodes
|
||||
.iter()
|
||||
.map(|node| node.size)
|
||||
.max()
|
||||
.unwrap_or(0)
|
||||
} else {
|
||||
top_level_nodes.iter().map(|node| node.size).sum()
|
||||
};
|
||||
|
||||
let nodes = handle_duplicate_top_level_names(top_level_nodes, display_data.short_paths);
|
||||
|
||||
root = Node {
|
||||
name: PathBuf::from("(total)"),
|
||||
size,
|
||||
children: nodes,
|
||||
inode_device: None,
|
||||
depth: 0,
|
||||
};
|
||||
|
||||
// Always include the base nodes if we add a 'parent' (total) node
|
||||
heap = always_add_children(&display_data, &root, heap);
|
||||
if number_top_level_nodes == 0 {
|
||||
root = total_node_builder(0, vec![])
|
||||
} else {
|
||||
root = top_level_nodes.into_iter().next().unwrap();
|
||||
if number_top_level_nodes > 1 {
|
||||
let size = if by_filetime.is_some() {
|
||||
top_level_nodes
|
||||
.iter()
|
||||
.map(|node| node.size)
|
||||
.max()
|
||||
.unwrap_or(0)
|
||||
} else {
|
||||
top_level_nodes.iter().map(|node| node.size).sum()
|
||||
};
|
||||
|
||||
let nodes = handle_duplicate_top_level_names(top_level_nodes, display_data.short_paths);
|
||||
root = total_node_builder(size, nodes);
|
||||
} else {
|
||||
root = top_level_nodes.into_iter().next().unwrap();
|
||||
}
|
||||
heap = add_children(&display_data, &root, heap);
|
||||
}
|
||||
|
||||
Some(fill_remaining_lines(
|
||||
heap,
|
||||
&root,
|
||||
display_data,
|
||||
keep_collapsed,
|
||||
))
|
||||
fill_remaining_lines(heap, &root, display_data, keep_collapsed)
|
||||
}
|
||||
|
||||
fn total_node_builder(size: u64, children: Vec<Node>) -> Node {
|
||||
Node {
|
||||
name: PathBuf::from("(total)"),
|
||||
size,
|
||||
children,
|
||||
inode_device: None,
|
||||
depth: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fill_remaining_lines<'a>(
|
||||
|
||||
@@ -15,7 +15,7 @@ pub fn get_all_file_types(
|
||||
top_level_nodes: &[Node],
|
||||
n: usize,
|
||||
by_filetime: &Option<FileTime>,
|
||||
) -> Option<DisplayNode> {
|
||||
) -> DisplayNode {
|
||||
let ext_nodes = {
|
||||
let mut extension_cumulative_sizes = HashMap::new();
|
||||
build_by_all_file_types(top_level_nodes, &mut extension_cumulative_sizes);
|
||||
@@ -67,13 +67,11 @@ pub fn get_all_file_types(
|
||||
displayed.iter().map(|node| node.size).sum()
|
||||
};
|
||||
|
||||
let result = DisplayNode {
|
||||
DisplayNode {
|
||||
name: PathBuf::from("(total)"),
|
||||
size: actual_size,
|
||||
children: displayed,
|
||||
};
|
||||
|
||||
Some(result)
|
||||
}
|
||||
}
|
||||
|
||||
fn build_by_all_file_types<'a>(
|
||||
|
||||
73
src/main.rs
73
src/main.rs
@@ -11,6 +11,9 @@ mod progress;
|
||||
mod utils;
|
||||
|
||||
use crate::cli::Cli;
|
||||
use crate::config::Config;
|
||||
use crate::display_node::DisplayNode;
|
||||
use crate::node::FileTime;
|
||||
use crate::progress::RuntimeErrors;
|
||||
use clap::Parser;
|
||||
use dir_walker::WalkData;
|
||||
@@ -298,32 +301,52 @@ fn main() {
|
||||
let print_errors = config.get_print_errors(&options);
|
||||
print_any_errors(print_errors, walk_data.errors);
|
||||
|
||||
if let Some(root_node) = tree {
|
||||
if config.get_output_json(&options) {
|
||||
OUTPUT_TYPE.with(|wrapped| {
|
||||
wrapped.replace(output_format);
|
||||
});
|
||||
println!("{}", serde_json::to_string(&root_node).unwrap());
|
||||
} else {
|
||||
let idd = InitialDisplayData {
|
||||
short_paths: !config.get_full_paths(&options),
|
||||
is_reversed: !config.get_reverse(&options),
|
||||
colors_on: is_colors,
|
||||
by_filecount,
|
||||
by_filetime,
|
||||
is_screen_reader: config.get_screen_reader(&options),
|
||||
output_format,
|
||||
bars_on_right: config.get_bars_on_right(&options),
|
||||
};
|
||||
print_output(
|
||||
config,
|
||||
options,
|
||||
tree,
|
||||
walk_data.by_filecount,
|
||||
by_filetime,
|
||||
is_colors,
|
||||
terminal_width,
|
||||
)
|
||||
}
|
||||
|
||||
draw_it(
|
||||
idd,
|
||||
config.get_no_bars(&options),
|
||||
terminal_width,
|
||||
&root_node,
|
||||
config.get_skip_total(&options),
|
||||
)
|
||||
}
|
||||
fn print_output(
|
||||
config: Config,
|
||||
options: Cli,
|
||||
tree: DisplayNode,
|
||||
by_filecount: bool,
|
||||
by_filetime: Option<FileTime>,
|
||||
is_colors: bool,
|
||||
terminal_width: usize,
|
||||
) {
|
||||
let output_format = config.get_output_format(&options);
|
||||
|
||||
if config.get_output_json(&options) {
|
||||
OUTPUT_TYPE.with(|wrapped| {
|
||||
wrapped.replace(output_format);
|
||||
});
|
||||
println!("{}", serde_json::to_string(&tree).unwrap());
|
||||
} else {
|
||||
let idd = InitialDisplayData {
|
||||
short_paths: !config.get_full_paths(&options),
|
||||
is_reversed: !config.get_reverse(&options),
|
||||
colors_on: is_colors,
|
||||
by_filecount,
|
||||
by_filetime,
|
||||
is_screen_reader: config.get_screen_reader(&options),
|
||||
output_format,
|
||||
bars_on_right: config.get_bars_on_right(&options),
|
||||
};
|
||||
|
||||
draw_it(
|
||||
idd,
|
||||
&tree,
|
||||
config.get_no_bars(&options),
|
||||
terminal_width,
|
||||
config.get_skip_total(&options),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user