From e82934c0f774533a0bf73dc62f4f0046271594a2 Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Tue, 3 Sep 2024 12:02:00 -0400 Subject: [PATCH 01/14] 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 75e3332b..fe90bdc4 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 b1f13592..37607839 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -408,20 +408,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!(); } @@ -545,41 +563,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))) - }; + 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), - ); - } + 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()) - ); - - format_doc( - config, - name, - doc.as_deref(), - max_signature_width, - &signature_widths, - ); } + + print!( + "{list_prefix}{}", + RecipeSignature { + name: recipe.name(), + recipe + } + .color_display(config.color.stdout()) + ); + + format_doc( + config, + recipe.name(), + doc.as_deref(), + aliases.get(recipe.name()).cloned(), + max_signature_width, + &signature_widths, + ); } } @@ -598,6 +612,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 590c13cd..a78f5563 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 ", } From 99c1b23cebdebc469600c63740e1ffde8db9237b Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Tue, 3 Sep 2024 18:35:51 -0400 Subject: [PATCH 02/14] Fix clippy errors --- src/subcommand.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/subcommand.rs b/src/subcommand.rs index 37607839..96b5413f 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -413,7 +413,7 @@ impl Subcommand { signature_widths: &BTreeMap<&str, usize>, ) { let doc = doc.unwrap_or(""); - let aliases = aliases.unwrap_or(Vec::new()); + let aliases = aliases.unwrap_or_default(); let print_doc = !doc.is_empty() && doc.lines().count() <= 1; if print_doc || !aliases.is_empty() { @@ -589,7 +589,7 @@ impl Subcommand { format_doc( config, recipe.name(), - doc.as_deref(), + doc, aliases.get(recipe.name()).cloned(), max_signature_width, &signature_widths, From 1890b94bfa3296a153b2ee832818043f53a4cbfc Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Sat, 7 Sep 2024 12:34:57 -0400 Subject: [PATCH 03/14] Paint aliases in --list purple --- src/color.rs | 4 ++++ src/subcommand.rs | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/color.rs b/src/color.rs index ccdf2185..cc454ea6 100644 --- a/src/color.rs +++ b/src/color.rs @@ -66,6 +66,10 @@ impl Color { self.restyle(Style::new().fg(Blue)) } + pub(crate) fn alias(self) -> Self { + self.restyle(Style::new().fg(Purple)) + } + pub(crate) fn error(self) -> Self { self.restyle(Style::new().fg(Red).bold()) } diff --git a/src/subcommand.rs b/src/subcommand.rs index 96b5413f..932ae572 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -435,7 +435,7 @@ impl Subcommand { config .color .stdout() - .doc() + .alias() .paint(&format!("[aliases: {}]", aliases.join(", "))) ); } From b03ab8c9a47b9ee2c0d4f4bc2862fea295bed8dd Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Thu, 12 Sep 2024 11:23:02 -0400 Subject: [PATCH 04/14] Use unwrap_or_default --- src/subcommand.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/subcommand.rs b/src/subcommand.rs index 932ae572..ec99b48b 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -412,7 +412,7 @@ impl Subcommand { max_signature_width: usize, signature_widths: &BTreeMap<&str, usize>, ) { - let doc = doc.unwrap_or(""); + let doc = doc.unwrap_or_default(); let aliases = aliases.unwrap_or_default(); let print_doc = !doc.is_empty() && doc.lines().count() <= 1; From 9757b70aa9b89d720ec1eb8ca3ca8fadad653b43 Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Thu, 12 Sep 2024 11:24:21 -0400 Subject: [PATCH 05/14] Add --no-inline-aliases option --- src/config.rs | 10 ++++++++ src/lib.rs | 1 + src/subcommand.rs | 62 +++++++++++++++++++++++++++-------------------- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/config.rs b/src/config.rs index d0b91d67..cdf50d66 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,6 +24,7 @@ pub(crate) struct Config { pub(crate) list_submodules: bool, pub(crate) load_dotenv: bool, pub(crate) no_aliases: bool, + pub(crate) no_inline_aliases: bool, pub(crate) no_dependencies: bool, pub(crate) one: bool, pub(crate) search_config: SearchConfig, @@ -98,6 +99,7 @@ mod arg { pub(crate) const LIST_PREFIX: &str = "LIST-PREFIX"; pub(crate) const LIST_SUBMODULES: &str = "LIST-SUBMODULES"; pub(crate) const NO_ALIASES: &str = "NO-ALIASES"; + pub(crate) const NO_INLINE_ALIASES: &str = "NO-INLINE-ALIASES"; pub(crate) const NO_DEPS: &str = "NO-DEPS"; pub(crate) const NO_DOTENV: &str = "NO-DOTENV"; pub(crate) const NO_HIGHLIGHT: &str = "NO-HIGHLIGHT"; @@ -276,6 +278,13 @@ impl Config { .action(ArgAction::SetTrue) .help("Don't show aliases in list"), ) + .arg( + Arg::new(arg::NO_INLINE_ALIASES) + .long("no-inline-aliases") + .env("JUST_NO_INLINE_ALIASES") + .action(ArgAction::SetTrue) + .help("Don't show aliases inline with recipe docs in list"), + ) .arg( Arg::new(arg::NO_DEPS) .long("no-deps") @@ -729,6 +738,7 @@ impl Config { list_submodules: matches.get_flag(arg::LIST_SUBMODULES), load_dotenv: !matches.get_flag(arg::NO_DOTENV), no_aliases: matches.get_flag(arg::NO_ALIASES), + no_inline_aliases: matches.get_flag(arg::NO_INLINE_ALIASES), no_dependencies: matches.get_flag(arg::NO_DEPS), one: matches.get_flag(arg::ONE), search_config, diff --git a/src/lib.rs b/src/lib.rs index fe90bdc4..75e3332b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,6 +46,7 @@ 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 ec99b48b..eacc3c4b 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -563,37 +563,47 @@ impl Subcommand { if let Some(recipes) = recipe_groups.get(&group) { for recipe in recipes { - let doc = recipe.doc(); + 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), - ); + 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.name(), - recipe + if i == 0 || config.no_inline_aliases { + print!( + "{list_prefix}{}", + RecipeSignature { name, recipe }.color_display(config.color.stdout()) + ); + + format_doc( + config, + name, + doc.as_deref(), + aliases + .get(recipe.name()) + .filter(|_| !config.no_inline_aliases) + .cloned(), + max_signature_width, + &signature_widths, + ); } - .color_display(config.color.stdout()) - ); - - format_doc( - config, - recipe.name(), - doc, - aliases.get(recipe.name()).cloned(), - max_signature_width, - &signature_widths, - ); + } } } From 699f07f56393dd989a93ab6bcd4d9aa84f7fe0c4 Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Wed, 25 Sep 2024 13:31:13 -0400 Subject: [PATCH 06/14] Use a Vec for aliases in format_doc instead of Option --- src/subcommand.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/subcommand.rs b/src/subcommand.rs index eacc3c4b..4d44275f 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -408,15 +408,14 @@ impl Subcommand { config: &Config, name: &str, doc: Option<&str>, - aliases: Option>, + aliases: &Vec<&str>, max_signature_width: usize, signature_widths: &BTreeMap<&str, usize>, ) { let doc = doc.unwrap_or_default(); - let aliases = aliases.unwrap_or_default(); let print_doc = !doc.is_empty() && doc.lines().count() <= 1; - if print_doc || !aliases.is_empty() { + if print_doc || (!config.no_inline_aliases && !aliases.is_empty()) { print!( "{:padding$}{}", "", @@ -429,7 +428,7 @@ impl Subcommand { print!(" {}", config.color.stdout().doc().paint(doc),); } - if !aliases.is_empty() { + if !aliases.is_empty() && !config.no_inline_aliases { print!( " {}", config @@ -595,10 +594,7 @@ impl Subcommand { config, name, doc.as_deref(), - aliases - .get(recipe.name()) - .filter(|_| !config.no_inline_aliases) - .cloned(), + aliases.get(recipe.name()).unwrap_or(&Vec::new()), max_signature_width, &signature_widths, ); @@ -622,7 +618,7 @@ impl Subcommand { config, submodule.name(), submodule.doc.as_deref(), - None, + &Vec::new(), max_signature_width, &signature_widths, ); From f91bf369dba0265a9777fb84e025172d45e8f4cb Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Wed, 25 Sep 2024 13:36:27 -0400 Subject: [PATCH 07/14] Fix clippy lint --- src/subcommand.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/subcommand.rs b/src/subcommand.rs index 4d44275f..7cb93c1d 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -408,7 +408,7 @@ impl Subcommand { config: &Config, name: &str, doc: Option<&str>, - aliases: &Vec<&str>, + aliases: &[&str], max_signature_width: usize, signature_widths: &BTreeMap<&str, usize>, ) { From 5bbaee1f0544c623c2e033a7b31d93528c40cd6c Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Wed, 25 Sep 2024 14:13:43 -0400 Subject: [PATCH 08/14] Alter the iterator instead of multiple conditionals --- src/subcommand.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/subcommand.rs b/src/subcommand.rs index 7cb93c1d..48c576d6 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -562,8 +562,14 @@ impl Subcommand { if let Some(recipes) = recipe_groups.get(&group) { for recipe in recipes { + let recipe_alias_entries = if config.no_inline_aliases { + aliases.get(recipe.name()) + } else { + None + }; + for (i, name) in iter::once(&recipe.name()) - .chain(aliases.get(recipe.name()).unwrap_or(&Vec::new())) + .chain(recipe_alias_entries.unwrap_or(&Vec::new())) .enumerate() { let doc = if i == 0 { @@ -584,21 +590,19 @@ impl Subcommand { } } - if i == 0 || config.no_inline_aliases { - print!( - "{list_prefix}{}", - RecipeSignature { name, recipe }.color_display(config.color.stdout()) - ); + print!( + "{list_prefix}{}", + RecipeSignature { name, recipe }.color_display(config.color.stdout()) + ); - format_doc( - config, - name, - doc.as_deref(), - aliases.get(recipe.name()).unwrap_or(&Vec::new()), - max_signature_width, - &signature_widths, - ); - } + format_doc( + config, + name, + doc.as_deref(), + aliases.get(recipe.name()).unwrap_or(&Vec::new()), + max_signature_width, + &signature_widths, + ); } } } From 8290c02d411047c1524dee5f9511be2401dc83c8 Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Wed, 25 Sep 2024 14:18:10 -0400 Subject: [PATCH 09/14] Convert test format --- tests/misc.rs | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/tests/misc.rs b/tests/misc.rs index a78f5563..db2eb552 100644 --- a/tests/misc.rs +++ b/tests/misc.rs @@ -15,20 +15,27 @@ test! { ", } -test! { - name: alias_listing_with_doc, - justfile: " - # foo command - foo: - echo foo +#[test] +fn alias_listing_with_doc() { + Test::new() + .justfile( + " + # foo command + foo: + echo foo - alias f := foo - ", - args: ("--list"), - stdout: " - Available recipes: - foo # foo command [aliases: f] - ", + alias f := foo + ", + ) + .arg("--list") + .stdout( + " + Available recipes: + foo # foo command [aliases: f] + ", + ) + .status(EXIT_SUCCESS) + .run(); } test! { From eaeb898711a75fe7dee5386affdf2d17fc22a134 Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Wed, 25 Sep 2024 14:20:19 -0400 Subject: [PATCH 10/14] Add test for --no-inline-aliases --- tests/lib.rs | 1 + tests/no_inline_aliases.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 tests/no_inline_aliases.rs diff --git a/tests/lib.rs b/tests/lib.rs index ec71c665..0bb48212 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -82,6 +82,7 @@ mod no_aliases; mod no_cd; mod no_dependencies; mod no_exit_message; +mod no_inline_aliases; mod os_attributes; mod parameters; mod parser; diff --git a/tests/no_inline_aliases.rs b/tests/no_inline_aliases.rs new file mode 100644 index 00000000..41949337 --- /dev/null +++ b/tests/no_inline_aliases.rs @@ -0,0 +1,20 @@ +use super::*; + +#[test] +fn no_inline_aliases_flag() { + Test::new() + .justfile( + " + alias t := test1 + + test1: + @echo 'test1' + + test2: + @echo 'test2' + ", + ) + .args(["--no-inline-aliases", "--list"]) + .stdout("Available recipes:\n test1\n t # alias for `test1`\n test2\n") + .run(); +} From 3a401325edd7af7a2bb54e44478e16ea29fd1580 Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Wed, 25 Sep 2024 15:58:19 -0400 Subject: [PATCH 11/14] Move NO_INLINE_ALIASES to be in alpha order, and make it conflict with --no-aliases --- src/config.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index cdf50d66..f3ecd87d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -99,10 +99,10 @@ mod arg { pub(crate) const LIST_PREFIX: &str = "LIST-PREFIX"; pub(crate) const LIST_SUBMODULES: &str = "LIST-SUBMODULES"; pub(crate) const NO_ALIASES: &str = "NO-ALIASES"; - pub(crate) const NO_INLINE_ALIASES: &str = "NO-INLINE-ALIASES"; pub(crate) const NO_DEPS: &str = "NO-DEPS"; pub(crate) const NO_DOTENV: &str = "NO-DOTENV"; pub(crate) const NO_HIGHLIGHT: &str = "NO-HIGHLIGHT"; + pub(crate) const NO_INLINE_ALIASES: &str = "NO-INLINE-ALIASES"; pub(crate) const ONE: &str = "ONE"; pub(crate) const QUIET: &str = "QUIET"; pub(crate) const SET: &str = "SET"; @@ -283,7 +283,8 @@ impl Config { .long("no-inline-aliases") .env("JUST_NO_INLINE_ALIASES") .action(ArgAction::SetTrue) - .help("Don't show aliases inline with recipe docs in list"), + .help("Don't show aliases inline with recipe docs in list") + .conflicts_with(arg::NO_ALIASES), ) .arg( Arg::new(arg::NO_DEPS) From bcc812ad25f3a3ecc790d1eabf1c193a8ea38f23 Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Wed, 25 Sep 2024 16:07:37 -0400 Subject: [PATCH 12/14] Add --inline-aliases-left flag to display aliases to the left of the doc --- src/config.rs | 12 ++++++++++++ src/subcommand.rs | 32 ++++++++++++++++++++++---------- tests/list.rs | 20 ++++++++++++++++++++ 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/config.rs b/src/config.rs index f3ecd87d..d5b98503 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,6 +18,7 @@ pub(crate) struct Config { pub(crate) dump_format: DumpFormat, pub(crate) explain: bool, pub(crate) highlight: bool, + pub(crate) inline_aliases_left: bool, pub(crate) invocation_directory: PathBuf, pub(crate) list_heading: String, pub(crate) list_prefix: String, @@ -94,6 +95,7 @@ mod arg { pub(crate) const EXPLAIN: &str = "EXPLAIN"; pub(crate) const GLOBAL_JUSTFILE: &str = "GLOBAL-JUSTFILE"; pub(crate) const HIGHLIGHT: &str = "HIGHLIGHT"; + pub(crate) const INLINE_ALIASES_LEFT: &str = "INLINE-ALIASES-LEFT"; pub(crate) const JUSTFILE: &str = "JUSTFILE"; pub(crate) const LIST_HEADING: &str = "LIST-HEADING"; pub(crate) const LIST_PREFIX: &str = "LIST-PREFIX"; @@ -236,6 +238,15 @@ impl Config { .help("Highlight echoed recipe lines in bold") .overrides_with(arg::NO_HIGHLIGHT), ) + .arg( + Arg::new(arg::INLINE_ALIASES_LEFT) + .long("inline-aliases-left") + .env("JUST_INLINE_ALIASES_LEFT") + .action(ArgAction::SetTrue) + .help("Display inlined recipe aliases to the left of their doc in listing") + .conflicts_with(arg::NO_ALIASES) + .conflicts_with(arg::NO_INLINE_ALIASES), + ) .arg( Arg::new(arg::JUSTFILE) .short('f') @@ -733,6 +744,7 @@ impl Config { .clone(), explain, highlight: !matches.get_flag(arg::NO_HIGHLIGHT), + inline_aliases_left: matches.get_flag(arg::INLINE_ALIASES_LEFT), invocation_directory: env::current_dir().context(config_error::CurrentDirContext)?, list_heading: matches.get_one::(arg::LIST_HEADING).unwrap().into(), list_prefix: matches.get_one::(arg::LIST_PREFIX).unwrap().into(), diff --git a/src/subcommand.rs b/src/subcommand.rs index 48c576d6..c33a3741 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -414,8 +414,9 @@ impl Subcommand { ) { let doc = doc.unwrap_or_default(); let print_doc = !doc.is_empty() && doc.lines().count() <= 1; + let print_aliases = !config.no_inline_aliases && !aliases.is_empty(); - if print_doc || (!config.no_inline_aliases && !aliases.is_empty()) { + if print_doc || print_aliases { print!( "{:padding$}{}", "", @@ -424,18 +425,29 @@ impl Subcommand { ); } - if print_doc { - print!(" {}", config.color.stdout().doc().paint(doc),); - } + let doc = print_doc.then_some(format!("{}", config.color.stdout().doc().paint(doc))); + let aliases = print_aliases.then_some(format!( + "{}", + config + .color + .stdout() + .alias() + .paint(&format!("[aliases: {}]", aliases.join(", "))) + )); - if !aliases.is_empty() && !config.no_inline_aliases { + let (left, right) = if config.inline_aliases_left { + (aliases, doc) + } else { + (doc, aliases) + }; + + if print_doc || print_aliases { print!( " {}", - config - .color - .stdout() - .alias() - .paint(&format!("[aliases: {}]", aliases.join(", "))) + [left, right] + .map(|s| s.unwrap_or_default()) + .join(" ") + .trim() ); } diff --git a/tests/list.rs b/tests/list.rs index db7b669e..5190c3d3 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -438,3 +438,23 @@ fn no_space_before_submodules_not_following_groups() { ) .run(); } + +#[test] +fn inline_aliases_left_flag() { + Test::new() + .justfile( + " + alias t := test1 + + # I'm a recipe + test1: + @echo 'test1' + + test2: + @echo 'test2' + ", + ) + .args(["--inline-aliases-left", "--list"]) + .stdout("Available recipes:\n test1 # [aliases: t] I'm a recipe\n test2\n") + .run(); +} From 7cebc58522757f7e64227e426b843c9d6f529ba5 Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Wed, 25 Sep 2024 16:12:53 -0400 Subject: [PATCH 13/14] Fix clippy error --- src/subcommand.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/subcommand.rs b/src/subcommand.rs index c33a3741..e5ea2879 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -445,7 +445,7 @@ impl Subcommand { print!( " {}", [left, right] - .map(|s| s.unwrap_or_default()) + .map(Option::unwrap_or_default) .join(" ") .trim() ); From 450581668018fbb186852e2571e3b6fe91e25a21 Mon Sep 17 00:00:00 2001 From: Marc Addeo Date: Tue, 8 Oct 2024 18:24:30 -0400 Subject: [PATCH 14/14] Switch to --alias-style flag --- src/alias_style.rs | 8 +++++ src/config.rs | 39 ++++++++++--------------- src/lib.rs | 21 ++++++------- src/subcommand.rs | 6 ++-- tests/alias_style.rs | 60 ++++++++++++++++++++++++++++++++++++++ tests/lib.rs | 2 +- tests/list.rs | 20 ------------- tests/no_inline_aliases.rs | 20 ------------- 8 files changed, 99 insertions(+), 77 deletions(-) create mode 100644 src/alias_style.rs create mode 100644 tests/alias_style.rs delete mode 100644 tests/no_inline_aliases.rs diff --git a/src/alias_style.rs b/src/alias_style.rs new file mode 100644 index 00000000..ade5b2fe --- /dev/null +++ b/src/alias_style.rs @@ -0,0 +1,8 @@ +use super::*; + +#[derive(Debug, PartialEq, Clone, ValueEnum)] +pub(crate) enum AliasStyle { + Inline, + InlineLeft, + Recipe, +} diff --git a/src/config.rs b/src/config.rs index d5b98503..ca4c58e8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,6 +9,7 @@ use { #[derive(Debug, PartialEq)] pub(crate) struct Config { + pub(crate) alias_style: AliasStyle, pub(crate) check: bool, pub(crate) color: Color, pub(crate) command_color: Option, @@ -18,14 +19,12 @@ pub(crate) struct Config { pub(crate) dump_format: DumpFormat, pub(crate) explain: bool, pub(crate) highlight: bool, - pub(crate) inline_aliases_left: bool, pub(crate) invocation_directory: PathBuf, pub(crate) list_heading: String, pub(crate) list_prefix: String, pub(crate) list_submodules: bool, pub(crate) load_dotenv: bool, pub(crate) no_aliases: bool, - pub(crate) no_inline_aliases: bool, pub(crate) no_dependencies: bool, pub(crate) one: bool, pub(crate) search_config: SearchConfig, @@ -82,6 +81,7 @@ mod cmd { } mod arg { + pub(crate) const ALIAS_STYLE: &str = "ALIAS_STYLE"; pub(crate) const ARGUMENTS: &str = "ARGUMENTS"; pub(crate) const CHECK: &str = "CHECK"; pub(crate) const CHOOSER: &str = "CHOOSER"; @@ -95,7 +95,6 @@ mod arg { pub(crate) const EXPLAIN: &str = "EXPLAIN"; pub(crate) const GLOBAL_JUSTFILE: &str = "GLOBAL-JUSTFILE"; pub(crate) const HIGHLIGHT: &str = "HIGHLIGHT"; - pub(crate) const INLINE_ALIASES_LEFT: &str = "INLINE-ALIASES-LEFT"; pub(crate) const JUSTFILE: &str = "JUSTFILE"; pub(crate) const LIST_HEADING: &str = "LIST-HEADING"; pub(crate) const LIST_PREFIX: &str = "LIST-PREFIX"; @@ -104,7 +103,6 @@ mod arg { pub(crate) const NO_DEPS: &str = "NO-DEPS"; pub(crate) const NO_DOTENV: &str = "NO-DOTENV"; pub(crate) const NO_HIGHLIGHT: &str = "NO-HIGHLIGHT"; - pub(crate) const NO_INLINE_ALIASES: &str = "NO-INLINE-ALIASES"; pub(crate) const ONE: &str = "ONE"; pub(crate) const QUIET: &str = "QUIET"; pub(crate) const SET: &str = "SET"; @@ -139,6 +137,16 @@ impl Config { .placeholder(AnsiColor::Green.on_default()) .usage(AnsiColor::Yellow.on_default()), ) + .arg( + Arg::new(arg::ALIAS_STYLE) + .long("alias-style") + .env("JUST_ALIAS_STYLE") + .action(ArgAction::Set) + .value_parser(clap::value_parser!(AliasStyle)) + .default_value("inline") + .help("Set the style that the list command will display aliases") + .conflicts_with(arg::NO_ALIASES), + ) .arg( Arg::new(arg::CHECK) .long("check") @@ -238,15 +246,6 @@ impl Config { .help("Highlight echoed recipe lines in bold") .overrides_with(arg::NO_HIGHLIGHT), ) - .arg( - Arg::new(arg::INLINE_ALIASES_LEFT) - .long("inline-aliases-left") - .env("JUST_INLINE_ALIASES_LEFT") - .action(ArgAction::SetTrue) - .help("Display inlined recipe aliases to the left of their doc in listing") - .conflicts_with(arg::NO_ALIASES) - .conflicts_with(arg::NO_INLINE_ALIASES), - ) .arg( Arg::new(arg::JUSTFILE) .short('f') @@ -289,14 +288,6 @@ impl Config { .action(ArgAction::SetTrue) .help("Don't show aliases in list"), ) - .arg( - Arg::new(arg::NO_INLINE_ALIASES) - .long("no-inline-aliases") - .env("JUST_NO_INLINE_ALIASES") - .action(ArgAction::SetTrue) - .help("Don't show aliases inline with recipe docs in list") - .conflicts_with(arg::NO_ALIASES), - ) .arg( Arg::new(arg::NO_DEPS) .long("no-deps") @@ -727,6 +718,10 @@ impl Config { let explain = matches.get_flag(arg::EXPLAIN); Ok(Self { + alias_style: matches + .get_one::(arg::ALIAS_STYLE) + .unwrap() + .clone(), check: matches.get_flag(arg::CHECK), color: (*matches.get_one::(arg::COLOR).unwrap()).into(), command_color: matches @@ -744,14 +739,12 @@ impl Config { .clone(), explain, highlight: !matches.get_flag(arg::NO_HIGHLIGHT), - inline_aliases_left: matches.get_flag(arg::INLINE_ALIASES_LEFT), invocation_directory: env::current_dir().context(config_error::CurrentDirContext)?, list_heading: matches.get_one::(arg::LIST_HEADING).unwrap().into(), list_prefix: matches.get_one::(arg::LIST_PREFIX).unwrap().into(), list_submodules: matches.get_flag(arg::LIST_SUBMODULES), load_dotenv: !matches.get_flag(arg::NO_DOTENV), no_aliases: matches.get_flag(arg::NO_ALIASES), - no_inline_aliases: matches.get_flag(arg::NO_INLINE_ALIASES), no_dependencies: matches.get_flag(arg::NO_DEPS), one: matches.get_flag(arg::ONE), search_config, diff --git a/src/lib.rs b/src/lib.rs index 75e3332b..ad0ce0d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,16 +6,16 @@ pub(crate) use { crate::{ - alias::Alias, analyzer::Analyzer, argument_parser::ArgumentParser, assignment::Assignment, - assignment_resolver::AssignmentResolver, ast::Ast, attribute::Attribute, binding::Binding, - color::Color, color_display::ColorDisplay, command_color::CommandColor, - command_ext::CommandExt, compilation::Compilation, compile_error::CompileError, - compile_error_kind::CompileErrorKind, compiler::Compiler, condition::Condition, - conditional_operator::ConditionalOperator, config::Config, config_error::ConfigError, - constants::constants, count::Count, delimiter::Delimiter, dependency::Dependency, - dump_format::DumpFormat, enclosure::Enclosure, error::Error, evaluator::Evaluator, - execution_context::ExecutionContext, executor::Executor, expression::Expression, - fragment::Fragment, function::Function, interpreter::Interpreter, + alias::Alias, alias_style::AliasStyle, analyzer::Analyzer, argument_parser::ArgumentParser, + assignment::Assignment, assignment_resolver::AssignmentResolver, ast::Ast, + attribute::Attribute, binding::Binding, color::Color, color_display::ColorDisplay, + command_color::CommandColor, command_ext::CommandExt, compilation::Compilation, + compile_error::CompileError, compile_error_kind::CompileErrorKind, compiler::Compiler, + condition::Condition, conditional_operator::ConditionalOperator, config::Config, + config_error::ConfigError, constants::constants, count::Count, delimiter::Delimiter, + dependency::Dependency, dump_format::DumpFormat, enclosure::Enclosure, error::Error, + evaluator::Evaluator, execution_context::ExecutionContext, executor::Executor, + expression::Expression, fragment::Fragment, function::Function, interpreter::Interpreter, interrupt_guard::InterruptGuard, interrupt_handler::InterruptHandler, item::Item, justfile::Justfile, keyed::Keyed, keyword::Keyword, lexer::Lexer, line::Line, list::List, load_dotenv::load_dotenv, loader::Loader, module_path::ModulePath, name::Name, @@ -107,6 +107,7 @@ pub mod fuzzing; pub mod summary; mod alias; +mod alias_style; mod analyzer; mod argument_parser; mod assignment; diff --git a/src/subcommand.rs b/src/subcommand.rs index e5ea2879..687a7a3e 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -414,7 +414,7 @@ impl Subcommand { ) { let doc = doc.unwrap_or_default(); let print_doc = !doc.is_empty() && doc.lines().count() <= 1; - let print_aliases = !config.no_inline_aliases && !aliases.is_empty(); + let print_aliases = config.alias_style != AliasStyle::Recipe && !aliases.is_empty(); if print_doc || print_aliases { print!( @@ -435,7 +435,7 @@ impl Subcommand { .paint(&format!("[aliases: {}]", aliases.join(", "))) )); - let (left, right) = if config.inline_aliases_left { + let (left, right) = if config.alias_style == AliasStyle::InlineLeft { (aliases, doc) } else { (doc, aliases) @@ -574,7 +574,7 @@ impl Subcommand { if let Some(recipes) = recipe_groups.get(&group) { for recipe in recipes { - let recipe_alias_entries = if config.no_inline_aliases { + let recipe_alias_entries = if config.alias_style == AliasStyle::Recipe { aliases.get(recipe.name()) } else { None diff --git a/tests/alias_style.rs b/tests/alias_style.rs new file mode 100644 index 00000000..1b2f94b2 --- /dev/null +++ b/tests/alias_style.rs @@ -0,0 +1,60 @@ +use super::*; + +#[test] +fn alias_style_inline() { + Test::new() + .justfile( + " + alias t := test1 + + # A test recipe + test1: + @echo 'test1' + + test2: + @echo 'test2' + ", + ) + .args(["--alias-style=inline", "--list"]) + .stdout("Available recipes:\n test1 # A test recipe [aliases: t]\n test2\n") + .run(); +} + +#[test] +fn alias_style_inline_left() { + Test::new() + .justfile( + " + alias t := test1 + + # A test recipe + test1: + @echo 'test1' + + test2: + @echo 'test2' + ", + ) + .args(["--alias-style=inline-left", "--list"]) + .stdout("Available recipes:\n test1 # [aliases: t] A test recipe\n test2\n") + .run(); +} + +#[test] +fn alias_style_recipe() { + Test::new() + .justfile( + " + alias t := test1 + + test1: + @echo 'test1' + + test2: + @echo 'test2' + ", + ) + .args(["--alias-style=recipe", "--list"]) + .stdout("Available recipes:\n test1\n t # alias for `test1`\n test2\n") + .run(); +} diff --git a/tests/lib.rs b/tests/lib.rs index 0bb48212..dfa11fc3 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -31,6 +31,7 @@ pub(crate) use { #[macro_use] mod test; +mod alias_style; mod allow_duplicate_recipes; mod allow_duplicate_variables; mod assert_stdout; @@ -82,7 +83,6 @@ mod no_aliases; mod no_cd; mod no_dependencies; mod no_exit_message; -mod no_inline_aliases; mod os_attributes; mod parameters; mod parser; diff --git a/tests/list.rs b/tests/list.rs index 5190c3d3..db7b669e 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -438,23 +438,3 @@ fn no_space_before_submodules_not_following_groups() { ) .run(); } - -#[test] -fn inline_aliases_left_flag() { - Test::new() - .justfile( - " - alias t := test1 - - # I'm a recipe - test1: - @echo 'test1' - - test2: - @echo 'test2' - ", - ) - .args(["--inline-aliases-left", "--list"]) - .stdout("Available recipes:\n test1 # [aliases: t] I'm a recipe\n test2\n") - .run(); -} diff --git a/tests/no_inline_aliases.rs b/tests/no_inline_aliases.rs deleted file mode 100644 index 41949337..00000000 --- a/tests/no_inline_aliases.rs +++ /dev/null @@ -1,20 +0,0 @@ -use super::*; - -#[test] -fn no_inline_aliases_flag() { - Test::new() - .justfile( - " - alias t := test1 - - test1: - @echo 'test1' - - test2: - @echo 'test2' - ", - ) - .args(["--no-inline-aliases", "--list"]) - .stdout("Available recipes:\n test1\n t # alias for `test1`\n test2\n") - .run(); -}