Skip to content

Prune bug #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 29, 2023
Merged
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
2 changes: 1 addition & 1 deletion src/render/context/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
disk_usage::{file_size::DiskUsage, unit::PrefixKind},
disk_usage::{file_size::DiskUsage, units::PrefixKind},
order::SortType,
};
use clap::{
Expand Down
7 changes: 4 additions & 3 deletions src/render/disk_usage/file_size.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::unit::{BinPrefix, PrefixKind, SiPrefix, UnitPrefix};
use super::units::{BinPrefix, PrefixKind, SiPrefix, UnitPrefix};
use crate::{render::styles::get_du_theme, Context};
use clap::ValueEnum;
use filesize::PathExt;
Expand Down Expand Up @@ -122,6 +122,7 @@ impl FileSize {
pub fn human_readable_components(&self) -> HumanReadableComponents {
let fbytes = self.bytes as f64;
let scale = self.scale;
let power = u32::try_from(scale).unwrap();

let (size, unit) = match self.prefix_kind {
PrefixKind::Bin => {
Expand All @@ -132,7 +133,7 @@ impl FileSize {
(format!("{}", self.bytes), format!("{unit}"))
} else {
// Checks if the `scale` provided results in a value that implies fractional bytes.
if self.bytes <= 10_u64.pow(scale as u32) {
if self.bytes <= 10_u64.pow(power) {
(format!("{}", self.bytes), format!("{}", BinPrefix::Base))
} else {
(
Expand All @@ -151,7 +152,7 @@ impl FileSize {
(format!("{}", self.bytes), format!("{unit}"))
} else {
// Checks if the `scale` provided results in a value that implies fractional bytes.
if 10_u64.pow(scale as u32) >= base_value {
if 10_u64.pow(power) >= base_value {
(format!("{}", self.bytes), format!("{}", SiPrefix::Base))
} else {
(
Expand Down
2 changes: 1 addition & 1 deletion src/render/disk_usage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Binary and SI prefixes
pub mod unit;
pub mod units;

/// Rules to display disk usage for individual files
pub mod file_size;
File renamed without changes.
33 changes: 15 additions & 18 deletions src/render/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,24 @@ impl Tree {
for node_id in root_id.descendants(tree) {
let node = tree[node_id].get();

if node.is_dir() && node_id.children(tree).peekable().peek().is_none() {
if !node.is_dir() {
continue;
}

if node_id.children(tree).count() == 0 {
to_prune.push(node_id);
}
}

if to_prune.is_empty() {
return;
}

for node_id in to_prune {
node_id.remove_subtree(tree);
}

Self::prune_directories(root_id, tree);
}
}

Expand Down Expand Up @@ -247,22 +257,8 @@ impl Display for Tree {

let root_node = inner[root].get();

fn display_node(
node: &Node,
base_prefix: &str,
ctx: &Context,
f: &mut Formatter<'_>,
) -> fmt::Result {
if ctx.size_left && !ctx.suppress_size {
node.display_size_left(f, base_prefix, ctx)?;
} else {
node.display_size_right(f, base_prefix, ctx)?;
}

writeln!(f)
}

display_node(root_node, "", ctx, f)?;
root_node.display(f, "", ctx)?;
writeln!(f)?;

let mut prefix_components = vec![""];

Expand Down Expand Up @@ -290,7 +286,8 @@ impl Display for Tree {
let prefix = current_prefix_components.join("");

if current_node.depth <= level {
display_node(current_node, &prefix, ctx, f)?;
current_node.display(f, &prefix, ctx)?;
writeln!(f)?;
}

if let Some(next_id) = descendants.peek() {
Expand Down
60 changes: 27 additions & 33 deletions src/render/tree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Node {

/// Stylizes input, `entity` based on [`LS_COLORS`]
///
/// [`LS_COLORS`]: crate::render::styles::::LS_COLORS
/// [`LS_COLORS`]: crate::render::styles::LS_COLORS
fn stylize(&self, entity: &str) -> String {
self.style().foreground.map_or_else(
|| entity.to_string(),
Expand All @@ -188,14 +188,22 @@ impl Node {
/// General method for printing a `Node`. The `Display` (and `ToString`) traits are not used,
/// to give more control over the output.
///
/// See [`Node::display_size_left`] and [`Node::display_size_right`] for examples of formatted output.
fn display(
&self,
f: &mut Formatter,
size_loc: SizeLocation,
prefix: &str,
ctx: &Context,
) -> fmt::Result {
/// Format a node for display with size on the right.
///
/// Example:
/// `| Some Directory (12.3 KiB)`
///
///
/// Format a node for display with size on the left.
///
/// Example:
/// ` 1.23 MiB | Some File`
///
/// Note the two spaces to the left of the first character of the number -- even if never used,
/// numbers are padded to 3 digits to the left of the decimal (and ctx.scale digits after)
pub fn display(&self, f: &mut Formatter, prefix: &str, ctx: &Context) -> fmt::Result {
let size_loc = SizeLocation::from(ctx);

let size = self.file_size().map_or_else(
|| size_loc.default_string(ctx),
|size| size_loc.format(size),
Expand Down Expand Up @@ -224,30 +232,6 @@ impl Node {
}
}

/// Format a node for display with size on the right.
///
/// Example:
/// `| Some Directory (12.3 KiB)`
pub fn display_size_right(
&self,
f: &mut Formatter,
prefix: &str,
ctx: &Context,
) -> fmt::Result {
self.display(f, SizeLocation::Right, prefix, ctx)
}

/// Format a node for display with size on the left.
///
/// Example:
/// ` 1.23 MiB | Some File`
///
/// Note the two spaces to the left of the first character of the number -- even if never used,
/// numbers are padded to 3 digits to the left of the decimal (and ctx.scale digits after)
pub fn display_size_left(&self, f: &mut Formatter, prefix: &str, ctx: &Context) -> fmt::Result {
self.display(f, SizeLocation::Left, prefix, ctx)
}

/// Unix file identifiers that you'd find in the `ls -l` command.
#[cfg(unix)]
pub fn file_type_identifier(&self) -> Option<&str> {
Expand Down Expand Up @@ -398,3 +382,13 @@ impl SizeLocation {
}
}
}

impl From<&Context> for SizeLocation {
fn from(ctx: &Context) -> Self {
if ctx.size_left && !ctx.suppress_size {
Self::Left
} else {
Self::Right
}
}
}
2 changes: 1 addition & 1 deletion src/render/tree/report.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{node::Node, Tree};
use crate::render::disk_usage::{
file_size::{FileSize, HumanReadableComponents},
unit::PrefixKind,
units::PrefixKind,
};
use std::{
convert::AsRef,
Expand Down