diff --git a/src/lib.rs b/src/lib.rs index 75e3332be4..9756027ef1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,7 @@ pub(crate) use { process::{self, Command, ExitStatus, Stdio}, rc::Rc, str::{self, Chars}, - sync::{Mutex, MutexGuard, OnceLock}, + sync::{LazyLock, Mutex, MutexGuard, OnceLock}, vec, }, strum::{Display, EnumDiscriminants, EnumString, IntoStaticStr}, diff --git a/src/subcommand.rs b/src/subcommand.rs index dd8903104e..f0879e0839 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -2,6 +2,8 @@ use {super::*, clap_mangen::Man}; const INIT_JUSTFILE: &str = "default:\n echo 'Hello, world!'\n"; +static BACKTICK_RE: LazyLock = LazyLock::new(|| Regex::new("(`.*?`)|(`[^`]*$)").unwrap()); + #[derive(PartialEq, Clone, Debug)] pub(crate) enum Subcommand { Changelog, @@ -413,26 +415,31 @@ impl Subcommand { ) { if let Some(doc) = doc { if !doc.is_empty() && doc.lines().count() <= 1 { + let color = config.color.stdout(); print!( "{:padding$}{} ", "", - config.color.stdout().doc().paint("#"), + color.doc().paint("#"), padding = max_signature_width.saturating_sub(signature_widths[name]) + 1, ); - let mut within_backticks = false; - for chunk in doc.split_inclusive('`') { - if within_backticks { - let color = config.color.stdout().doc_backtick(); - print!("{}{}", color.paint("`"), color.paint(chunk),); - } else { - let color = config.color.stdout().doc(); - print!("{}", color.paint(chunk.split('`').next().unwrap())); + let mut end = 0; + for backtick in BACKTICK_RE.find_iter(doc) { + let prefix = &doc[end..backtick.start()]; + if !prefix.is_empty() { + print!("{}", color.doc().paint(prefix)); } - within_backticks = !within_backticks; + print!("{}", color.doc_backtick().paint(backtick.as_str())); + end = backtick.end(); + } + + let suffix = &doc[end..]; + if !suffix.is_empty() { + print!("{}", color.doc().paint(suffix)); } } } + println!(); } diff --git a/tests/list.rs b/tests/list.rs index 974ca75f62..396a337afd 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -449,7 +449,11 @@ fn backticks_highlighted() { ", ) .args(["--list", "--color=always"]) - .stdout("Available recipes:\n recipe \u{1b}[34m#\u{1b}[0m \u{1b}[34mComment \u{1b}[0m\u{1b}[40;37m`\u{1b}[0m\u{1b}[40;37m`\u{1b}[0m\u{1b}[34m \u{1b}[0m\u{1b}[40;37m`\u{1b}[0m\u{1b}[40;37mwith backticks`\u{1b}[0m\n") + .stdout( + " + Available recipes: + recipe \u{1b}[34m#\u{1b}[0m \u{1b}[34mComment \u{1b}[0m\u{1b}[40;37m``\u{1b}[0m\u{1b}[34m \u{1b}[0m\u{1b}[40;37m`with backticks`\u{1b}[0m + ") .run(); } @@ -463,6 +467,10 @@ fn unclosed_backticks() { ", ) .args(["--list", "--color=always"]) - .stdout("Available recipes:\n recipe \u{1b}[34m#\u{1b}[0m \u{1b}[34mComment \u{1b}[0m\u{1b}[40;37m`\u{1b}[0m\u{1b}[40;37mwith unclosed backick\u{1b}[0m\n") + .stdout( + " + Available recipes: + recipe \u{1b}[34m#\u{1b}[0m \u{1b}[34mComment \u{1b}[0m\u{1b}[40;37m`with unclosed backick\u{1b}[0m + ") .run(); }