Skip to content

Commit

Permalink
Condense aliases into the recipe doc
Browse files Browse the repository at this point in the history
  • Loading branch information
marcaddeo committed Sep 3, 2024
1 parent f222b02 commit 1daa64f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 53 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub(crate) use {
},
snafu::{ResultExt, Snafu},
std::{
borrow::Cow,
cmp,
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
env,
Expand Down
97 changes: 56 additions & 41 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,20 +447,38 @@ impl Subcommand {
config: &Config,
name: &str,
doc: Option<&str>,
aliases: Option<Vec<&str>>,
max_signature_width: usize,
signature_widths: &BTreeMap<&str, usize>,
) {
if let Some(doc) = doc {
if !doc.is_empty() && doc.lines().count() <= 1 {
print!(
"{:padding$}{} {}",
"",
config.color.stdout().doc().paint("#"),
config.color.stdout().doc().paint(doc),
padding = max_signature_width.saturating_sub(signature_widths[name]) + 1,
);
}
let doc = doc.unwrap_or("");
let aliases = aliases.unwrap_or(Vec::new());
let print_doc = !doc.is_empty() && doc.lines().count() <= 1;

if print_doc || !aliases.is_empty() {
print!(
"{:padding$}{}",
"",
config.color.stdout().doc().paint("#"),
padding = max_signature_width.saturating_sub(signature_widths[name]) + 1,
);
}

if print_doc {
print!(" {}", config.color.stdout().doc().paint(doc),);
}

if !aliases.is_empty() {
print!(
" {}",
config
.color
.stdout()
.doc()
.paint(&format!("[aliases: {}]", aliases.join(", ")))
);
}

println!();
}

Expand Down Expand Up @@ -581,41 +599,37 @@ impl Subcommand {

if let Some(recipes) = recipe_groups.get(&group) {
for recipe in recipes {
for (i, name) in iter::once(&recipe.name())
.chain(aliases.get(recipe.name()).unwrap_or(&Vec::new()))
.enumerate()
{
let doc = if i == 0 {
recipe.doc().map(Cow::Borrowed)
} else {
Some(Cow::Owned(format!("alias for `{}`", recipe.name)))
};

if let Some(doc) = &doc {
if doc.lines().count() > 1 {
for line in doc.lines() {
println!(
"{list_prefix}{} {}",
config.color.stdout().doc().paint("#"),
config.color.stdout().doc().paint(line),
);
}
let doc = recipe.doc();

if let Some(doc) = &doc {
if doc.lines().count() > 1 {
for line in doc.lines() {
println!(
"{list_prefix}{} {}",
config.color.stdout().doc().paint("#"),
config.color.stdout().doc().paint(line),
);
}
}
}

print!(
"{list_prefix}{}",
RecipeSignature { name, recipe }.color_display(config.color.stdout())
);
print!(
"{list_prefix}{}",
RecipeSignature {
name: recipe.name(),
recipe
}
.color_display(config.color.stdout())
);

format_doc(
config,
name,
doc.as_deref(),
max_signature_width,
&signature_widths,
);
}
format_doc(
config,
recipe.name(),
doc.as_deref(),
aliases.get(recipe.name()).cloned(),
max_signature_width,
&signature_widths,
);
}
}

Expand All @@ -634,6 +648,7 @@ impl Subcommand {
config,
submodule.name(),
submodule.doc.as_deref(),
None,
max_signature_width,
&signature_widths,
);
Expand Down
32 changes: 21 additions & 11 deletions tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,23 @@ test! {
args: ("--list"),
stdout: "
Available recipes:
foo
f # alias for `foo`
foo # [aliases: f]
",
}

test! {
name: alias_listing_with_doc,
justfile: "
# foo command
foo:
echo foo
alias f := foo
",
args: ("--list"),
stdout: "
Available recipes:
foo # foo command [aliases: f]
",
}

Expand All @@ -22,9 +37,7 @@ test! {
args: ("--list"),
stdout: "
Available recipes:
foo
f # alias for `foo`
fo # alias for `foo`
foo # [aliases: f, fo]
",
}

Expand All @@ -34,8 +47,7 @@ test! {
args: ("--list"),
stdout: "
Available recipes:
foo PARAM='foo'
f PARAM='foo' # alias for `foo`
foo PARAM='foo' # [aliases: f]
",
}

Expand Down Expand Up @@ -927,8 +939,7 @@ a:
stdout: r"
Available recipes:
a
b
c # alias for `b`
b # [aliases: c]
",
}

Expand All @@ -942,8 +953,7 @@ a:
args: ("--list", "--unsorted"),
stdout: r"
Available recipes:
b
c # alias for `b`
b # [aliases: c]
a
",
}
Expand Down

0 comments on commit 1daa64f

Please sign in to comment.