From e8961564bfb76fc02ec2ff4e505848dafaf085e5 Mon Sep 17 00:00:00 2001 From: Riccardo Attilio Galli Date: Tue, 20 Feb 2024 15:02:23 -0800 Subject: [PATCH 1/8] Display short help when run without args --- CHANGELOG.md | 1 + src/bin/tuc.rs | 33 +++++++++++++++++++++++++++++++++ tests/cli.rs | 11 ++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4951a1..713fba6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), it triggers when you cut fields on 1-byte characters) * fields cut is now done on bytes, not strings (as long as your delimiter is proper utf-8 you'll be fine) +- feat: display short help when run without arguments - feat: --characters now depends on the (default) regex feature - refactor: --json internally uses serde_json, faster and more precise diff --git a/src/bin/tuc.rs b/src/bin/tuc.rs index d510265..f8713ca 100644 --- a/src/bin/tuc.rs +++ b/src/bin/tuc.rs @@ -1,5 +1,6 @@ use anyhow::Result; use std::convert::TryFrom; +use std::env::args; use std::io::Write; use std::str::FromStr; use tuc::bounds::{BoundOrFiller, BoundsType, UserBoundsList}; @@ -86,9 +87,41 @@ Memory consumption: "# ); +const SHORT_HELP: &str = concat!( + "tuc ", + env!("CARGO_PKG_VERSION"), + r#" - Created by Riccardo Attilio Galli + +Cuts selected parts and lets you re-arrange them. + +Some examples: + + $ echo "a/b/c" | tuc -d / -f 1,-1 + ac + + $ echo "a/b/c" | tuc -d / -f 2: + b/c + + $ echo "hello.bak" | tuc -d . -f 'mv {1:} {1}' + mv hello.bak hello + + $ printf "a\nb\nc\nd\ne" | tuc -l 2:-2 + b + c + d + +Run `tuc --help` for more detailed information. +"# +); + fn parse_args() -> Result { let mut pargs = pico_args::Arguments::from_env(); + if args().len() == 1 { + print!("{SHORT_HELP}"); + std::process::exit(0); + } + if pargs.contains(["-h", "--help"]) { print!("{HELP}"); std::process::exit(0); diff --git a/tests/cli.rs b/tests/cli.rs index 0a172d0..1d9739c 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1,10 +1,19 @@ use assert_cmd::Command; +#[test] +fn it_display_short_help_when_run_without_arguments() { + let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); + + let assert = cmd.assert(); + + assert.success().stdout(predicates::str::starts_with("tuc")); +} + #[test] fn it_echo_non_delimited_line() { let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); - let assert = cmd.write_stdin("foobar").assert(); + let assert = cmd.args(["-d", "/"]).write_stdin("foobar").assert(); assert.success().stdout("foobar\n"); } From 222621ac376bae33b24176f27e22ac113b45d517 Mon Sep 17 00:00:00 2001 From: Riccardo Attilio Galli Date: Tue, 20 Feb 2024 15:04:53 -0800 Subject: [PATCH 2/8] Move help to its own file --- src/bin/tuc.rs | 99 +------------------------------------------------- src/help.rs | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 3 files changed, 99 insertions(+), 98 deletions(-) create mode 100644 src/help.rs diff --git a/src/bin/tuc.rs b/src/bin/tuc.rs index f8713ca..0ce9201 100644 --- a/src/bin/tuc.rs +++ b/src/bin/tuc.rs @@ -8,6 +8,7 @@ use tuc::cut_bytes::read_and_cut_bytes; use tuc::cut_lines::read_and_cut_lines; use tuc::cut_str::read_and_cut_str; use tuc::fast_lane::{read_and_cut_text_as_bytes, FastOpt}; +use tuc::help::{HELP, SHORT_HELP}; use tuc::options::{Opt, EOL}; #[cfg(feature = "regex")] @@ -16,104 +17,6 @@ use tuc::options::RegexBag; #[cfg(feature = "regex")] use regex::bytes::Regex; -const HELP: &str = concat!( - "tuc ", - env!("CARGO_PKG_VERSION"), - r#" -Cut text (or bytes) where a delimiter matches, then keep the desired parts. - -The data is read from standard input. - -USAGE: - tuc [FLAGS] [OPTIONS] - -FLAGS: - -g, --greedy-delimiter Match consecutive delimiters as if it was one - -p, --compress-delimiter Print only the first delimiter of a sequence - -s, --only-delimited Print only lines containing the delimiter - -V, --version Print version information - -z, --zero-terminated Line delimiter is NUL (\0), not LF (\n) - -h, --help Print this help and exit - -m, --complement Invert fields (e.g. '2' becomes '1,3:') - -j, --(no-)join Print selected parts with delimiter in between - --json Print fields as a JSON array of strings - -OPTIONS: - -f, --fields Fields to keep, 1-indexed, comma separated. - Use colon to include everything in a range. - Fields can be negative (-1 is the last field). - [default 1:] - - e.g. cutting the string 'a-b-c-d' on '-' - -f 1 => a - -f 1: => a-b-c-d - -f 1:3 => a-b-c - -f 3,2 => cb - -f 3,1:2 => ca-b - -f -3:-2 => b-c - - To re-apply the delimiter add -j, to replace - it add -r (followed by the new delimiter). - - You can also format the output using {} syntax - e.g. - -f '({1}, {2})' => (a, b) - - You can escape { and } using {{ and }}. - - -b, --bytes Same as --fields, but it keeps bytes - -c, --characters Same as --fields, but it keeps characters - -l, --lines Same as --fields, but it keeps lines - Implies --join. To merge lines, use --no-join - -d, --delimiter Delimiter used by --fields to cut the text - [default: \t] - -e, --regex Use a regular expression as delimiter - -r, --replace-delimiter Replace the delimiter with the provided text. - Implies --join - -t, --trim Trim the delimiter (greedy). Valid values are - (l|L)eft, (r|R)ight, (b|B)oth - -Options precedence: - --trim and --compress-delimiter are applied before --fields or similar - -Memory consumption: - --characters and --fields read and allocate memory one line at a time - - --lines allocate memory one line at a time as long as the requested fields - are ordered and non-negative (e.g. -l 1,3:4,4,7), otherwise it allocates - the whole input in memory (it also happens when -p or -m are being used) - - --bytes allocate the whole input in memory -"# -); - -const SHORT_HELP: &str = concat!( - "tuc ", - env!("CARGO_PKG_VERSION"), - r#" - Created by Riccardo Attilio Galli - -Cuts selected parts and lets you re-arrange them. - -Some examples: - - $ echo "a/b/c" | tuc -d / -f 1,-1 - ac - - $ echo "a/b/c" | tuc -d / -f 2: - b/c - - $ echo "hello.bak" | tuc -d . -f 'mv {1:} {1}' - mv hello.bak hello - - $ printf "a\nb\nc\nd\ne" | tuc -l 2:-2 - b - c - d - -Run `tuc --help` for more detailed information. -"# -); - fn parse_args() -> Result { let mut pargs = pico_args::Arguments::from_env(); diff --git a/src/help.rs b/src/help.rs new file mode 100644 index 0000000..addbb95 --- /dev/null +++ b/src/help.rs @@ -0,0 +1,97 @@ +pub const HELP: &str = concat!( + "tuc ", + env!("CARGO_PKG_VERSION"), + r#" +Cut text (or bytes) where a delimiter matches, then keep the desired parts. + +The data is read from standard input. + +USAGE: + tuc [FLAGS] [OPTIONS] + +FLAGS: + -g, --greedy-delimiter Match consecutive delimiters as if it was one + -p, --compress-delimiter Print only the first delimiter of a sequence + -s, --only-delimited Print only lines containing the delimiter + -V, --version Print version information + -z, --zero-terminated Line delimiter is NUL (\0), not LF (\n) + -h, --help Print this help and exit + -m, --complement Invert fields (e.g. '2' becomes '1,3:') + -j, --(no-)join Print selected parts with delimiter in between + --json Print fields as a JSON array of strings + +OPTIONS: + -f, --fields Fields to keep, 1-indexed, comma separated. + Use colon to include everything in a range. + Fields can be negative (-1 is the last field). + [default 1:] + + e.g. cutting the string 'a-b-c-d' on '-' + -f 1 => a + -f 1: => a-b-c-d + -f 1:3 => a-b-c + -f 3,2 => cb + -f 3,1:2 => ca-b + -f -3:-2 => b-c + + To re-apply the delimiter add -j, to replace + it add -r (followed by the new delimiter). + + You can also format the output using {} syntax + e.g. + -f '({1}, {2})' => (a, b) + + You can escape { and } using {{ and }}. + + -b, --bytes Same as --fields, but it keeps bytes + -c, --characters Same as --fields, but it keeps characters + -l, --lines Same as --fields, but it keeps lines + Implies --join. To merge lines, use --no-join + -d, --delimiter Delimiter used by --fields to cut the text + [default: \t] + -e, --regex Use a regular expression as delimiter + -r, --replace-delimiter Replace the delimiter with the provided text. + Implies --join + -t, --trim Trim the delimiter (greedy). Valid values are + (l|L)eft, (r|R)ight, (b|B)oth + +Options precedence: + --trim and --compress-delimiter are applied before --fields or similar + +Memory consumption: + --characters and --fields read and allocate memory one line at a time + + --lines allocate memory one line at a time as long as the requested fields + are ordered and non-negative (e.g. -l 1,3:4,4,7), otherwise it allocates + the whole input in memory (it also happens when -p or -m are being used) + + --bytes allocate the whole input in memory +"# +); + +pub const SHORT_HELP: &str = concat!( + "tuc ", + env!("CARGO_PKG_VERSION"), + r#" - Created by Riccardo Attilio Galli + +Cuts selected parts and lets you re-arrange them. + +Some examples: + + $ echo "a/b/c" | tuc -d / -f 1,-1 + ac + + $ echo "a/b/c" | tuc -d / -f 2: + b/c + + $ echo "hello.bak" | tuc -d . -f 'mv {1:} {1}' + mv hello.bak hello + + $ printf "a\nb\nc\nd\ne" | tuc -l 2:-2 + b + c + d + +Run `tuc --help` for more detailed information. +"# +); diff --git a/src/lib.rs b/src/lib.rs index 48dbb6e..cf1c9e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,5 +3,6 @@ pub mod cut_bytes; pub mod cut_lines; pub mod cut_str; pub mod fast_lane; +pub mod help; pub mod options; mod read_utils; From 051d1e7ddd94efee8c51ebf2a53758024b94ed7e Mon Sep 17 00:00:00 2001 From: Riccardo Attilio Galli Date: Thu, 22 Feb 2024 21:51:16 -0800 Subject: [PATCH 3/8] Coloured help --- src/bin/tuc.rs | 4 ++-- src/help.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/bin/tuc.rs b/src/bin/tuc.rs index 0ce9201..0d7c3ad 100644 --- a/src/bin/tuc.rs +++ b/src/bin/tuc.rs @@ -8,7 +8,7 @@ use tuc::cut_bytes::read_and_cut_bytes; use tuc::cut_lines::read_and_cut_lines; use tuc::cut_str::read_and_cut_str; use tuc::fast_lane::{read_and_cut_text_as_bytes, FastOpt}; -use tuc::help::{HELP, SHORT_HELP}; +use tuc::help::{get_colored_help, HELP, SHORT_HELP}; use tuc::options::{Opt, EOL}; #[cfg(feature = "regex")] @@ -26,7 +26,7 @@ fn parse_args() -> Result { } if pargs.contains(["-h", "--help"]) { - print!("{HELP}"); + print!("{}", get_colored_help(HELP)); std::process::exit(0); } diff --git a/src/help.rs b/src/help.rs index addbb95..add7ba1 100644 --- a/src/help.rs +++ b/src/help.rs @@ -1,3 +1,5 @@ +use regex::Regex; + pub const HELP: &str = concat!( "tuc ", env!("CARGO_PKG_VERSION"), @@ -24,7 +26,7 @@ OPTIONS: -f, --fields Fields to keep, 1-indexed, comma separated. Use colon to include everything in a range. Fields can be negative (-1 is the last field). - [default 1:] + [default: 1:] e.g. cutting the string 'a-b-c-d' on '-' -f 1 => a @@ -74,7 +76,7 @@ pub const SHORT_HELP: &str = concat!( env!("CARGO_PKG_VERSION"), r#" - Created by Riccardo Attilio Galli -Cuts selected parts and lets you re-arrange them. +Cut text (or bytes) where a delimiter matches, then keep the desired parts. Some examples: @@ -95,3 +97,55 @@ Some examples: Run `tuc --help` for more detailed information. "# ); + +pub fn get_colored_help(text: &str) -> String { + // This is very unprofessional but: + // - I'm playing around and there's no need to look for serious + // performance for the help + // - for getting the colours as I wanted, the alternative + // was to tag the original help, but I'm more afraid + // of desyncing readme/man/help than getting this wrong + // (which I will, no doubt about it) + + // optional parameters + let text = Regex::new(r#"<.*?>"#) + .unwrap() + .replace_all(text, "\x1b[33m$0\x1b[0m"); + + // any example using "-f something" + let text = Regex::new(r#"-(f|l) ('.+'|[0-9,:-]+)"#) + .unwrap() + .replace_all(&text, "-$1 \x1b[33m$2\x1b[0m"); + + // a few one-shot fields" + let text = Regex::new(r#"'2'|'1,3:'|-1 "#) + .unwrap() + .replace_all(&text, "\x1b[33m$0\x1b[0m"); + + // Main labels + let text = Regex::new(r#"(?m)^[^\s].+?:.*"#) + .unwrap() + .replace_all(&text, "\x1b[1;32m$0\x1b[0m"); + + // args (e.g. -j, --join) + let text = Regex::new(r#"\s-[^\s\d,]+"#) + .unwrap() + .replace_all(&text, "\x1b[1;36m$0\x1b[0m"); + + // first line + let text = Regex::new(r#"tuc.*"#) + .unwrap() + .replace_all(&text, "\x1b[1;35m$0\x1b[0m"); + + // trim examples: (l|L)eft, (r|R)ight, (b|B)oth + let text = Regex::new(r#"\((.)\|(.)\)(eft|ight|oth)"#) + .unwrap() + .replace_all(&text, "(\x1b[33m$1\x1b[0m|\x1b[33m$2\x1b[0m)$3"); + + // defaults + let text = Regex::new(r#"default: ([^\]]+)"#) + .unwrap() + .replace_all(&text, "\x1b[35mdefault\x1b[0m: \x1b[33m$1\x1b[0m"); + + text.into_owned() +} From 29b7f564bd1b63560280b9749653be701c716677 Mon Sep 17 00:00:00 2001 From: Riccardo Attilio Galli Date: Thu, 22 Feb 2024 22:26:52 -0800 Subject: [PATCH 4/8] Display help as colored only if allowed --- src/bin/tuc.rs | 4 ++-- src/help.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/bin/tuc.rs b/src/bin/tuc.rs index 0d7c3ad..fbe92fb 100644 --- a/src/bin/tuc.rs +++ b/src/bin/tuc.rs @@ -8,7 +8,7 @@ use tuc::cut_bytes::read_and_cut_bytes; use tuc::cut_lines::read_and_cut_lines; use tuc::cut_str::read_and_cut_str; use tuc::fast_lane::{read_and_cut_text_as_bytes, FastOpt}; -use tuc::help::{get_colored_help, HELP, SHORT_HELP}; +use tuc::help::{get_help, SHORT_HELP}; use tuc::options::{Opt, EOL}; #[cfg(feature = "regex")] @@ -26,7 +26,7 @@ fn parse_args() -> Result { } if pargs.contains(["-h", "--help"]) { - print!("{}", get_colored_help(HELP)); + print!("{}", get_help()); std::process::exit(0); } diff --git a/src/help.rs b/src/help.rs index add7ba1..8f94c33 100644 --- a/src/help.rs +++ b/src/help.rs @@ -1,6 +1,8 @@ +use std::{borrow::Cow, io::IsTerminal}; + use regex::Regex; -pub const HELP: &str = concat!( +const HELP: &str = concat!( "tuc ", env!("CARGO_PKG_VERSION"), r#" @@ -98,7 +100,7 @@ Run `tuc --help` for more detailed information. "# ); -pub fn get_colored_help(text: &str) -> String { +fn get_colored_help(text: &str) -> String { // This is very unprofessional but: // - I'm playing around and there's no need to look for serious // performance for the help @@ -149,3 +151,23 @@ pub fn get_colored_help(text: &str) -> String { text.into_owned() } + +fn can_use_color() -> bool { + let is_tty = std::io::stdout().is_terminal(); + let term = std::env::var("TERM"); + let no_color = std::env::var("NO_COLOR"); + + is_tty + && term.is_ok() + && term.as_deref() != Ok("dumb") + && term.as_deref() != Ok("") + && no_color.is_err() +} + +pub fn get_help() -> Cow<'static, str> { + if can_use_color() { + Cow::Owned(get_colored_help(HELP)) + } else { + Cow::Borrowed(HELP) + } +} From d710d38b3150086d94320f55c1a8315eee6a7e9e Mon Sep 17 00:00:00 2001 From: Riccardo Attilio Galli Date: Thu, 22 Feb 2024 23:03:31 -0800 Subject: [PATCH 5/8] Display short help as colored by default --- src/bin/tuc.rs | 4 ++-- src/help.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/bin/tuc.rs b/src/bin/tuc.rs index fbe92fb..21d2119 100644 --- a/src/bin/tuc.rs +++ b/src/bin/tuc.rs @@ -8,7 +8,7 @@ use tuc::cut_bytes::read_and_cut_bytes; use tuc::cut_lines::read_and_cut_lines; use tuc::cut_str::read_and_cut_str; use tuc::fast_lane::{read_and_cut_text_as_bytes, FastOpt}; -use tuc::help::{get_help, SHORT_HELP}; +use tuc::help::{get_help, get_short_help}; use tuc::options::{Opt, EOL}; #[cfg(feature = "regex")] @@ -21,7 +21,7 @@ fn parse_args() -> Result { let mut pargs = pico_args::Arguments::from_env(); if args().len() == 1 { - print!("{SHORT_HELP}"); + print!("{}", get_short_help()); std::process::exit(0); } diff --git a/src/help.rs b/src/help.rs index 8f94c33..36f53c5 100644 --- a/src/help.rs +++ b/src/help.rs @@ -97,6 +97,7 @@ Some examples: d Run `tuc --help` for more detailed information. +Send bug reports to: https://github.com/riquito/tuc/issues "# ); @@ -152,6 +153,30 @@ fn get_colored_help(text: &str) -> String { text.into_owned() } +fn get_colored_short_help(text: &str) -> String { + let text = Regex::new(r#"( tuc|echo|printf)"#) + .unwrap() + .replace_all(text, "\x1b[1;32m$1\x1b[0m"); + + let text = Regex::new(r#"(?ms)(\$) (.*?)\n(.*?)\n\n"#) + .unwrap() + .replace_all(&text, "\x1b[1;36m$1\x1b[0m $2\n\x1b[0m$3\x1b[0m\n\n"); + + let text = Regex::new(r#"\|"#) + .unwrap() + .replace_all(&text, "\x1b[1;35m|\x1b[0m"); + + let text = Regex::new(r#"(tuc --help)"#) + .unwrap() + .replace_all(&text, "\x1b[33m$1\x1b[0m"); + + let text = Regex::new(r#"(tuc [^\s]+)"#) + .unwrap() + .replace_all(&text, "\x1b[1;35m$1\x1b[0m"); + + text.into_owned() +} + fn can_use_color() -> bool { let is_tty = std::io::stdout().is_terminal(); let term = std::env::var("TERM"); @@ -171,3 +196,11 @@ pub fn get_help() -> Cow<'static, str> { Cow::Borrowed(HELP) } } + +pub fn get_short_help() -> Cow<'static, str> { + if can_use_color() { + Cow::Owned(get_colored_short_help(SHORT_HELP)) + } else { + Cow::Borrowed(SHORT_HELP) + } +} From 3ae8adc79ef7c98edbba2ce328041714e0adf177 Mon Sep 17 00:00:00 2001 From: Riccardo Attilio Galli Date: Thu, 22 Feb 2024 23:05:30 -0800 Subject: [PATCH 6/8] Update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 713fba6..fb1cd80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), delimiter is proper utf-8 you'll be fine) - feat: display short help when run without arguments - feat: --characters now depends on the (default) regex feature +- feat: help and short help are colored, as long as output is a tty and + unless env var TERM=dumb or NO_COLOR (any value) is set - refactor: --json internally uses serde_json, faster and more precise ## [1.2.0] - 2024-01-01 From 353921f2196728278d0f05f10dc9a37ec17f1bd0 Mon Sep 17 00:00:00 2001 From: Riccardo Attilio Galli Date: Sat, 24 Feb 2024 17:34:00 -0800 Subject: [PATCH 7/8] Update help text everywhere --- README.md | 6 ++++++ doc/tuc.1 | 8 ++++++++ doc/tuc.1.md | 9 +++++++++ src/help.rs | 6 ++++++ 4 files changed, 29 insertions(+) diff --git a/README.md b/README.md index e2813ac..a03d329 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,12 @@ Memory consumption: the whole input in memory (it also happens when -p or -m are being used) --bytes allocate the whole input in memory + +Colors: + Help is displayed using colors. Colors will be suppressed in the + following circumstances: + - when the TERM environment variable is not set or set to "dumb" + - when the NO_COLOR environment variable is set (regardless of value) ``` ## Examples diff --git a/doc/tuc.1 b/doc/tuc.1 index 0601746..e5a33b1 100644 --- a/doc/tuc.1 +++ b/doc/tuc.1 @@ -186,6 +186,14 @@ allocates the whole input in memory (it also happens when -p or -m are being used) .PP --bytes allocate the whole input in memory +.SH COLORS +.PP +Help is displayed using colors. +Colors will be suppressed in the following circumstances: +.IP \[bu] 2 +when the TERM environment variable is not set or set to \[lq]dumb\[rq] +.IP \[bu] 2 +when the NO_COLOR environment variable is set (regardless of value) .SH BUGS .PP See GitHub Issues: diff --git a/doc/tuc.1.md b/doc/tuc.1.md index 1e6bdc7..f264785 100644 --- a/doc/tuc.1.md +++ b/doc/tuc.1.md @@ -116,6 +116,15 @@ MEMORY CONSUMPTION \--bytes allocate the whole input in memory +COLORS +====== + +Help is displayed using colors. Colors will be suppressed in the +following circumstances: + +- when the TERM environment variable is not set or set to "dumb" +- when the NO_COLOR environment variable is set (regardless of value) + BUGS ==== diff --git a/src/help.rs b/src/help.rs index 36f53c5..b6ead29 100644 --- a/src/help.rs +++ b/src/help.rs @@ -70,6 +70,12 @@ Memory consumption: the whole input in memory (it also happens when -p or -m are being used) --bytes allocate the whole input in memory + +Colors: + Help is displayed using colors. Colors will be suppressed in the + following circumstances: + - when the TERM environment variable is not set or set to "dumb" + - when the NO_COLOR environment variable is set (regardless of value) "# ); From c6d6740e29f6470f5968152f37a08dc85bdc76a2 Mon Sep 17 00:00:00 2001 From: Riccardo Attilio Galli Date: Sat, 24 Feb 2024 17:40:10 -0800 Subject: [PATCH 8/8] Regex is required for colored help --- src/help.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/help.rs b/src/help.rs index b6ead29..0135213 100644 --- a/src/help.rs +++ b/src/help.rs @@ -1,6 +1,9 @@ -use std::{borrow::Cow, io::IsTerminal}; - +#[cfg(feature = "regex")] use regex::Regex; +#[cfg(feature = "regex")] +use std::borrow::Cow; +#[cfg(feature = "regex")] +use std::io::IsTerminal; const HELP: &str = concat!( "tuc ", @@ -107,6 +110,7 @@ Send bug reports to: https://github.com/riquito/tuc/issues "# ); +#[cfg(feature = "regex")] fn get_colored_help(text: &str) -> String { // This is very unprofessional but: // - I'm playing around and there's no need to look for serious @@ -159,6 +163,7 @@ fn get_colored_help(text: &str) -> String { text.into_owned() } +#[cfg(feature = "regex")] fn get_colored_short_help(text: &str) -> String { let text = Regex::new(r#"( tuc|echo|printf)"#) .unwrap() @@ -183,6 +188,7 @@ fn get_colored_short_help(text: &str) -> String { text.into_owned() } +#[cfg(feature = "regex")] fn can_use_color() -> bool { let is_tty = std::io::stdout().is_terminal(); let term = std::env::var("TERM"); @@ -195,6 +201,7 @@ fn can_use_color() -> bool { && no_color.is_err() } +#[cfg(feature = "regex")] pub fn get_help() -> Cow<'static, str> { if can_use_color() { Cow::Owned(get_colored_help(HELP)) @@ -203,6 +210,7 @@ pub fn get_help() -> Cow<'static, str> { } } +#[cfg(feature = "regex")] pub fn get_short_help() -> Cow<'static, str> { if can_use_color() { Cow::Owned(get_colored_short_help(SHORT_HELP)) @@ -210,3 +218,13 @@ pub fn get_short_help() -> Cow<'static, str> { Cow::Borrowed(SHORT_HELP) } } + +#[cfg(not(feature = "regex"))] +pub fn get_help() -> &'static str { + HELP +} + +#[cfg(not(feature = "regex"))] +pub fn get_short_help() -> &'static str { + SHORT_HELP +}