Compare commits

..

1 Commits

Author SHA1 Message Date
andy.boot ba04d4c42c Increment version 2018-04-27 12:44:24 +01:00
8 changed files with 145 additions and 190 deletions
Generated
+1 -1
View File
@@ -121,7 +121,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "du-dust"
version = "0.3.1"
version = "0.2.4"
dependencies = [
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"assert_cli 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
+1 -1
View File
@@ -1,7 +1,7 @@
[package]
name = "du-dust"
description = "A more intuitive version of du"
version = "0.3.1"
version = "0.3.0"
authors = ["bootandy <bootandy@gmail.com>", "nebkor <code@ardent.nebcorp.com>"]
documentation = "https://github.com/bootandy/dust"
+2 -2
View File
@@ -7,11 +7,11 @@ du + rust = dust. Like du but more intuitive
## Install
#### Cargo Install
### Cargo
* cargo install du-dust
#### Download Install
### Download
* Download linux / mac binary from [Releases](https://github.com/bootandy/dust/releases)
* unzip file: tar -xvf _downloaded_file.tar.gz_
+28 -46
View File
@@ -1,7 +1,6 @@
extern crate ansi_term;
use self::ansi_term::Colour::Fixed;
use std::collections::HashSet;
static UNITS: [char; 4] = ['T', 'G', 'M', 'K'];
@@ -9,18 +8,15 @@ pub fn draw_it(
permissions: bool,
short_paths: bool,
depth: Option<u64>,
base_dirs: HashSet<String>,
base_dirs: Vec<String>,
to_display: Vec<(String, u64)>,
) -> () {
if !permissions {
eprintln!("Did not have permissions for all directories");
}
let mut found = HashSet::new();
for &(ref k, _) in to_display.iter() {
if base_dirs.contains(k) {
display_node(&k, &mut found, &to_display, true, short_paths, depth, "─┬")
}
for f in base_dirs {
display_node(f, &to_display, true, short_paths, depth, "")
}
}
@@ -34,59 +30,65 @@ fn get_size(nodes: &Vec<(String, u64)>, node_to_print: &String) -> Option<u64> {
}
fn display_node<S: Into<String>>(
node_to_print: &String,
found: &mut HashSet<String>,
node_to_print: String,
to_display: &Vec<(String, u64)>,
is_biggest: bool,
short_paths: bool,
depth: Option<u64>,
indentation_str: S,
) {
if found.contains(node_to_print) {
return;
}
found.insert(node_to_print.to_string());
let new_depth = match depth {
None => None,
Some(0) => return,
Some(d) => Some(d - 1),
};
match get_size(to_display, node_to_print) {
match get_size(to_display, &node_to_print) {
None => println!("Can not find path: {}", node_to_print),
Some(size) => {
let is = indentation_str.into();
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());
let new_indent_str = clean_indentation_string(is);
let num_slashes = node_to_print.matches('/').count();
let mut num_siblings = count_siblings(to_display, num_slashes, ntp);
is = is.replace("└─┬", " ");
is = is.replace("└──", " ");
is = is.replace("├──", "");
is = is.replace("├─┬", "");
let printable_node_slashes = node_to_print.matches('/').count();
let mut num_siblings = to_display.iter().fold(0, |a, b| {
if b.0.starts_with(ntp) && b.0.matches('/').count() == printable_node_slashes + 1 {
a + 1
} else {
a
}
});
let mut is_biggest = true;
for &(ref k, _) in to_display.iter() {
if k.starts_with(ntp) && k.matches('/').count() == num_slashes + 1 {
if k.starts_with(ntp) && k.matches('/').count() == printable_node_slashes + 1 {
num_siblings -= 1;
let mut has_children = false;
if new_depth.is_none() || new_depth.unwrap() != 1 {
for &(ref k2, _) in to_display.iter() {
let kk: &str = k.as_ref();
if k2.starts_with(kk) && k2.matches('/').count() == num_slashes + 2 {
if k2.starts_with(kk)
&& k2.matches('/').count() == printable_node_slashes + 2
{
has_children = true;
}
}
}
display_node(
k,
found,
k.to_string(),
to_display,
is_biggest,
short_paths,
new_depth,
new_indent_str.to_string() + get_tree_chars(num_siblings, has_children),
is.to_string() + get_tree_chars(num_siblings, has_children),
);
is_biggest = false;
}
@@ -95,26 +97,6 @@ fn display_node<S: Into<String>>(
}
}
fn clean_indentation_string<S: Into<String>>(s: S) -> String {
let mut is = s.into();
is = is.replace("└─┬", " ");
is = is.replace("└──", " ");
is = is.replace("├──", "");
is = is.replace("├─┬", "");
is = is.replace("─┬", " ");
is
}
fn count_siblings(to_display: &Vec<(String, u64)>, num_slashes: usize, ntp: &str) -> u64 {
to_display.iter().fold(0, |a, b| {
if b.0.starts_with(ntp) && b.0.matches('/').count() == num_slashes + 1 {
a + 1
} else {
a
}
})
}
fn get_tree_chars(num_siblings: u64, has_children: bool) -> &'static str {
if num_siblings == 0 {
if has_children {
@@ -159,8 +141,8 @@ pub fn format_string(
indentation: &str,
) -> String {
let printable_name = {
if short_paths {
dir_name.split('/').last().unwrap_or(dir_name)
if short_paths && dir_name.contains('/') {
dir_name.split('/').last().unwrap()
} else {
dir_name
}
+14 -21
View File
@@ -5,15 +5,15 @@ extern crate walkdir;
use self::display::draw_it;
use clap::{App, AppSettings, Arg};
use utils::{find_big_ones, get_dir_tree, simplify_dir_names, sort};
use std::io::{self, Write};
use utils::{find_big_ones, get_dir_tree, sort};
mod display;
mod utils;
static DEFAULT_NUMBER_OF_LINES: usize = 20;
static DEFAULT_NUMBER_OF_LINES: &'static str = "20";
fn main() {
let def_num_str = DEFAULT_NUMBER_OF_LINES.to_string();
let options = App::new("Dust")
.setting(AppSettings::TrailingVarArg)
.arg(
@@ -29,7 +29,7 @@ fn main() {
.long("number-of-lines")
.help("Number of lines of output to show")
.takes_value(true)
.default_value(def_num_str.as_ref()),
.default_value(DEFAULT_NUMBER_OF_LINES),
)
.arg(
Arg::with_name("display_full_paths")
@@ -46,44 +46,37 @@ fn main() {
.arg(Arg::with_name("inputs").multiple(true))
.get_matches();
let target_dirs = {
let filenames = {
match options.values_of("inputs") {
None => vec!["."],
Some(r) => r.collect(),
}
};
let number_of_lines = match value_t!(options.value_of("number_of_lines"), usize) {
Ok(v) => v,
Err(_) => {
eprintln!("Ignoring bad value for number_of_lines");
DEFAULT_NUMBER_OF_LINES
}
};
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(_) => {
eprintln!("Ignoring bad value for depth");
None
}
Err(_) => None,
}
} else {
None
}
};
if options.is_present("depth") && number_of_lines != DEFAULT_NUMBER_OF_LINES {
eprintln!("Use either -n or -d. Not both");
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_full_path = options.is_present("display_full_paths");
let simplified_dirs = simplify_dir_names(target_dirs);
let (permissions, nodes, top_level_names) = get_dir_tree(simplified_dirs, use_apparent_size);
let (permissions, nodes, top_level_names) = get_dir_tree(&filenames, use_apparent_size);
let sorted_data = sort(nodes);
let biggest_ones = {
if depth.is_none() {
+28 -36
View File
@@ -28,15 +28,6 @@ pub fn test_main_long_paths() {
.unwrap();
}
#[test]
pub fn test_main_multi_arg() {
assert_cli::Assert::main_binary()
.with_args(&["src/test_dir/many/", "src/test_dir/", "src/test_dir"])
.stdout()
.is(main_output(true))
.unwrap();
}
#[cfg(target_os = "macos")]
fn main_output(short_paths: bool) -> String {
format!(
@@ -44,21 +35,21 @@ fn main_output(short_paths: bool) -> String {
{}
{}
{}",
format_string("src/test_dir", true, short_paths, " 4.0K", "─┬"),
format_string("src/test_dir/many", true, short_paths, " 4.0K", " └─┬",),
format_string("src/test_dir", true, short_paths, " 4.0K", ""),
format_string("src/test_dir/many", true, short_paths, " 4.0K", "└─┬",),
format_string(
"src/test_dir/many/hello_file",
true,
short_paths,
" 4.0K",
" ├──",
" ├──",
),
format_string(
"src/test_dir/many/a_file",
false,
short_paths,
" 0B",
" └──",
" └──",
),
)
}
@@ -70,21 +61,21 @@ fn main_output(short_paths: bool) -> String {
{}
{}
{}",
format_string("src/test_dir", true, short_paths, " 12K", "─┬"),
format_string("src/test_dir/many", true, short_paths, " 8.0K", " └─┬",),
format_string("src/test_dir", true, short_paths, " 12K", ""),
format_string("src/test_dir/many", true, short_paths, " 8.0K", "└─┬",),
format_string(
"src/test_dir/many/hello_file",
true,
short_paths,
" 4.0K",
" ├──",
" ├──",
),
format_string(
"src/test_dir/many/a_file",
false,
short_paths,
" 0B",
" └──",
" └──",
),
)
}
@@ -98,7 +89,7 @@ pub fn test_apparent_size() {
true,
true,
" 6B",
" ├──",
" ├──",
),
);
@@ -149,9 +140,9 @@ fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> String {
"{}
{}
{}",
format_string(dir, true, true, " 8.0K", "─┬"),
format_string(file_path, true, true, " 4.0K", " ├──",),
format_string(link_name, false, true, " 4.0K", " └──",),
format_string(dir, true, true, " 8.0K", ""),
format_string(file_path, true, true, " 4.0K", "├──",),
format_string(link_name, false, true, " 4.0K", "└──",),
)
}
@@ -161,9 +152,9 @@ fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> String {
"{}
{}
{}",
format_string(dir, true, true, " 8.0K", "─┬"),
format_string(file_path, true, true, " 4.0K", " ├──",),
format_string(link_name, false, true, " 0B", " └──",),
format_string(dir, true, true, " 8.0K", ""),
format_string(file_path, true, true, " 4.0K", "├──",),
format_string(link_name, false, true, " 0B", "└──",),
)
}
@@ -209,14 +200,14 @@ fn hard_link_output(dir_s: &str, file_path_s: &str, link_name_s: &str) -> (Strin
let r = format!(
"{}
{}",
format_string(dir_s, true, true, " 4.0K", "─┬"),
format_string(file_path_s, true, true, " 4.0K", " └──")
format_string(dir_s, true, true, " 4.0K", ""),
format_string(file_path_s, true, true, " 4.0K", "└──")
);
let r2 = format!(
"{}
{}",
format_string(dir_s, true, true, " 4.0K", "─┬"),
format_string(link_name_s, true, true, " 4.0K", " └──")
format_string(dir_s, true, true, " 4.0K", ""),
format_string(link_name_s, true, true, " 4.0K", "└──")
);
(r, r2)
}
@@ -226,14 +217,14 @@ fn hard_link_output(dir_s: &str, file_path_s: &str, link_name_s: &str) -> (Strin
let r = format!(
"{}
{}",
format_string(dir_s, true, true, " 8.0K", "─┬"),
format_string(file_path_s, true, true, " 4.0K", " └──")
format_string(dir_s, true, true, " 8.0K", ""),
format_string(file_path_s, true, true, " 4.0K", "└──")
);
let r2 = format!(
"{}
{}",
format_string(dir_s, true, true, " 8.0K", "─┬"),
format_string(link_name_s, true, true, " 4.0K", " └──")
format_string(dir_s, true, true, " 8.0K", ""),
format_string(link_name_s, true, true, " 4.0K", "└──")
);
(r, r2)
}
@@ -266,8 +257,8 @@ fn recursive_sym_link_output(dir: &str, link_name: &str) -> String {
format!(
"{}
{}",
format_string(dir, true, true, " 4.0K", "─┬"),
format_string(link_name, true, true, " 4.0K", " └──",),
format_string(dir, true, true, " 4.0K", ""),
format_string(link_name, true, true, " 4.0K", "└──",),
)
}
#[cfg(target_os = "linux")]
@@ -275,8 +266,9 @@ fn recursive_sym_link_output(dir: &str, link_name: &str) -> String {
format!(
"{}
{}",
format_string(dir, true, true, " 4.0K", "─┬"),
format_string(link_name, true, true, " 0B", " └──",),
format_string(dir, true, true, " 4.0K", ""),
format_string(link_name, true, true, " 0B", "└──",),
)
}
// TODO: add test for bad path
+21 -71
View File
@@ -1,49 +1,34 @@
use std::cmp::Ordering;
use std::collections::HashMap;
use std::collections::HashSet;
use std::cmp::Ordering;
use walkdir::WalkDir;
use std::path::Path;
use std::path::PathBuf;
mod platform;
use self::platform::*;
pub fn simplify_dir_names(filenames: Vec<&str>) -> HashSet<String> {
let mut top_level_names: HashSet<String> = HashSet::new();
for t in filenames {
let top_level_name = strip_end_slashes(t);
let mut can_add = true;
let mut to_remove: Vec<String> = Vec::new();
for tt in top_level_names.iter() {
let temp = tt.to_string();
if top_level_name.starts_with(&temp) {
can_add = false;
} else if tt.starts_with(&top_level_name) {
to_remove.push(temp);
}
}
for tr in to_remove {
top_level_names.remove(&tr);
}
if can_add {
top_level_names.insert(top_level_name);
}
}
top_level_names
}
pub fn get_dir_tree(
top_level_names: HashSet<String>,
filenames: &Vec<&str>,
apparent_size: bool,
) -> (bool, HashMap<String, u64>, HashSet<String>) {
) -> (bool, HashMap<String, u64>, Vec<String>) {
let mut permissions = 0;
let mut inodes: HashSet<(u64, u64)> = HashSet::new();
let mut data: HashMap<String, u64> = HashMap::new();
let mut top_level_names = Vec::new();
for b in top_level_names.iter() {
examine_dir(&b, apparent_size, &mut inodes, &mut data, &mut permissions);
for b in filenames {
let top_level_name = strip_end_slashes(b);
examine_dir(
&Path::new(&top_level_name).to_path_buf(),
apparent_size,
&mut inodes,
&mut data,
&mut permissions,
);
top_level_names.push(top_level_name);
}
(permissions == 0, data, top_level_names)
}
@@ -57,7 +42,7 @@ fn strip_end_slashes(s: &str) -> String {
}
fn examine_dir(
top_dir: &String,
top_dir: &PathBuf,
apparent_size: bool,
inodes: &mut HashSet<(u64, u64)>,
data: &mut HashMap<String, u64>,
@@ -81,9 +66,9 @@ fn examine_dir(
let mut e_path = e.path().to_path_buf();
loop {
let path_name = e_path.to_string_lossy().to_string();
let s = data.entry(path_name.clone()).or_insert(0);
let s = data.entry(path_name).or_insert(0);
*s += size;
if path_name == *top_dir {
if e_path == *top_dir {
break;
}
e_path.pop();
@@ -96,7 +81,7 @@ fn examine_dir(
}
}
}
pub fn compare_tuple(a: &(String, u64), b: &(String, u64)) -> Ordering {
pub fn compare_tuple(a :&(String, u64), b: &(String, u64)) -> Ordering {
let result = b.1.cmp(&a.1);
if result == Ordering::Equal {
a.0.cmp(&b.0)
@@ -118,38 +103,3 @@ pub fn find_big_ones<'a>(new_l: Vec<(String, u64)>, max_to_show: usize) -> Vec<(
new_l
}
}
mod tests {
#[allow(unused_imports)]
use super::*;
#[test]
fn test_simplify_dir() {
let mut correct = HashSet::new();
correct.insert("a".to_string());
assert!(simplify_dir_names(vec!["a"]) == correct);
}
#[test]
fn test_simplify_dir_rm_subdir() {
let mut correct = HashSet::new();
correct.insert("a/b".to_string());
assert!(simplify_dir_names(vec!["a/b", "a/b/c", "a/b/d/f"]) == correct);
}
#[test]
fn test_simplify_dir_duplicates() {
let mut correct = HashSet::new();
correct.insert("a/b".to_string());
correct.insert("c".to_string());
assert!(simplify_dir_names(vec!["a/b", "a/b//", "c", "c/"]) == correct);
}
#[test]
fn test_simplify_dir_rm_subdir_and_not_substrings() {
let mut correct = HashSet::new();
correct.insert("a/b".to_string());
correct.insert("c/a/b".to_string());
correct.insert("b".to_string());
assert!(simplify_dir_names(vec!["a/b", "c/a/b/", "b"]) == correct);
}
}
+50 -12
View File
@@ -6,20 +6,58 @@ fn get_block_size() -> u64 {
512
}
#[cfg(target_family = "unix")]
#[cfg(target_os = "linux")]
pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> {
use std::os::unix::fs::MetadataExt;
d.metadata().ok().map_or(None, |md| {
let inode = Some((md.ino(), md.dev()));
if use_apparent_size {
Some((md.len(), inode))
} else {
Some((md.blocks() * get_block_size(), inode))
use std::os::linux::fs::MetadataExt;
match d.metadata().ok() {
Some(md) => {
let inode = Some((md.st_ino(), md.st_dev()));
if use_apparent_size {
Some((md.len(), inode))
} else {
Some((md.st_blocks() * get_block_size(), inode))
}
}
})
None => None,
}
}
#[cfg(not(target_family = "unix"))]
pub fn get_metadata(d: &DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, u64)>)> {
d.metadata().ok().map_or(None, |md| Some((md.len(), None)))
#[cfg(target_os = "unix")]
pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> {
use std::os::unix::fs::MetadataExt;
match d.metadata().ok() {
Some(md) => {
let inode = Some((md.ino(), md.dev()));
if use_apparent_size {
Some((md.len(), inode))
} else {
Some((md.blocks() * get_block_size(), inode))
}
}
None => None,
}
}
#[cfg(target_os = "macos")]
pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> {
use std::os::macos::fs::MetadataExt;
match d.metadata().ok() {
Some(md) => {
let inode = Some((md.st_ino(), md.st_dev()));
if use_apparent_size {
Some((md.len(), inode))
} else {
Some((md.st_blocks() * get_block_size(), inode))
}
}
None => None,
}
}
#[cfg(not(any(target_os = "linux", target_os = "unix", target_os = "macos")))]
pub fn get_metadata(d: &DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, u64)>)> {
match d.metadata().ok() {
Some(md) => Some((md.len(), None)),
None => None,
}
}