Skip to content

Commit

Permalink
Highlight backticks in docs when listing recipes (#2423)
Browse files Browse the repository at this point in the history
  • Loading branch information
neunenak authored Nov 9, 2024
1 parent a93b0bf commit 1ae6a6d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fuzz:
run:
cargo run

# only run tests matching PATTERN
# only run tests matching `PATTERN`
[group: 'test']
filter PATTERN:
cargo test {{PATTERN}}
Expand Down
4 changes: 4 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl Color {
self.restyle(Style::new().fg(Blue))
}

pub(crate) fn doc_backtick(self) -> Self {
self.restyle(Style::new().fg(White).on(Black))
}

pub(crate) fn error(self) -> Self {
self.restyle(Style::new().fg(Red).bold())
}
Expand Down
27 changes: 24 additions & 3 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ use {super::*, clap_mangen::Man};

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

fn backtick_re() -> &'static Regex {
static BACKTICK_RE: OnceLock<Regex> = OnceLock::new();
BACKTICK_RE.get_or_init(|| Regex::new("(`.*?`)|(`[^`]*$)").unwrap())
}

#[derive(PartialEq, Clone, Debug)]
pub(crate) enum Subcommand {
Changelog,
Expand Down Expand Up @@ -413,15 +418,31 @@ impl Subcommand {
) {
if let Some(doc) = doc {
if !doc.is_empty() && doc.lines().count() <= 1 {
let color = config.color.stdout();
print!(
"{:padding$}{} {}",
"{:padding$}{} ",
"",
config.color.stdout().doc().paint("#"),
config.color.stdout().doc().paint(doc),
color.doc().paint("#"),
padding = max_signature_width.saturating_sub(signature_widths[name]) + 1,
);

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));
}
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
36 changes: 36 additions & 0 deletions tests/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,39 @@ fn no_space_before_submodules_not_following_groups() {
)
.run();
}

#[test]
fn backticks_highlighted() {
Test::new()
.justfile(
"
# Comment `` `with backticks` and trailing text
recipe:
",
)
.args(["--list", "--color=always"])
.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\u{1b}[34m and trailing text\u{1b}[0m
")
.run();
}

#[test]
fn unclosed_backticks() {
Test::new()
.justfile(
"
# Comment `with unclosed backick
recipe:
",
)
.args(["--list", "--color=always"])
.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 1ae6a6d

Please sign in to comment.