Compare commits

...

7 Commits

Author SHA1 Message Date
andy.boot
2fe91806c7 Increment version 2023-03-14 08:18:23 +00:00
andy.boot
514bb2799c Fix: some panics are occuring when creating rayon
Wrap and ignore these panics.

I can't reproduce this problem but I'm curious to know if this fixes it
2023-03-14 08:15:19 +00:00
Efertone
e17a1af476 remove depth from config.toml and fix style issues
Signed-off-by: Efertone <efertone@pm.me>
2023-03-06 21:43:15 +00:00
Efertone
2f7c197cd7 feat: default option for depth from config file
- If `--depth` flag is not defined (or it has an invalid value), a value
  from the config file will be used.
- If no `depth` entry in the config file (or there is no config file),
  the default `usize::MAX` will be used.

Added test cases:
- no config and no flag defined              -> `usize::MAX` should be used
- config defined, but flag is not defined    -> config value should be used
- config is not defined, but flag is defined -> flag value should be used
- both config and flag is defined            -> flag value should be used

Additional changes:

- Fixed some clippy issues.
- Added comments to the example `config.toml` file.
  (copy from flag description)

Closes: #314

Signed-off-by: Balazs Nadasdi <efertone@pm.me>
2023-03-06 21:43:15 +00:00
andy.boot
7d13fe972c refactor: DisplayData 2023-02-04 11:20:50 +00:00
andy.boot
5a3e15d0ce refactor: simplify filter.rs 2023-02-04 11:20:50 +00:00
andy.boot
6db013a601 Update README.md
more parameters
2023-01-29 10:46:39 +00:00
10 changed files with 136 additions and 73 deletions

2
Cargo.lock generated
View File

