diff --git a/Cargo.lock b/Cargo.lock index 9e18446..2df1b81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,91 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "runmany" version = "0.2.3" +dependencies = [ + "colored", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml index 06ff7d1..1356344 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,6 @@ license = "GPL-3.0" repository = "https://github.com/soanvig/runmany" readme = "README.md" keywords = ["system", "process", "many"] + +[dependencies] +colored = "2.1.0" diff --git a/src/main.rs b/src/main.rs index 777f114..406546e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,16 @@ +use colored::*; use std::env; use std::io::{BufRead, BufReader}; use std::process::{Command, ExitCode, Stdio}; use std::thread; +static colors: [&str; 5] = ["green", "yellow", "blue", "magenta", "cyan"]; + +#[derive(Clone)] struct RunmanyOptions { help: bool, version: bool, + no_color: bool, } fn main() -> ExitCode { @@ -25,7 +30,7 @@ fn main() -> ExitCode { return ExitCode::SUCCESS; } - spawn_commands(commands); + spawn_commands(commands, &runmany_options); } else { // No arguments given to runmany print_help(); @@ -44,6 +49,7 @@ fn print_help() { println!("Flags:"); println!(" -h, --help - print help"); println!(" -v, --version - print version"); + println!(" --no-color - do not color command output"); } fn print_version() { @@ -55,17 +61,23 @@ fn runmany_args_to_options(args: &&[String]) -> RunmanyOptions { // todo: wtf is wrong with those types :D let help = args.contains(&"-h".to_string()) || args.contains(&"--help".to_string()); let version = args.contains(&"-v".to_string()) || args.contains(&"--version".to_string()); + let no_color = args.contains(&"--no-color".to_string()); - RunmanyOptions { help, version } + RunmanyOptions { + help, + version, + no_color, + } } -fn spawn_commands(commands: &[&[String]]) { +fn spawn_commands(commands: &[&[String]], options: &RunmanyOptions) { let mut handles = vec![]; for (index, &command) in commands.iter().enumerate() { let command = command.to_vec(); + let options = options.clone(); let handle = thread::spawn(move || { - spawn_command(command, index + 1); + spawn_command(command, index + 1, options); }); handles.push(handle); } @@ -75,12 +87,23 @@ fn spawn_commands(commands: &[&[String]]) { } } -fn spawn_command(command_with_args: Vec, command_number: usize) { - println!( +/// command_number has to start from 1 +fn spawn_command(command_with_args: Vec, command_number: usize, options: RunmanyOptions) { + let color = colors[(command_number - 1) % colors.len()]; + + let print_color = move |str: String| { + if options.no_color { + println!("{}", str); + } else { + println!("{}", str.color(color)); + } + }; + + print_color(format!( "[{}]: Spawning command: \"{}\"", command_number, command_with_args.join(" ") - ); + )); let mut child = Command::new(command_with_args.get(0).expect("Command should be defined")) .args(&command_with_args[1..]) @@ -92,14 +115,22 @@ fn spawn_command(command_with_args: Vec, command_number: usize) { let stdout = BufReader::new(child.stdout.take().expect("Cannot reference stdout")); let stdout_handle = thread::spawn(move || { for line in stdout.lines() { - println!("[{}]: {}", command_number, line.expect("stdout to be line")); + print_color(format!( + "[{}]: {}", + command_number, + line.expect("stdout to be line") + )); } }); let stderr = BufReader::new(child.stderr.take().expect("Cannot reference stderr")); let stderr_handle = thread::spawn(move || { for line in stderr.lines() { - println!("[{}]: {}", command_number, line.expect("stdout to be line")); + print_color(format!( + "[{}]: {}", + command_number, + line.expect("stdout to be line") + )); } }); @@ -109,16 +140,19 @@ fn spawn_command(command_with_args: Vec, command_number: usize) { let status_code = child.wait().unwrap(); if status_code.success() { - println!("[{}]: Command finished successfully", command_number) + print_color(format!( + "[{}]: Command finished successfully", + command_number + )); } else { - println!( + print_color(format!( "[{}]: Command exited with status: {}", command_number, status_code .code() .map(|code| code.to_string()) .unwrap_or("unknown".to_string()) - ) + )); } }