Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ required-features = ["cli"]
[features]
default = ["cli"]
json = ["serde/derive", "serde_json"]
cli = ["clap/derive", "clap_complete", "clap-utilities", "json"]
cli = ["clap/derive", "clap_complete", "clap-utilities", "json", "lscolors"]
cli-completions = ["cli"]

[dependencies]
Expand All @@ -65,6 +65,7 @@ derive_setters = "0.1.9"
fmt-iter = "0.2.1"
into-sorted = "0.0.3"
itertools = "0.14.0"
lscolors = { version = "0.21.0", optional = true }
pipe-trait = "0.4.0"
rayon = "1.10.0"
rounded-div = "0.1.4"
Expand All @@ -83,3 +84,4 @@ maplit = "1.0.2"
normalize-path = "0.2.1"
pretty_assertions = "1.4.1"
rand = "0.10.0"
strip-ansi-escapes = "0.2.1"
11 changes: 11 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ Do not output `.shared.details` in the JSON output.

Do not output `.shared.summary` in the JSON output.

<a id="color" name="color"></a>
### `--color`

* _Default:_ `auto`.
* _Choices:_
- `auto`: Colorize output only when stdout is a terminal
- `always`: Always colorize the output
- `never`: Never colorize the output

When to colorize the output.

<a id="option-h" name="option-h"></a><a id="help" name="help"></a>
### `--help`

Expand Down
6 changes: 5 additions & 1 deletion exports/completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ _pdu() {

case "${cmd}" in
pdu)
opts="-b -H -q -d -w -m -s -p -h -V --json-input --json-output --bytes-format --detect-links --dedupe-links --deduplicate-hardlinks --top-down --align-right --quantity --depth --max-depth --width --total-width --column-width --min-ratio --no-sort --no-errors --silent-errors --progress --threads --omit-json-shared-details --omit-json-shared-summary --help --version [FILES]..."
opts="-b -H -q -d -w -m -s -p -h -V --json-input --json-output --bytes-format --detect-links --dedupe-links --deduplicate-hardlinks --top-down --align-right --quantity --depth --max-depth --width --total-width --column-width --min-ratio --no-sort --no-errors --silent-errors --progress --threads --omit-json-shared-details --omit-json-shared-summary --color --help --version [FILES]..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down Expand Up @@ -85,6 +85,10 @@ _pdu() {
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--color)
COMPREPLY=($(compgen -W "auto always never" -- "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
Expand Down
1 change: 1 addition & 0 deletions exports/completion.elv
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set edit:completion:arg-completer[pdu] = {|@words|
cand -m 'Minimal size proportion required to appear'
cand --min-ratio 'Minimal size proportion required to appear'
cand --threads 'Set the maximum number of threads to spawn. Could be either "auto", "max", or a positive integer'
cand --color 'When to colorize the output'
cand --json-input 'Read JSON data from stdin'
cand --json-output 'Print JSON data instead of an ASCII chart'
cand -H 'Detect and subtract the sizes of hardlinks from their parent directory totals'
Expand Down
3 changes: 3 additions & 0 deletions exports/completion.fish
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ complete -c pdu -s w -l total-width -l width -d 'Width of the visualization' -r
complete -c pdu -l column-width -d 'Maximum widths of the tree column and width of the bar column' -r
complete -c pdu -s m -l min-ratio -d 'Minimal size proportion required to appear' -r
complete -c pdu -l threads -d 'Set the maximum number of threads to spawn. Could be either "auto", "max", or a positive integer' -r
complete -c pdu -l color -d 'When to colorize the output' -r -f -a "auto\t'Colorize output only when stdout is a terminal'
always\t'Always colorize the output'
never\t'Never colorize the output'"
complete -c pdu -l json-input -d 'Read JSON data from stdin'
complete -c pdu -l json-output -d 'Print JSON data instead of an ASCII chart'
complete -c pdu -s H -l deduplicate-hardlinks -l detect-links -l dedupe-links -d 'Detect and subtract the sizes of hardlinks from their parent directory totals'
Expand Down
1 change: 1 addition & 0 deletions exports/completion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Register-ArgumentCompleter -Native -CommandName 'pdu' -ScriptBlock {
[CompletionResult]::new('-m', '-m', [CompletionResultType]::ParameterName, 'Minimal size proportion required to appear')
[CompletionResult]::new('--min-ratio', '--min-ratio', [CompletionResultType]::ParameterName, 'Minimal size proportion required to appear')
[CompletionResult]::new('--threads', '--threads', [CompletionResultType]::ParameterName, 'Set the maximum number of threads to spawn. Could be either "auto", "max", or a positive integer')
[CompletionResult]::new('--color', '--color', [CompletionResultType]::ParameterName, 'When to colorize the output')
[CompletionResult]::new('--json-input', '--json-input', [CompletionResultType]::ParameterName, 'Read JSON data from stdin')
[CompletionResult]::new('--json-output', '--json-output', [CompletionResultType]::ParameterName, 'Print JSON data instead of an ASCII chart')
[CompletionResult]::new('-H', '-H ', [CompletionResultType]::ParameterName, 'Detect and subtract the sizes of hardlinks from their parent directory totals')
Expand Down
3 changes: 3 additions & 0 deletions exports/completion.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ block-count\:"Count numbers of blocks"))' \
'-m+[Minimal size proportion required to appear]:MIN_RATIO:_default' \
'--min-ratio=[Minimal size proportion required to appear]:MIN_RATIO:_default' \
'--threads=[Set the maximum number of threads to spawn. Could be either "auto", "max", or a positive integer]:THREADS:_default' \
'--color=[When to colorize the output]:COLOR:((auto\:"Colorize output only when stdout is a terminal"
always\:"Always colorize the output"
never\:"Never colorize the output"))' \
'(-q --quantity -H --deduplicate-hardlinks)--json-input[Read JSON data from stdin]' \
'--json-output[Print JSON data instead of an ASCII chart]' \
'-H[Detect and subtract the sizes of hardlinks from their parent directory totals]' \
Expand Down
10 changes: 10 additions & 0 deletions exports/long.help
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ Options:
--omit-json-shared-summary
Do not output `.shared.summary` in the JSON output

--color <COLOR>
When to colorize the output

Possible values:
- auto: Colorize output only when stdout is a terminal
- always: Always colorize the output
- never: Never colorize the output

[default: auto]

-h, --help
Print help (see a summary with '-h')

Expand Down
2 changes: 2 additions & 0 deletions exports/short.help
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Options:
Do not output `.shared.details` in the JSON output
--omit-json-shared-summary
Do not output `.shared.summary` in the JSON output
--color <COLOR>
When to colorize the output [default: auto] [possible values: auto, always, never]
-h, --help
Print help (see more with '--help')
-V, --version
Expand Down
3 changes: 3 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl App {
column_width_distribution,
direction,
bar_alignment,
coloring: None,
};

let JsonShared { details, summary } = shared;
Expand Down Expand Up @@ -291,6 +292,7 @@ impl App {
max_depth,
min_ratio,
no_sort,
color,
omit_json_shared_details,
omit_json_shared_summary,
..
Expand All @@ -307,6 +309,7 @@ impl App {
max_depth,
min_ratio,
no_sort,
color,
}
.run(),
)*} };
Expand Down
21 changes: 20 additions & 1 deletion src/app/sub.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
args::{Depth, Fraction},
args::{ColorOption, Depth, Fraction},
coloring::Coloring,
data_tree::DataTree,
fs_tree_builder::FsTreeBuilder,
get_size::GetSize,
Expand Down Expand Up @@ -49,6 +50,8 @@ where
pub min_ratio: Fraction,
/// Preserve order of entries.
pub no_sort: bool,
/// When to colorize the output.
pub color: ColorOption,
}

