mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
Add support for -d depth flag
Following a user request the option '-d N' allows a user to output N levels of sub directories. Fixed bug: so that trailing slashes are now removed.
This commit is contained in:
+29
-22
@@ -7,7 +7,8 @@ static UNITS: [char; 4] = ['T', 'G', 'M', 'K'];
|
|||||||
pub fn draw_it(
|
pub fn draw_it(
|
||||||
permissions: bool,
|
permissions: bool,
|
||||||
short_paths: bool,
|
short_paths: bool,
|
||||||
base_dirs: Vec<&str>,
|
depth: Option<u64>,
|
||||||
|
base_dirs: Vec<String>,
|
||||||
to_display: Vec<(String, u64)>,
|
to_display: Vec<(String, u64)>,
|
||||||
) -> () {
|
) -> () {
|
||||||
if !permissions {
|
if !permissions {
|
||||||
@@ -15,13 +16,13 @@ pub fn draw_it(
|
|||||||
}
|
}
|
||||||
|
|
||||||
for f in base_dirs {
|
for f in base_dirs {
|
||||||
display_node(f, &to_display, true, short_paths, "")
|
display_node(f, &to_display, true, short_paths, depth, "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_size(nodes: &Vec<(String, u64)>, node_to_print: String) -> Option<u64> {
|
fn get_size(nodes: &Vec<(String, u64)>, node_to_print: &String) -> Option<u64> {
|
||||||
for &(ref k, ref v) in nodes.iter() {
|
for &(ref k, ref v) in nodes.iter() {
|
||||||
if *k == node_to_print {
|
if *k == *node_to_print {
|
||||||
return Some(*v);
|
return Some(*v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,18 +30,25 @@ fn get_size(nodes: &Vec<(String, u64)>, node_to_print: String) -> Option<u64> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn display_node<S: Into<String>>(
|
fn display_node<S: Into<String>>(
|
||||||
node_to_print: &str,
|
node_to_print: String,
|
||||||
to_display: &Vec<(String, u64)>,
|
to_display: &Vec<(String, u64)>,
|
||||||
is_biggest: bool,
|
is_biggest: bool,
|
||||||
short_paths: bool,
|
short_paths: bool,
|
||||||
|
depth: Option<u64>,
|
||||||
indentation_str: S,
|
indentation_str: S,
|
||||||
) {
|
) {
|
||||||
let mut is = indentation_str.into();
|
let new_depth = match depth {
|
||||||
let size = get_size(to_display, node_to_print.to_string());
|
None => None,
|
||||||
match size {
|
Some(0) => return,
|
||||||
|
Some(d) => Some(d - 1),
|
||||||
|
};
|
||||||
|
match get_size(to_display, &node_to_print) {
|
||||||
None => println!("Can not find path: {}", node_to_print),
|
None => println!("Can not find path: {}", node_to_print),
|
||||||
Some(size) => {
|
Some(size) => {
|
||||||
print_this_node(node_to_print, size, is_biggest, short_paths, is.as_ref());
|
let mut is = indentation_str.into();
|
||||||
|
let ntp: &str = node_to_print.as_ref();
|
||||||
|
|
||||||
|
print_this_node(ntp, size, is_biggest, short_paths, is.as_ref());
|
||||||
|
|
||||||
is = is.replace("└─┬", " ");
|
is = is.replace("└─┬", " ");
|
||||||
is = is.replace("└──", " ");
|
is = is.replace("└──", " ");
|
||||||
@@ -50,9 +58,7 @@ fn display_node<S: Into<String>>(
|
|||||||
let printable_node_slashes = node_to_print.matches('/').count();
|
let printable_node_slashes = node_to_print.matches('/').count();
|
||||||
|
|
||||||
let mut num_siblings = to_display.iter().fold(0, |a, b| {
|
let mut num_siblings = to_display.iter().fold(0, |a, b| {
|
||||||
if b.0.starts_with(node_to_print)
|
if b.0.starts_with(ntp) && b.0.matches('/').count() == printable_node_slashes + 1 {
|
||||||
&& b.0.matches('/').count() == printable_node_slashes + 1
|
|
||||||
{
|
|
||||||
a + 1
|
a + 1
|
||||||
} else {
|
} else {
|
||||||
a
|
a
|
||||||
@@ -61,26 +67,27 @@ fn display_node<S: Into<String>>(
|
|||||||
|
|
||||||
let mut is_biggest = true;
|
let mut is_biggest = true;
|
||||||
for &(ref k, _) in to_display.iter() {
|
for &(ref k, _) in to_display.iter() {
|
||||||
if k.starts_with(node_to_print)
|
if k.starts_with(ntp) && k.matches('/').count() == printable_node_slashes + 1 {
|
||||||
&& k.matches('/').count() == printable_node_slashes + 1
|
|
||||||
{
|
|
||||||
num_siblings -= 1;
|
num_siblings -= 1;
|
||||||
|
|
||||||
let mut has_children = false;
|
let mut has_children = false;
|
||||||
for &(ref k2, _) in to_display.iter() {
|
if new_depth.is_none() || new_depth.unwrap() != 1 {
|
||||||
let kk :&str = k.as_ref();
|
for &(ref k2, _) in to_display.iter() {
|
||||||
if k2.starts_with(kk)
|
let kk: &str = k.as_ref();
|
||||||
&& k2.matches('/').count() == printable_node_slashes + 2
|
if k2.starts_with(kk)
|
||||||
{
|
&& k2.matches('/').count() == printable_node_slashes + 2
|
||||||
has_children = true;
|
{
|
||||||
|
has_children = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
display_node(
|
display_node(
|
||||||
&k,
|
k.to_string(),
|
||||||
to_display,
|
to_display,
|
||||||
is_biggest,
|
is_biggest,
|
||||||
short_paths,
|
short_paths,
|
||||||
|
new_depth,
|
||||||
is.to_string() + get_tree_chars(num_siblings, has_children),
|
is.to_string() + get_tree_chars(num_siblings, has_children),
|
||||||
);
|
);
|
||||||
is_biggest = false;
|
is_biggest = false;
|
||||||
|
|||||||
+44
-4
@@ -5,7 +5,8 @@ extern crate walkdir;
|
|||||||
|
|
||||||
use self::display::draw_it;
|
use self::display::draw_it;
|
||||||
use clap::{App, AppSettings, Arg};
|
use clap::{App, AppSettings, Arg};
|
||||||
use utils::{find_big_ones, get_dir_tree};
|
use std::io::{self, Write};
|
||||||
|
use utils::{find_big_ones, get_dir_tree, sort};
|
||||||
|
|
||||||
mod display;
|
mod display;
|
||||||
mod utils;
|
mod utils;
|
||||||
@@ -15,6 +16,12 @@ static DEFAULT_NUMBER_OF_LINES: &'static str = "15";
|
|||||||
fn main() {
|
fn main() {
|
||||||
let options = App::new("Dust")
|
let options = App::new("Dust")
|
||||||
.setting(AppSettings::TrailingVarArg)
|
.setting(AppSettings::TrailingVarArg)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("depth")
|
||||||
|
.short("d")
|
||||||
|
.help("Depth to show")
|
||||||
|
.takes_value(true),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("number_of_lines")
|
Arg::with_name("number_of_lines")
|
||||||
.short("n")
|
.short("n")
|
||||||
@@ -41,13 +48,46 @@ fn main() {
|
|||||||
Some(r) => r.collect(),
|
Some(r) => r.collect(),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let number_of_lines = value_t!(options.value_of("number_of_lines"), usize).unwrap();
|
let number_of_lines = value_t!(options.value_of("number_of_lines"), usize).unwrap();
|
||||||
|
let depth = {
|
||||||
|
if options.is_present("depth") {
|
||||||
|
match value_t!(options.value_of("depth"), u64) {
|
||||||
|
Ok(v) => Some(v + 1),
|
||||||
|
Err(_) => None,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if options.is_present("depth")
|
||||||
|
&& options.value_of("number_of_lines").unwrap() != DEFAULT_NUMBER_OF_LINES
|
||||||
|
{
|
||||||
|
io::stderr()
|
||||||
|
.write(b"Use either -n for number of directories to show. Or -d for depth. Not both")
|
||||||
|
.expect("Error writing to stderr. Oh the irony!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let use_apparent_size = options.is_present("display_apparent_size");
|
let use_apparent_size = options.is_present("display_apparent_size");
|
||||||
let use_full_path = options.is_present("display_full_paths");
|
let use_full_path = options.is_present("display_full_paths");
|
||||||
|
|
||||||
let (permissions, nodes) = get_dir_tree(&filenames, use_apparent_size);
|
let (permissions, nodes, top_level_names) = get_dir_tree(&filenames, use_apparent_size);
|
||||||
let biggest_ones = find_big_ones(nodes, number_of_lines);
|
let sorted_data = sort(nodes);
|
||||||
draw_it(permissions, !use_full_path, filenames, biggest_ones);
|
let biggest_ones = {
|
||||||
|
if depth.is_none() {
|
||||||
|
find_big_ones(sorted_data, number_of_lines)
|
||||||
|
} else {
|
||||||
|
sorted_data
|
||||||
|
}
|
||||||
|
};
|
||||||
|
draw_it(
|
||||||
|
permissions,
|
||||||
|
!use_full_path,
|
||||||
|
depth,
|
||||||
|
top_level_names,
|
||||||
|
biggest_ones,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
+23
-5
@@ -9,20 +9,35 @@ use std::path::PathBuf;
|
|||||||
mod platform;
|
mod platform;
|
||||||
use self::platform::*;
|
use self::platform::*;
|
||||||
|
|
||||||
pub fn get_dir_tree(filenames: &Vec<&str>, apparent_size: bool) -> (bool, HashMap<String, u64>) {
|
pub fn get_dir_tree(
|
||||||
|
filenames: &Vec<&str>,
|
||||||
|
apparent_size: bool,
|
||||||
|
) -> (bool, HashMap<String, u64>, Vec<String>) {
|
||||||
let mut permissions = 0;
|
let mut permissions = 0;
|
||||||
let mut inodes: HashSet<(u64, u64)> = HashSet::new();
|
let mut inodes: HashSet<(u64, u64)> = HashSet::new();
|
||||||
let mut data: HashMap<String, u64> = HashMap::new();
|
let mut data: HashMap<String, u64> = HashMap::new();
|
||||||
|
let mut top_level_names = Vec::new();
|
||||||
|
|
||||||
for b in filenames {
|
for b in filenames {
|
||||||
|
let top_level_name = strip_end_slashes(b);
|
||||||
examine_dir(
|
examine_dir(
|
||||||
&Path::new(b).to_path_buf(),
|
&Path::new(&top_level_name).to_path_buf(),
|
||||||
apparent_size,
|
apparent_size,
|
||||||
&mut inodes,
|
&mut inodes,
|
||||||
&mut data,
|
&mut data,
|
||||||
&mut permissions,
|
&mut permissions,
|
||||||
);
|
);
|
||||||
|
top_level_names.push(top_level_name);
|
||||||
}
|
}
|
||||||
(permissions == 0, data)
|
(permissions == 0, data, top_level_names)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn strip_end_slashes(s: &str) -> String {
|
||||||
|
let mut new_name = String::from(s);
|
||||||
|
while new_name.chars().last() == Some('/') && new_name.len() != 1 {
|
||||||
|
new_name.pop();
|
||||||
|
}
|
||||||
|
new_name
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examine_dir(
|
fn examine_dir(
|
||||||
@@ -66,11 +81,14 @@ fn examine_dir(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_big_ones<'a>(data: HashMap<String, u64>, max_to_show: usize) -> Vec<(String, u64)> {
|
pub fn sort<'a>(data: HashMap<String, u64>) -> Vec<(String, u64)> {
|
||||||
let mut new_l: Vec<(String, u64)> = data.iter().map(|(a, b)| (a.clone(), *b)).collect();
|
let mut new_l: Vec<(String, u64)> = data.iter().map(|(a, b)| (a.clone(), *b)).collect();
|
||||||
new_l.sort_by(|a, b| b.1.cmp(&a.1));
|
new_l.sort_by(|a, b| b.1.cmp(&a.1));
|
||||||
|
new_l
|
||||||
|
}
|
||||||
|
|
||||||
if new_l.len() > max_to_show {
|
pub fn find_big_ones<'a>(new_l: Vec<(String, u64)>, max_to_show: usize) -> Vec<(String, u64)> {
|
||||||
|
if max_to_show > 0 && new_l.len() > max_to_show {
|
||||||
new_l[0..max_to_show + 1].to_vec()
|
new_l[0..max_to_show + 1].to_vec()
|
||||||
} else {
|
} else {
|
||||||
new_l
|
new_l
|
||||||
|
|||||||
Reference in New Issue
Block a user