From 34e090c2d713be98b7be748c2312eab1f0127d80 Mon Sep 17 00:00:00 2001 From: miampf Date: Sun, 21 Jan 2024 15:08:32 +0100 Subject: [PATCH 1/2] make command flag optional and indent command output --- Cargo.lock | 30 ++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 11 +++++++---- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 831e79c..05d4f29 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -263,6 +263,12 @@ dependencies = [ "digest", ] +[[package]] +name = "smawk" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c" + [[package]] name = "strsim" version = "0.10.0" @@ -288,9 +294,21 @@ dependencies = [ "lazy_static", "pest", "pest_derive", + "textwrap", "walkdir", ] +[[package]] +name = "textwrap" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" +dependencies = [ + "smawk", + "unicode-linebreak", + "unicode-width", +] + [[package]] name = "thiserror" version = "1.0.56" @@ -329,6 +347,18 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-linebreak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" + +[[package]] +name = "unicode-width" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" + [[package]] name = "utf8parse" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 2e0e1ae..fda8929 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ clap = { version = "4.4.18", features = ["derive"] } lazy_static = "1.4.0" pest = "2.7.6" pest_derive = "2.7.6" +textwrap = "0.16.0" walkdir = "2.4.0" diff --git a/src/main.rs b/src/main.rs index fa2fa1e..428182e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,7 @@ mod cli { #[arg(short, long)] /// A command that will be executed on matched files. - pub command: String, + pub command: Option, } impl Cli { @@ -81,13 +81,16 @@ fn main() -> Result<(), Box> { println!("{}", file.path.display()); let mut output = String::new(); - if !args.command.is_empty() { - output = execute_command_on_file(file.path.clone(), args.command.clone()); + if args.command.is_some() { + output = execute_command_on_file(file.path.clone(), args.command.clone().unwrap()); } if !args.silent { println!("\ttags:{:?} ", file.tags); - println!("\tOutput of command:\n{}", output); + println!( + "\tOutput of command:\n{}", + textwrap::indent(output.as_str(), "\t\t") + ); } } From 5511f82dc26fb600c2c7054e6cf8c6efe6b1c34b Mon Sep 17 00:00:00 2001 From: miampf Date: Sun, 21 Jan 2024 15:29:49 +0100 Subject: [PATCH 2/2] added colored printing for easier distinction --- Cargo.lock | 97 +++++++++++++++++++++++++++++++++++++++++++++++------ Cargo.toml | 1 + src/main.rs | 44 +++++++++++++++++++----- 3 files changed, 123 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 05d4f29..b7e56d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,7 +37,7 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" dependencies = [ - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -47,7 +47,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -111,6 +111,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "cpufeatures" version = "0.2.12" @@ -291,6 +301,7 @@ name = "tag" version = "0.1.0" dependencies = [ "clap", + "colored", "lazy_static", "pest", "pest_derive", @@ -412,13 +423,37 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.0", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -427,51 +462,93 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" 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", + "windows_aarch64_gnullvm 0.52.0", + "windows_aarch64_msvc 0.52.0", + "windows_i686_gnu 0.52.0", + "windows_i686_msvc 0.52.0", + "windows_x86_64_gnu 0.52.0", + "windows_x86_64_gnullvm 0.52.0", + "windows_x86_64_msvc 0.52.0", ] +[[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_gnullvm" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" +[[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_gnu" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" +[[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_gnu" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" +[[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_gnullvm" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index fda8929..41c0289 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] clap = { version = "4.4.18", features = ["derive"] } +colored = "2.1.0" lazy_static = "1.4.0" pest = "2.7.6" pest_derive = "2.7.6" diff --git a/src/main.rs b/src/main.rs index 428182e..23f2482 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::{path::PathBuf, process::Command}; +use colored::Colorize; use pest::Parser; use tag::{ parsers::query::{construct_query_ast, evaluate_ast, QueryParser, Rule}, @@ -27,6 +28,10 @@ mod cli { #[arg(short, long)] /// A command that will be executed on matched files. pub command: Option, + + #[arg(short, long)] + /// Disable coloring. + pub no_color: bool, } impl Cli { @@ -42,14 +47,24 @@ fn execute_command_on_file(path: PathBuf, command: String) -> String { let output = Command::new("bash").arg("-c").arg(command.clone()).output(); if let Err(e) = &output { - eprintln!("Wasn't able to execute command {}: {}", command, e); + eprintln!( + "{} Wasn't able to execute command {}: {}", + "[ERROR]".red().bold(), + command.blue().underline(), + e.to_string().red() + ); } let output = output.unwrap(); let output_string = std::str::from_utf8(output.stdout.as_slice()); if let Err(e) = &output_string { - eprintln!("Failed to get output from command {}: {}", command, e); + eprintln!( + "{} Failed to get output from command {}: {}", + "[ERROR]".red().bold(), + command.blue().underline(), + e.to_string().red() + ); } output_string.unwrap().to_string() @@ -58,11 +73,20 @@ fn execute_command_on_file(path: PathBuf, command: String) -> String { fn main() -> Result<(), Box> { let args = cli::Cli::new_and_parse(); + if args.no_color { + colored::control::set_override(false); + } + let file_index = get_tags_from_files(args.path.as_str())?; let query = QueryParser::parse(Rule::tagsearch, args.query.as_str()); if let Err(e) = &query { - eprintln!("Error: {}", e); + eprintln!( + "{} {}\n{}", + "[ERROR]".red().bold(), + "Invalid query".red(), + e.to_string().red() + ); std::process::exit(1); } @@ -78,7 +102,7 @@ fn main() -> Result<(), Box> { continue; } - println!("{}", file.path.display()); + println!("{}", file.path.display().to_string().green()); let mut output = String::new(); if args.command.is_some() { @@ -86,11 +110,13 @@ fn main() -> Result<(), Box> { } if !args.silent { - println!("\ttags:{:?} ", file.tags); - println!( - "\tOutput of command:\n{}", - textwrap::indent(output.as_str(), "\t\t") - ); + println!("\t{}", format!("tags: {:?}", file.tags).blue()); + if !output.is_empty() { + println!( + "\tOutput of command:\n{}", + textwrap::indent(output.as_str(), "\t\t") + ); + } } }