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

progress bar #176

Merged
merged 5 commits into from
May 25, 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
113 changes: 113 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ansi_term = "0.12.1"
chrono = "0.4.24"
clap = { version = "4.1.1", features = ["derive"] }
clap_complete = "4.1.1"
crossterm = "0.26.1"
dirs = "5.0"
errno = "0.3.1"
filesize = "0.2.0"
Expand Down
11 changes: 11 additions & 0 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ pub struct Context {
#[arg(long)]
pub no_config: bool,

/// Hides the progress indicator
#[arg(long)]
pub no_progress: bool,

/// Omit disk usage from output
#[arg(long)]
pub suppress_size: bool,
Expand Down Expand Up @@ -525,10 +529,17 @@ impl Context {
}

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

/// Answers whether disk usage is asked to be reported in bytes.
pub const fn byte_metric(&self) -> bool {
matches!(self.disk_usage, DiskUsage::Logical)
|| matches!(self.disk_usage, DiskUsage::Physical)
}

/// Do any of the components of a path match the provided glob? This is used for ensuring that
/// all children of a directory that a glob targets gets captured.
#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/disk_usage/file_size/byte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub struct Metric {
prefix_kind: PrefixKind,

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

Expand Down
42 changes: 28 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
#![cfg_attr(windows, feature(windows_by_handle))]
#![warn(
clippy::all,
clippy::correctness,
clippy::suspicious,
clippy::style,
clippy::cargo,
clippy::complexity,
clippy::perf,
clippy::pedantic,
clippy::correctness,
clippy::nursery,
clippy::cargo
clippy::pedantic,
clippy::perf,
clippy::style,
clippy::suspicious
)]
#![allow(
clippy::struct_excessive_bools,
clippy::too_many_arguments,
clippy::cast_possible_truncation,
clippy::cast_precision_loss,
clippy::cast_sign_loss,
clippy::cast_possible_truncation
clippy::let_underscore_untyped,
clippy::struct_excessive_bools,
clippy::too_many_arguments
)]

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

/// Concerned with displaying a progress indicator when stdout is a tty.
mod progress;

/// Concerned with taking an initialized [`Tree`] and its [`Node`]s and rendering the output.
///
/// [`Tree`]: tree::Tree
Expand Down Expand Up @@ -68,22 +73,31 @@ fn main() -> Result<(), Box<dyn Error>> {

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

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

match ctx.layout {
let (tree, ctx) = Tree::try_init_and_update_context(ctx, indicator.as_ref())?;

let output = match ctx.layout {
layout::Type::Flat => {
let render = Engine::<Flat>::new(tree, ctx);
println!("{render}");
format!("{render}")
}
layout::Type::Inverted => {
let render = Engine::<Inverted>::new(tree, ctx);
println!("{render}");
format!("{render}")
}
layout::Type::Regular => {
let render = Engine::<Regular>::new(tree, ctx);
println!("{render}");
format!("{render}")
}
};

if let Some(progress) = indicator {
progress.mailbox().send(Message::RenderReady)?;
progress.join_handle.join().unwrap()?;
}

println!("{output}");

Ok(())
}
Loading