Skip to content

Commit

Permalink
Search for backticks using regex
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Nov 9, 2024
1 parent 98f982f commit f805cb7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
27 changes: 17 additions & 10 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use {super::*, clap_mangen::Man};

const INIT_JUSTFILE: &str = "default:\n echo 'Hello, world!'\n";

static BACKTICK_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new("(`.*?`)|(`[^`]*$)").unwrap());

#[derive(PartialEq, Clone, Debug)]
pub(crate) enum Subcommand {
Changelog,
Expand Down Expand Up @@ -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!();
}

Expand Down
12 changes: 10 additions & 2 deletions tests/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}

0 comments on commit f805cb7

Please sign in to comment.