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

feat: add concurrent working threads option to CLI args #760

Merged
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
5 changes: 5 additions & 0 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ pub struct CliArgs {
#[arg(short = 'p', long = "password", global = true)]
pub password: Option<OsString>,

/// cocurrent working threads
#[arg(short = 'c', long, global = true)]
pub threads: Option<usize>,

// Ouch and claps subcommands
#[command(subcommand)]
pub cmd: Subcommand,
Expand Down Expand Up @@ -142,6 +146,7 @@ mod tests {
format: None,
// This is usually replaced in assertion tests
password: None,
threads: None,
cmd: Subcommand::Decompress {
// Put a crazy value here so no test can assert it unintentionally
files: vec!["\x00\x11\x22".into()],
Expand Down
7 changes: 7 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ pub fn run(
question_policy: QuestionPolicy,
file_visibility_policy: FileVisibilityPolicy,
) -> crate::Result<()> {
if let Some(threads) = args.threads {
rayon::ThreadPoolBuilder::new()
.num_threads(threads)
.build_global()
.unwrap();
}

match args.cmd {
Subcommand::Compress {
files,
Expand Down
7 changes: 5 additions & 2 deletions src/utils/logger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::sync::{mpsc, Arc, Barrier, OnceLock};
use std::{
sync::{mpsc, Arc, Barrier, OnceLock},
thread,
};

pub use logger_thread::spawn_logger_thread;

Expand Down Expand Up @@ -168,7 +171,7 @@ mod logger_thread {

pub fn spawn_logger_thread() {
let log_receiver = setup_channel();
rayon::spawn(move || run_logger(log_receiver));
thread::spawn(move || run_logger(log_receiver));
}

fn run_logger(log_receiver: LogReceiver) {
Expand Down
2 changes: 2 additions & 0 deletions tests/snapshots/ui__ui_test_usage_help_flag-2.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: tests/ui.rs
expression: "output_to_string(ouch!(\"-h\"))"
snapshot_kind: text
---
A command-line utility for easily compressing and decompressing files and directories.

Expand All @@ -21,5 +22,6 @@ Options:
-g, --gitignore Ignores files matched by git's ignore files
-f, --format <FORMAT> Specify the format of the archive
-p, --password <PASSWORD> decompress or list with password
-c, --threads <THREADS> cocurrent working threads
-h, --help Print help (see more with '--help')
-V, --version Print version
4 changes: 4 additions & 0 deletions tests/snapshots/ui__ui_test_usage_help_flag.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: tests/ui.rs
expression: "output_to_string(ouch!(\"--help\"))"
snapshot_kind: text
---
A command-line utility for easily compressing and decompressing files and directories.

Expand Down Expand Up @@ -43,6 +44,9 @@ Options:
-p, --password <PASSWORD>
decompress or list with password

-c, --threads <THREADS>
cocurrent working threads

-h, --help
Print help (see a summary with '-h')

Expand Down
Loading