impl<Size, SizeGetter, HardlinksHandler, Report> Sub<Size, SizeGetter, HardlinksHandler, Report>
Expand All @@ -74,6 +77,7 @@ where
reporter,
min_ratio,
no_sort,
color,
} = self;

let max_depth = max_depth.get();
Expand Down Expand Up @@ -187,12 +191,27 @@ where
.or(deduplication_result);
}

// Build color map AFTER pruning to save CPU/IO cycles
let coloring = match color {
ColorOption::Always => Some(Coloring::from_tree(&data_tree)),
ColorOption::Never => None,
ColorOption::Auto => {
use std::io::IsTerminal;
if std::io::stdout().is_terminal() {
Some(Coloring::from_tree(&data_tree))
} else {
None
}
}
};

let visualizer = Visualizer {
data_tree: &data_tree,
bytes_format,
direction,
bar_alignment,
column_width_distribution,
coloring: coloring.as_ref(),
};

print!("{visualizer}"); // visualizer already ends with "\n", println! isn't needed here.
Expand Down
6 changes: 6 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub mod color_option;
pub mod depth;
pub mod fraction;
pub mod quantity;
pub mod threads;

pub use color_option::ColorOption;
pub use depth::Depth;
pub use fraction::Fraction;
pub use quantity::Quantity;
Expand Down Expand Up @@ -170,6 +172,10 @@ pub struct Args {
/// Do not output `.shared.summary` in the JSON output.
#[clap(long, requires = "json_output", requires = "deduplicate_hardlinks")]
pub omit_json_shared_summary: bool,

/// When to colorize the output.
#[clap(long, value_enum, default_value_t = ColorOption::Auto)]
pub color: ColorOption,
}

impl Args {
Expand Down
15 changes: 15 additions & 0 deletions src/args/color_option.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[cfg(feature = "cli")]
use clap::ValueEnum;

/// When to colorize the output.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "cli", derive(ValueEnum))]
pub enum ColorOption {
/// Colorize output only when stdout is a terminal.
#[default]
Auto,
/// Always colorize the output.
Always,
/// Never colorize the output.
Never,
}
Loading