@@ -220,7 +220,7 @@ checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
[[package]]
name = "du-dust"
version = "0.8.4"
version = "0.8.5"
dependencies = [
"ansi_term",
"assert_cmd",

View File

@@ -1,7 +1,7 @@
[package]
name = "du-dust"
description = "A more intuitive version of du"
version = "0.8.4"
version = "0.8.5"
authors = ["bootandy <bootandy@gmail.com>", "nebkor <code@ardent.nebcorp.com>"]
edition = "2021"
readme = "README.md"

View File

@@ -64,6 +64,7 @@ Usage: dust -s (apparent-size - shows the length of the file as opposed to the a
Usage: dust -n 30 (Shows 30 directories instead of the default [default is terminal height])
Usage: dust -d 3 (Shows 3 levels of subdirectories)
Usage: dust -D (Show only directories (eg dust -D))
Usage: dust -F (Show only files - finds your largest files)
Usage: dust -r (reverse order of output)
Usage: dust -H (si print sizes in powers of 1000 instead of 1024)
Usage: dust -X ignore (ignore all files and directories with the name 'ignore')
@@ -76,6 +77,11 @@ Usage: dust -t (Group by filetype)
Usage: dust -z 10M (min-size, Only include files larger than 10M)
Usage: dust -e regex (Only include files matching this regex (eg dust -e "\.png$" would match png files))
Usage: dust -v regex (Exclude files matching this regex (eg dust -v "\.png$" would ignore png files))
Usage: dust -L (dereference-links - Treat sym links as directories and go into them)
Usage: dust -P (Disable the progress indicator)
Usage: dust -R (For screen readers. Removes bars/symbols. Adds new column: depth level. (May want to use -p for full path too))
Usage: dust --skip-total (No total row will be displayed)
Usage: dust -z 4000000 (Exclude files below size 4MB)
```

View File

@@ -3,11 +3,26 @@
# ~/.config/dust/config.toml
# ~/.dust.toml
# Print tree upside down (biggest highest)
reverse=true
# Subdirectories will not have their path shortened
display-full-paths=true
# Use file length instead of blocks
display-apparent-size=true
# No colors will be printed
no-colors=true
# No percent bars or percentages will be displayed
no-bars=true
# No total row will be displayed
skip-total=true
# Do not display hidden files
ignore-hidden=true
# print sizes in powers of 1000 (e.g., 1.1G)
iso=true

View File

@@ -1,6 +1,6 @@
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH Dust 1 "Dust 0.8.4"
.TH Dust 1 "Dust 0.8.5"
.SH NAME
Dust \- Like du but more intuitive
.SH SYNOPSIS
@@ -87,4 +87,4 @@ Only files will be displayed. (Finds your largest files)
[\fIinputs\fR]
.SH VERSION
v0.8.4
v0.8.5

View File

@@ -23,6 +23,7 @@ pub struct Config {
pub only_dir: Option<bool>,
pub only_file: Option<bool>,
pub disable_progress: Option<bool>,
pub depth: Option<usize>,
}
impl Config {
@@ -59,6 +60,15 @@ impl Config {
pub fn get_screen_reader(&self, options: &ArgMatches) -> bool {
Some(true) == self.screen_reader || options.is_present("screen_reader")
}
pub fn get_depth(&self, options: &ArgMatches) -> usize {
if let Some(v) = options.value_of("depth") {
if let Ok(v) = v.parse::<usize>() {
return v;
}
}
self.depth.unwrap_or(usize::MAX)
}
pub fn get_min_size(&self, options: &ArgMatches, iso: bool) -> Option<usize> {
let size_from_param = options.value_of("min_size");
self._get_min_size(size_from_param, iso)
@@ -137,9 +147,11 @@ pub fn get_config() -> Config {
}
}
#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;
use clap::{Arg, ArgMatches, Command};
#[test]
fn test_conversion() {
@@ -164,4 +176,40 @@ mod tests {
assert_eq!(c._get_min_size(None, true), Some(1000));
assert_eq!(c._get_min_size(Some("2K"), true), Some(2000));
}
#[test]
fn test_get_depth() {
// No config and no flag.
let c = Config::default();
let args = get_args(vec![]);
assert_eq!(c.get_depth(&args), usize::MAX);
// Config is not defined and flag is defined.
let c = Config::default();
let args = get_args(vec!["dust", "--depth", "5"]);
assert_eq!(c.get_depth(&args), 5);
// Config is defined and flag is not defined.
let c = Config {
depth: Some(3),
..Default::default()
};
let args = get_args(vec![]);
assert_eq!(c.get_depth(&args), 3);
// Both config and flag are defined.
let c = Config {
depth: Some(3),
..Default::default()
};
let args = get_args(vec!["dust", "--depth", "5"]);
assert_eq!(c.get_depth(&args), 5);
}
fn get_args(args: Vec<&str>) -> ArgMatches {
Command::new("Dust")
.trailing_var_arg(true)
.arg(Arg::new("depth").long("depth").takes_value(true))
.get_matches_from(args)
}
}

View File

@@ -17,22 +17,26 @@ use thousands::Separable;
pub static UNITS: [char; 4] = ['T', 'G', 'M', 'K'];
static BLOCKS: [char; 5] = ['█', '▓', '▒', '░', ' '];
pub struct DisplayData {
pub struct InitialDisplayData {
pub short_paths: bool,
pub is_reversed: bool,
pub colors_on: bool,
pub by_filecount: bool,
pub is_screen_reader: bool,
pub iso: bool,
}
pub struct DisplayData {
pub initial: InitialDisplayData,
pub num_chars_needed_on_left_most: usize,
pub base_size: u64,
pub longest_string_length: usize,
pub ls_colors: LsColors,
pub iso: bool,
}
impl DisplayData {
fn get_tree_chars(&self, was_i_last: bool, has_children: bool) -> &'static str {
match (self.is_reversed, was_i_last, has_children) {
match (self.initial.is_reversed, was_i_last, has_children) {
(true, true, true) => "┌─┴",
(true, true, false) => "┌──",
(true, false, true) => "├─┴",
@@ -45,7 +49,7 @@ impl DisplayData {
}
fn is_biggest(&self, num_siblings: usize, max_siblings: u64) -> bool {
if self.is_reversed {
if self.initial.is_reversed {
num_siblings == (max_siblings - 1) as usize
} else {
num_siblings == 0
@@ -53,7 +57,7 @@ impl DisplayData {
}
fn is_last(&self, num_siblings: usize, max_siblings: u64) -> bool {
if self.is_reversed {
if self.initial.is_reversed {
num_siblings == 0
} else {
num_siblings == (max_siblings - 1) as usize
@@ -84,7 +88,7 @@ impl DrawData<'_> {
// TODO: can we test this?
fn generate_bar(&self, node: &DisplayNode, level: usize) -> String {
if self.display_data.is_screen_reader {
if self.display_data.initial.is_screen_reader {
return level.to_string();
}
let chars_in_bar = self.percent_bar.chars().count();
@@ -108,19 +112,12 @@ impl DrawData<'_> {
}
}
// TODO: Push these into one object ?
#[allow(clippy::too_many_arguments)]
pub fn draw_it(
use_full_path: bool,
is_reversed: bool,
no_colors: bool,
idd: InitialDisplayData,
no_percent_bars: bool,
terminal_width: usize,
by_filecount: bool,
root_node: &DisplayNode,
iso: bool,
skip_total: bool,
is_screen_reader: bool,
) {
let biggest = match skip_total {
false => root_node,
@@ -130,11 +127,11 @@ pub fn draw_it(
.unwrap_or(root_node),
};
let num_chars_needed_on_left_most = if by_filecount {
let num_chars_needed_on_left_most = if idd.by_filecount {
let max_size = biggest.size;
max_size.separate_with_commas().chars().count()
} else {
find_biggest_size_str(root_node, iso)
find_biggest_size_str(root_node, idd.iso)
};
assert!(
@@ -144,13 +141,8 @@ pub fn draw_it(
let allowed_width = terminal_width - num_chars_needed_on_left_most - 2;
let num_indent_chars = 3;
let longest_string_length = find_longest_dir_name(
root_node,
num_indent_chars,
allowed_width,
!use_full_path,
is_screen_reader,
);
let longest_string_length =
find_longest_dir_name(root_node, num_indent_chars, allowed_width, &idd);
let max_bar_length = if no_percent_bars || longest_string_length + 7 >= allowed_width {
0
@@ -161,16 +153,11 @@ pub fn draw_it(
let first_size_bar = repeat(BLOCKS[0]).take(max_bar_length).collect();
let display_data = DisplayData {
short_paths: !use_full_path,
is_reversed,
colors_on: !no_colors,
by_filecount,
is_screen_reader,
initial: idd,
num_chars_needed_on_left_most,
base_size: biggest.size,
longest_string_length,
ls_colors: LsColors::from_env().unwrap_or_default(),
iso,
};
let draw_data = DrawData {
indent: "".to_string(),
@@ -182,7 +169,7 @@ pub fn draw_it(
display_node(root_node, &draw_data, true, true);
} else {
for (count, c) in root_node
.get_children_from_node(draw_data.display_data.is_reversed)
.get_children_from_node(draw_data.display_data.initial.is_reversed)
.enumerate()
{
let is_biggest = display_data.is_biggest(count, root_node.num_siblings());
@@ -204,12 +191,11 @@ fn find_longest_dir_name(
node: &DisplayNode,
indent: usize,
terminal: usize,
long_paths: bool,
is_screen_reader: bool,
idd: &InitialDisplayData,
) -> usize {
let printable_name = get_printable_name(&node.name, long_paths);
let printable_name = get_printable_name(&node.name, idd.short_paths);
let longest = if is_screen_reader {
let longest = if idd.is_screen_reader {
UnicodeWidthStr::width(&*printable_name) + 1
} else {
min(
@@ -221,7 +207,7 @@ fn find_longest_dir_name(
// each none root tree drawing is 2 more chars, hence we increment indent by 2
node.children
.iter()
.map(|c| find_longest_dir_name(c, indent + 2, terminal, long_paths, is_screen_reader))
.map(|c| find_longest_dir_name(c, indent + 2, terminal, idd))
.fold(longest, max)
}
@@ -233,7 +219,7 @@ fn display_node(node: &DisplayNode, draw_data: &DrawData, is_biggest: bool, is_l
let to_print = format_string(node, &indent, &bar_text, is_biggest, draw_data.display_data);
if !draw_data.display_data.is_reversed {
if !draw_data.display_data.initial.is_reversed {
println!("{to_print}")
}
@@ -246,7 +232,7 @@ fn display_node(node: &DisplayNode, draw_data: &DrawData, is_biggest: bool, is_l
let num_siblings = node.num_siblings();
for (count, c) in node
.get_children_from_node(draw_data.display_data.is_reversed)
.get_children_from_node(draw_data.display_data.initial.is_reversed)
.enumerate()
{
let is_biggest = dd.display_data.is_biggest(count, num_siblings);
@@ -254,7 +240,7 @@ fn display_node(node: &DisplayNode, draw_data: &DrawData, is_biggest: bool, is_l
display_node(c, &dd, is_biggest, was_i_last);
}
if draw_data.display_data.is_reversed {
if draw_data.display_data.initial.is_reversed {
println!("{to_print}")
}
}
@@ -295,7 +281,7 @@ fn get_printable_name<P: AsRef<Path>>(dir_name: &P, short_paths: bool) -> String
}
fn pad_or_trim_filename(node: &DisplayNode, indent: &str, display_data: &DisplayData) -> String {
let name = get_printable_name(&node.name, display_data.short_paths);
let name = get_printable_name(&node.name, display_data.initial.short_paths);
let indent_and_name = format!("{indent} {name}");
let width = UnicodeWidthStr::width(&*indent_and_name);
@@ -340,7 +326,7 @@ pub fn format_string(
let pretty_size = get_pretty_size(node, is_biggest, display_data);
let pretty_name = get_pretty_name(node, name_and_padding, display_data);
// we can clean this and the method below somehow, not sure yet
if display_data.is_screen_reader {
if display_data.initial.is_screen_reader {
// if screen_reader then bars is 'depth'
format!("{pretty_name} {bars} {pretty_size}{percent}")
} else {
@@ -354,7 +340,7 @@ fn get_name_percent(
bar_chart: &str,
display_data: &DisplayData,
) -> (String, String) {
if display_data.is_screen_reader {
if display_data.initial.is_screen_reader {
let percent = display_data.percent_size(node) * 100.0;
let percent_size_str = format!("{percent:.0}%");
let percents = format!(" {percent_size_str:>4}",);
@@ -368,22 +354,22 @@ fn get_name_percent(
let name_and_padding = pad_or_trim_filename(node, indent, display_data);
(percents, name_and_padding)
} else {
let n = get_printable_name(&node.name, display_data.short_paths);
let n = get_printable_name(&node.name, display_data.initial.short_paths);
let name = maybe_trim_filename(n, indent, display_data);
("".into(), name)
}
}
fn get_pretty_size(node: &DisplayNode, is_biggest: bool, display_data: &DisplayData) -> String {
let output = if display_data.by_filecount {
let output = if display_data.initial.by_filecount {
node.size.separate_with_commas()
} else {
human_readable_number(node.size, display_data.iso)
human_readable_number(node.size, display_data.initial.iso)
};
let spaces_to_add = display_data.num_chars_needed_on_left_most - output.chars().count();
let output = " ".repeat(spaces_to_add) + output.as_str();
if is_biggest && display_data.colors_on {
if is_biggest && display_data.initial.colors_on {
format!("{}", Red.paint(output))
} else {
output
@@ -395,7 +381,7 @@ fn get_pretty_name(
name_and_padding: String,
display_data: &DisplayData,
) -> String {
if display_data.colors_on {
if display_data.initial.colors_on {
let meta_result = fs::metadata(&node.name);
let directory_color = display_data
.ls_colors
@@ -433,17 +419,20 @@ mod tests {
#[cfg(test)]
fn get_fake_display_data(longest_string_length: usize) -> DisplayData {
DisplayData {
let initial = InitialDisplayData {
short_paths: true,
is_reversed: false,
colors_on: false,
by_filecount: false,
is_screen_reader: false,
iso: false,
};
DisplayData {
initial,
num_chars_needed_on_left_most: 5,
base_size: 2_u64.pow(12), // 4.0K
longest_string_length,
ls_colors: LsColors::from_env().unwrap_or_default(),
iso: false,
}
}
@@ -494,7 +483,7 @@ mod tests {
let percent_bar = "3";
let is_biggest = false;
let mut data = get_fake_display_data(20);
data.is_screen_reader = true;
data.initial.is_screen_reader = true;
let s = format_string(&n, indent, percent_bar, is_biggest, &data);
assert_eq!(s, "short 3 4.0K 100%");

View File

@@ -39,14 +39,14 @@ pub fn get_biggest(top_level_nodes: Vec<Node>, display_data: AggregateData) -> O
heap = add_children(&display_data, &root, heap);
}
fill_remaining_lines(heap, &root, display_data)
Some(fill_remaining_lines(heap, &root, display_data))
}
pub fn fill_remaining_lines<'a>(
mut heap: BinaryHeap<&'a Node>,
root: &'a Node,
display_data: AggregateData,
) -> Option<DisplayNode> {
) -> DisplayNode {
let mut allowed_nodes = HashMap::new();
while allowed_nodes.len() < display_data.number_of_lines {
@@ -106,22 +106,19 @@ fn always_add_children<'a>(
}
// 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> {
fn recursive_rebuilder(allowed_nodes: &HashMap<&Path, &Node>, current: &Node) -> DisplayNode {
let new_children: Vec<_> = current
.children
.iter()
.filter(|c| allowed_nodes.contains_key(c.name.as_path()))
.filter_map(|c| recursive_rebuilder(allowed_nodes, c))
.map(|c| recursive_rebuilder(allowed_nodes, c))
.collect();
Some(build_node(new_children, current))
build_node(new_children, current)
}
// Applies all allowed nodes as children to current node
fn flat_rebuilder(allowed_nodes: HashMap<&Path, &Node>, current: &Node) -> Option<DisplayNode> {
fn flat_rebuilder(allowed_nodes: HashMap<&Path, &Node>, current: &Node) -> DisplayNode {
let new_children: Vec<DisplayNode> = allowed_nodes
.into_values()
.map(|v| DisplayNode {
@@ -130,7 +127,7 @@ fn flat_rebuilder(allowed_nodes: HashMap<&Path, &Node>, current: &Node) -> Optio
children: vec![],
})
.collect::<Vec<DisplayNode>>();
Some(build_node(new_children, current))
build_node(new_children, current)
}
fn build_node(mut new_children: Vec<DisplayNode>, current: &Node) -> DisplayNode {

View File

@@ -12,11 +12,13 @@ mod utils;
use crate::cli::build_cli;
use dir_walker::WalkData;
use display::InitialDisplayData;
use filter::AggregateData;
use progress::PIndicator;
use progress::ORDERING;
use std::collections::HashSet;
use std::io::BufRead;
use std::panic;
use std::process;
use sysinfo::{System, SystemExt};
@@ -129,7 +131,7 @@ fn main() {
.value_of_t("width")
.unwrap_or_else(|_| get_width_of_terminal());
let depth = options.value_of_t("depth").unwrap_or(usize::MAX);
let depth = config.get_depth(&options);
// If depth is set, then we set the default number_of_lines to be max
// instead of screen height
@@ -189,7 +191,10 @@ fn main() {
progress_data: indicator.data.clone(),
};
let _rayon = init_rayon();
let result = panic::catch_unwind(|| init_rayon);
if result.is_err() {
eprintln!("Problem initializing rayon, try: export RAYON_NUM_THREADS=1")
}
let top_level_nodes = walk_it(simplified_dirs, walk_data);
@@ -217,17 +222,20 @@ fn main() {
}
if let Some(root_node) = tree {
let idd = InitialDisplayData {
short_paths: !config.get_full_paths(&options),
is_reversed: !config.get_reverse(&options),
colors_on: !no_colors,
by_filecount,
iso,
is_screen_reader: config.get_screen_reader(&options),
};
draw_it(
config.get_full_paths(&options),
!config.get_reverse(&options),
no_colors,
idd,
config.get_no_bars(&options),
terminal_width,
by_filecount,
&root_node,
iso,
config.get_skip_total(&options),
config.get_screen_reader(&options),
)
}
}
@@ -244,6 +252,6 @@ fn init_rayon() -> Result<(), ThreadPoolBuildError> {
.stack_size(large_stack)
.build_global()
} else {
Ok(())
rayon::ThreadPoolBuilder::new().build_global()
}
}

View File

@@ -147,7 +147,7 @@ pub fn test_output_screen_reader() {
assert!(output.contains("a_file 2"));
// Verify no 'symbols' reported by screen reader
assert!(!output.contains(""));
assert!(!output.contains('│'));
for block in ['█', '▓', '▒', '░'] {
assert!(!output.contains(block));