From 067347911d335d8900051442fdb9e062cf03254c Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Tue, 3 Sep 2024 12:02:00 -0400 Subject: [PATCH] Condense aliases into the recipe doc --- src/lib.rs | 1 - src/subcommand.rs | 97 +++++++++++++++++++++++++++-------------------- tests/misc.rs | 32 ++++++++++------ 3 files changed, 77 insertions(+), 53 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 75e3332be4..fe90bdc468 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,7 +46,6 @@ pub(crate) use { }, snafu::{ResultExt, Snafu}, std::{ - borrow::Cow, cmp, collections::{BTreeMap, BTreeSet, HashMap, HashSet}, env, diff --git a/src/subcommand.rs b/src/subcommand.rs index 9e7486863d..a5d43ff42a 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -447,20 +447,38 @@ impl Subcommand { config: &Config, name: &str, doc: Option<&str>, + aliases: Option>, 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!(); } @@ -584,41 +602,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, + ); } } @@ -637,6 +651,7 @@ impl Subcommand { config, submodule.name(), submodule.doc.as_deref(), + None, max_signature_width, &signature_widths, ); diff --git a/tests/misc.rs b/tests/misc.rs index b4c256370c..818f4228f6 100644 --- a/tests/misc.rs +++ b/tests/misc.rs @@ -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] ", } @@ -22,9 +37,7 @@ test! { args: ("--list"), stdout: " Available recipes: - foo - f # alias for `foo` - fo # alias for `foo` + foo # [aliases: f, fo] ", } @@ -34,8 +47,7 @@ test! { args: ("--list"), stdout: " Available recipes: - foo PARAM='foo' - f PARAM='foo' # alias for `foo` + foo PARAM='foo' # [aliases: f] ", } @@ -927,8 +939,7 @@ a: stdout: r" Available recipes: a - b - c # alias for `b` + b # [aliases: c] ", } @@ -942,8 +953,7 @@ a: args: ("--list", "--unsorted"), stdout: r" Available recipes: - b - c # alias for `b` + b # [aliases: c] a ", }