Skip to content

Commit 1eef4b5

Browse files
authored
Merge pull request #176 from solidiquis/progress-indicator
progress bar
2 parents 6bba955 + 184d0a8 commit 1eef4b5

File tree

7 files changed

+345
-20
lines changed

7 files changed

+345
-20
lines changed

Cargo.lock

Lines changed: 113 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ ansi_term = "0.12.1"
2828
chrono = "0.4.24"
2929
clap = { version = "4.1.1", features = ["derive"] }
3030
clap_complete = "4.1.1"
31+
crossterm = "0.26.1"
3132
dirs = "5.0"
3233
errno = "0.3.1"
3334
filesize = "0.2.0"

src/context/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ pub struct Context {
180180
#[arg(long)]
181181
pub no_config: bool,
182182

183+
/// Hides the progress indicator
184+
#[arg(long)]
185+
pub no_progress: bool,
186+
183187
/// Omit disk usage from output
184188
#[arg(long)]
185189
pub suppress_size: bool,
@@ -525,10 +529,17 @@ impl Context {
525529
}
526530

527531
/// Setter for `window_width` which is set to the current terminal emulator's window width.
532+
#[inline]
528533
pub fn set_window_width(&mut self) {
529534
self.window_width = crate::tty::get_window_width(self.stdout_is_tty);
530535
}
531536

537+
/// Answers whether disk usage is asked to be reported in bytes.
538+
pub const fn byte_metric(&self) -> bool {
539+
matches!(self.disk_usage, DiskUsage::Logical)
540+
|| matches!(self.disk_usage, DiskUsage::Physical)
541+
}
542+
532543
/// Do any of the components of a path match the provided glob? This is used for ensuring that
533544
/// all children of a directory that a glob targets gets captured.
534545
#[inline]

src/disk_usage/file_size/byte.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ pub struct Metric {
1717
prefix_kind: PrefixKind,
1818

1919
/// To prevent allocating the same string twice. We allocate the first time
20-
/// in [`crate::tree::update_column_properties`] in order to compute the max column width for
21-
/// human-readable size and the second time during the actual render.
20+
/// in [`crate::tree::Tree::update_column_properties`] in order to compute the max column width for
21+
/// human-readable size and cache it. It will then be used again when preparing the output.
2222
cached_display: RefCell<String>,
2323
}
2424

src/main.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
#![cfg_attr(windows, feature(windows_by_handle))]
22
#![warn(
33
clippy::all,
4-
clippy::correctness,
5-
clippy::suspicious,
6-
clippy::style,
4+
clippy::cargo,
75
clippy::complexity,
8-
clippy::perf,
9-
clippy::pedantic,
6+
clippy::correctness,
107
clippy::nursery,
11-
clippy::cargo
8+
clippy::pedantic,
9+
clippy::perf,
10+
clippy::style,
11+
clippy::suspicious
1212
)]
1313
#![allow(
14-
clippy::struct_excessive_bools,
15-
clippy::too_many_arguments,
14+
clippy::cast_possible_truncation,
1615
clippy::cast_precision_loss,
1716
clippy::cast_sign_loss,
18-
clippy::cast_possible_truncation
17+
clippy::let_underscore_untyped,
18+
clippy::struct_excessive_bools,
19+
clippy::too_many_arguments
1920
)]
2021

2122
use clap::CommandFactory;
2223
use context::{layout, Context};
24+
use progress::Message;
2325
use render::{Engine, Flat, Inverted, Regular};
2426
use std::{error::Error, io::stdout};
2527
use tree::Tree;
@@ -39,6 +41,9 @@ mod fs;
3941
/// All things related to icons on how to map certain files to the appropriate icons.
4042
mod icons;
4143

44+
/// Concerned with displaying a progress indicator when stdout is a tty.
45+
mod progress;
46+
4247
/// Concerned with taking an initialized [`Tree`] and its [`Node`]s and rendering the output.
4348
///
4449
/// [`Tree`]: tree::Tree
@@ -68,22 +73,31 @@ fn main() -> Result<(), Box<dyn Error>> {
6873

6974
styles::init(ctx.no_color());
7075

71-
let (tree, ctx) = Tree::try_init_and_update_context(ctx)?;
76+
let indicator = (ctx.stdout_is_tty && !ctx.no_progress).then(progress::Indicator::measure);
7277

73-
match ctx.layout {
78+
let (tree, ctx) = Tree::try_init_and_update_context(ctx, indicator.as_ref())?;
79+
80+
let output = match ctx.layout {
7481
layout::Type::Flat => {
7582
let render = Engine::<Flat>::new(tree, ctx);
76-
println!("{render}");
83+
format!("{render}")
7784
}
7885
layout::Type::Inverted => {
7986
let render = Engine::<Inverted>::new(tree, ctx);
80-
println!("{render}");
87+
format!("{render}")
8188
}
8289
layout::Type::Regular => {
8390
let render = Engine::<Regular>::new(tree, ctx);
84-
println!("{render}");
91+
format!("{render}")
8592
}
93+
};
94+
95+
if let Some(progress) = indicator {
96+
progress.mailbox().send(Message::RenderReady)?;
97+
progress.join_handle.join().unwrap()?;
8698
}
8799

100+
println!("{output}");
101+
88102
Ok(())
89103
}

0 commit comments

Comments
 (0)