Skip to content

Commit

Permalink
Move ByteFormat out of WalkOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrwach committed Jan 7, 2024
1 parent feec3eb commit e53036a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 22 deletions.
11 changes: 8 additions & 3 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, InodeFilter, Throttle, WalkOptions, WalkResult, ByteFormat};
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 @@ -94,6 +95,7 @@ pub fn aggregate(
num_bytes,
num_errors,
path_color_of(&path),
byte_format
)?;
}
total += num_bytes;
Expand All @@ -114,6 +116,7 @@ pub fn aggregate(
num_bytes,
num_errors,
path_color_of(&path),
byte_format
)?;
}
}
Expand All @@ -126,6 +129,7 @@ pub fn aggregate(
total,
res.num_errors,
None,
byte_format
)?;
}
Ok((res, stats))
Expand All @@ -142,10 +146,11 @@ fn output_colored_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
1 change: 0 additions & 1 deletion 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 Down
4 changes: 2 additions & 2 deletions src/interactive/app/bytevis.rs
Original file line number Diff line number Diff line change
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
8 changes: 3 additions & 5 deletions src/interactive/app/terminal_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use crossbeam::channel::Receiver;
use crosstermion::input::Event;
use dua::{traverse::{Traversal, Tree, EntryData}, WalkResult, WalkOptions};
use dua::{traverse::{Traversal, Tree, EntryData}, WalkResult, WalkOptions, ByteFormat};
use tui::prelude::Backend;
use tui_react::Terminal;
use anyhow::Result;
Expand Down Expand Up @@ -60,7 +60,7 @@ impl TerminalApp {

pub fn initialize<B>(
terminal: &mut Terminal<B>,
options: WalkOptions,
byte_format: ByteFormat,
input_paths: Vec<PathBuf>,
keys_rx: Receiver<Event>,
) -> Result<Option<KeyboardInputAndApp>>
Expand All @@ -70,9 +70,7 @@ impl TerminalApp {
terminal.hide_cursor()?;
terminal.clear()?;

let mut display: DisplayOptions = options.clone().into();
display.byte_vis = ByteVisualization::PercentageAndBar;

let mut display = DisplayOptions::new(byte_format);
let mut window = MainWindow::default();

// #[inline]
Expand Down
20 changes: 11 additions & 9 deletions src/interactive/app/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,7 @@ pub fn initialized_app_and_terminal_with_closure(
let input_paths = fixture_paths.iter().map(|c| convert(c.as_ref())).collect();
let app = TerminalApp::initialize(
&mut terminal,
WalkOptions {
threads: 1,
byte_format: ByteFormat::Metric,
apparent_size: true,
count_hard_links: false,
sorting: TraversalSorting::AlphabeticalByFileName,
cross_filesystems: false,
ignore_dirs: Default::default(),
},
ByteFormat::Metric,
input_paths,
keys_rx,
)?
Expand All @@ -196,6 +188,16 @@ pub fn initialized_app_and_terminal_with_closure(
terminal,
app.expect("app that didn't try to abort iteration"),
))

// WalkOptions {
// threads: 1,
// byte_format: ,
// apparent_size: true,
// count_hard_links: false,
// sorting: TraversalSorting::AlphabeticalByFileName,
// cross_filesystems: false,
// ignore_dirs: Default::default(),
// }
}

pub fn new_test_terminal() -> std::io::Result<Terminal<TestBackend>> {
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{fs, io, io::Write, path::PathBuf, process};

use crate::interactive::input::input_channel;
use crate::interactive::terminal_app::TerminalApp;
use crate::options::ByteFormat;

mod crossdev;
#[cfg(feature = "tui-crossplatform")]
Expand Down Expand Up @@ -42,9 +43,9 @@ fn main() -> Result<()> {
info!("dua options={opt:#?}");
}

let byte_format: dua::ByteFormat = opt.format.into();
let walk_options = dua::WalkOptions {
threads: opt.threads,
byte_format: opt.format.into(),
apparent_size: opt.apparent_size,
count_hard_links: opt.count_hard_links,
sorting: TraversalSorting::None,
Expand All @@ -70,7 +71,7 @@ fn main() -> Result<()> {
let keys_rx = input_channel();
let res = TerminalApp::initialize(
&mut terminal,
walk_options,
byte_format,
extract_paths_maybe_set_cwd(input, !opt.stay_on_filesystem)?,
keys_rx,
)?
Expand Down Expand Up @@ -123,6 +124,7 @@ fn main() -> Result<()> {
walk_options,
!no_total,
!no_sort,
byte_format,
extract_paths_maybe_set_cwd(input, !opt.stay_on_filesystem)?,
)?;
if statistics {
Expand All @@ -139,6 +141,7 @@ fn main() -> Result<()> {
walk_options,
true,
true,
byte_format,
extract_paths_maybe_set_cwd(opt.input, !opt.stay_on_filesystem)?,
)?
.0
Expand Down

0 comments on commit e53036a

Please sign in to comment.