feature: --files-only -F

Add feature to only show files skipping directories.
This commit is contained in:
andy.boot
2023-01-28 17:54:02 +00:00
parent 520c439edc
commit 1495251ebc
11 changed files with 81 additions and 22 deletions
+9 -1
View File
@@ -146,11 +146,19 @@ pub fn build_cli() -> Command<'static> {
.long("no-progress")
.help("Disable the progress indication."),
)
.arg(Arg::new("inputs").multiple_occurrences(true))
.arg(
Arg::new("only_dir")
.short('D')
.long("only-dir")
.conflicts_with("only_file")
.help("Only directories will be displayed."),
)
.arg(
Arg::new("only_file")
.short('F')
.long("only-file")
.conflicts_with("only_dir")
.help("Only files will be displayed. (Finds your largest files)"),
)
.arg(Arg::new("inputs").multiple_occurrences(true))
}
+8 -1
View File
@@ -20,6 +20,7 @@ pub struct Config {
pub iso: Option<bool>,
pub min_size: Option<String>,
pub only_dir: Option<bool>,
pub only_file: Option<bool>,
pub disable_progress: Option<bool>,
}
@@ -37,7 +38,10 @@ impl Config {
Some(true) == self.ignore_hidden || options.is_present("ignore_hidden")
}
pub fn get_full_paths(&self, options: &ArgMatches) -> bool {
Some(true) == self.display_full_paths || options.is_present("display_full_paths")
// If we are only showing files, always show full paths
Some(true) == self.display_full_paths
|| options.is_present("display_full_paths")
|| self.get_only_file(options)
}
pub fn get_reverse(&self, options: &ArgMatches) -> bool {
Some(true) == self.reverse || options.is_present("reverse")
@@ -69,6 +73,9 @@ impl Config {
pub fn get_only_dir(&self, options: &ArgMatches) -> bool {
Some(true) == self.only_dir || options.is_present("only_dir")
}
pub fn get_only_file(&self, options: &ArgMatches) -> bool {
Some(true) == self.only_file || options.is_present("only_file")
}
}
fn convert_min_size(input: &str, iso: bool) -> Option<usize> {
+40 -16
View File
@@ -1,13 +1,14 @@
use crate::display_node::DisplayNode;
use crate::node::Node;
use std::collections::BinaryHeap;
use std::collections::HashSet;
use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;
pub struct AggregateData {
pub min_size: Option<usize>,
pub only_dir: bool,
pub only_file: bool,
pub number_of_lines: usize,
pub depth: usize,
pub using_a_filter: bool,
@@ -38,31 +39,34 @@ pub fn get_biggest(top_level_nodes: Vec<Node>, display_data: AggregateData) -> O
heap = add_children(&display_data, &root, heap);
}
let nol = display_data.number_of_lines;
let remaining = nol.saturating_sub(number_top_level_nodes);
fill_remaining_lines(heap, &root, remaining, display_data)
fill_remaining_lines(heap, &root, display_data)
}
pub fn fill_remaining_lines<'a>(
mut heap: BinaryHeap<&'a Node>,
root: &'a Node,
remaining_lines: usize,
display_data: AggregateData,
) -> Option<DisplayNode> {
let mut allowed_nodes = HashSet::new();
allowed_nodes.insert(root.name.as_path());
let mut allowed_nodes = HashMap::new();
for _ in 0..remaining_lines {
while allowed_nodes.len() < display_data.number_of_lines {
let line = heap.pop();
match line {
Some(line) => {
allowed_nodes.insert(line.name.as_path());
if !display_data.only_file || line.children.is_empty() {
allowed_nodes.insert(line.name.as_path(), line);
}
heap = add_children(&display_data, line, heap);
}
None => break,
}
}
recursive_rebuilder(&allowed_nodes, root)
if display_data.only_file {
flat_rebuilder(allowed_nodes, root)
} else {
recursive_rebuilder(&allowed_nodes, root)
}
}
fn add_children<'a>(
@@ -101,19 +105,39 @@ fn always_add_children<'a>(
heap
}
fn recursive_rebuilder(allowed_nodes: &HashSet<&Path>, current: &Node) -> Option<DisplayNode> {
let mut new_children: Vec<_> = current
// Finds children of current, if in allowed_nodes adds them as children to new DisplayNode
fn recursive_rebuilder(
allowed_nodes: &HashMap<&Path, &Node>,
current: &Node,
) -> Option<DisplayNode> {
let new_children: Vec<_> = current
.children
.iter()
.filter(|c| allowed_nodes.contains(c.name.as_path()))
.filter(|c| allowed_nodes.contains_key(c.name.as_path()))
.filter_map(|c| recursive_rebuilder(allowed_nodes, c))
.collect();
new_children.sort_by(|lhs, rhs| lhs.cmp(rhs).reverse());
Some(build_node(new_children, current))
}
Some(DisplayNode {
// Applies all allowed nodes as children to current node
fn flat_rebuilder(allowed_nodes: HashMap<&Path, &Node>, current: &Node) -> Option<DisplayNode> {
let new_children: Vec<DisplayNode> = allowed_nodes
.into_values()
.map(|v| DisplayNode {
name: v.name.clone(),
size: v.size,
children: vec![],
})
.collect::<Vec<DisplayNode>>();
Some(build_node(new_children, current))
}
fn build_node(mut new_children: Vec<DisplayNode>, current: &Node) -> DisplayNode {
new_children.sort_by(|lhs, rhs| lhs.cmp(rhs).reverse());
DisplayNode {
name: current.name.clone(),
size: current.size,
children: new_children,
})
}
}
+1
View File
@@ -199,6 +199,7 @@ fn main() {
let agg_data = AggregateData {
min_size: config.get_min_size(&options, iso),
only_dir: config.get_only_dir(&options),
only_file: config.get_only_file(&options),
number_of_lines,
depth,
using_a_filter: options.values_of("filter").is_some()