Skip to content
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

Refactors app initialisation #214

Merged
merged 20 commits into from
Jan 9, 2024
15 changes: 8 additions & 7 deletions src/aggregate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{crossdev, InodeFilter, Throttle, WalkOptions, WalkResult};
use crate::{crossdev, ByteFormat, InodeFilter, Throttle, WalkOptions, WalkResult};
use anyhow::Result;
use filesize::PathExt;
use owo_colors::{AnsiColors as Color, OwoColorize};
Expand All @@ -14,6 +14,7 @@ pub fn aggregate(
walk_options: WalkOptions,
compute_total: bool,
sort_by_size_in_bytes: bool,
byte_format: ByteFormat,
paths: impl IntoIterator<Item = impl AsRef<Path>>,
) -> Result<(WalkResult, Statistics)> {
let mut res = WalkResult::default();
Expand Down Expand Up @@ -89,11 +90,11 @@ pub fn aggregate(
} else {
output_colored_path(
&mut out,
&walk_options,
&path,
num_bytes,
num_errors,
path_color_of(&path),
byte_format,
)?;
}
total += num_bytes;
Expand All @@ -109,23 +110,23 @@ pub fn aggregate(
for (path, num_bytes, num_errors) in aggregates.into_iter() {
output_colored_path(
&mut out,
&walk_options,
&path,
num_bytes,
num_errors,
path_color_of(&path),
byte_format,
)?;
}
}

if num_roots > 1 && compute_total {
output_colored_path(
&mut out,
&walk_options,
Path::new("total"),
total,
res.num_errors,
None,
byte_format,
)?;
}
Ok((res, stats))
Expand All @@ -137,15 +138,15 @@ fn path_color_of(path: impl AsRef<Path>) -> Option<Color> {

fn output_colored_path(
out: &mut impl io::Write,
options: &WalkOptions,
path: impl AsRef<Path>,
num_bytes: u128,
num_errors: u64,
path_color: Option<Color>,
byte_format: ByteFormat,
) -> std::result::Result<(), io::Error> {
let size = options.byte_format.display(num_bytes).to_string();
let size = byte_format.display(num_bytes).to_string();
let size = size.green();
let size_width = options.byte_format.width();
let size_width = byte_format.width();
let path = path.as_ref().display();

let errors = (num_errors != 0)
Expand Down
3 changes: 1 addition & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ pub struct WalkOptions {
/// The amount of threads to use. Refer to [`WalkDir::num_threads()`](https://docs.rs/jwalk/0.4.0/jwalk/struct.WalkDir.html#method.num_threads)
/// for more information.
pub threads: usize,
pub byte_format: ByteFormat,
pub count_hard_links: bool,
pub apparent_size: bool,
pub sorting: TraversalSorting,
Expand All @@ -177,7 +176,7 @@ pub struct WalkOptions {
type WalkDir = jwalk::WalkDirGeneric<((), Option<Result<std::fs::Metadata, jwalk::Error>>)>;

impl WalkOptions {
pub(crate) fn iter_from_path(&self, root: &Path, root_device_id: u64) -> WalkDir {
pub fn iter_from_path(&self, root: &Path, root_device_id: u64) -> WalkDir {
let ignore_dirs = self.ignore_dirs.clone();
let cwd = std::env::current_dir().unwrap_or_else(|_| root.to_owned());
WalkDir::new(root)
Expand Down
6 changes: 3 additions & 3 deletions src/interactive/app/bytevis.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dua::{ByteFormat, WalkOptions};
use dua::ByteFormat;
use std::fmt;

#[derive(Default, Clone, Copy)]
Expand Down Expand Up @@ -106,8 +106,8 @@ pub struct DisplayOptions {
pub byte_vis: ByteVisualization,
}

impl From<WalkOptions> for DisplayOptions {
fn from(WalkOptions { byte_format, .. }: WalkOptions) -> Self {
impl DisplayOptions {
pub fn new(byte_format: ByteFormat) -> Self {
DisplayOptions {
byte_format,
byte_vis: ByteVisualization::default(),
Expand Down
Loading
Loading