Skip to content

Commit

Permalink
List by groups
Browse files Browse the repository at this point in the history
  • Loading branch information
neunenak committed Jan 14, 2024
1 parent c440b75 commit 77657a1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 24 deletions.
6 changes: 5 additions & 1 deletion src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ impl<'src> Display for Attribute<'src> {
let attr_name = self.name();
match self {
Self::Confirm(Some(prompt)) => write!(f, "{attr_name}('{prompt}')"),
Self::Group { name } => write!(f, "{attr_name}({name})"),
Self::Group { name } => {
let use_quotes = name.contains(char::is_whitespace);
let mq = if use_quotes { "\"" } else { "" };
write!(f, "{attr_name}({mq}{name}{mq})")
}
_other => write!(f, "{attr_name}"),
}
}
Expand Down
62 changes: 39 additions & 23 deletions src/list_recipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,31 @@ fn print_doc_comment(doc: &str, padding: usize, doc_color: Color) {
);
}

fn recipes_by_group<'a>(
justfile: &'a Justfile,
sort_order: bool,
) -> BTreeMap<Option<&'a str>, Vec<&'a Recipe<'a>>> {
let mut by_groups: BTreeMap<Option<&str>, Vec<&Recipe<'_>>> = BTreeMap::new();

for recipe in justfile.public_recipes(sort_order) {
let groups = recipe.groups();
if groups.is_empty() {
by_groups
.entry(None)
.and_modify(|e| e.push(recipe))
.or_insert(vec![recipe]);
} else {
for group in groups {
by_groups
.entry(Some(group))
.and_modify(|e| e.push(recipe))
.or_insert(vec![recipe]);
}
}
}
by_groups
}

pub(crate) fn list(config: &Config, level: usize, justfile: &Justfile) {
let recipe_aliases = get_recipe_aliases(justfile);
let line_widths = get_line_widths(config, justfile, &recipe_aliases);
Expand All @@ -103,32 +128,23 @@ pub(crate) fn list(config: &Config, level: usize, justfile: &Justfile) {
print!("{}", config.list_heading);
}

let public_recipes = justfile.public_recipes(config.unsorted);
let mut by_groups: HashMap<Option<&str>, Vec<&str>> = HashMap::new();
let by_groups = recipes_by_group(justfile, config.unsorted);
let no_recipes_in_group = by_groups.contains_key(&None) && by_groups.len() == 1;

for recipe in &public_recipes {
let recipe_name = recipe.name();
let groups = recipe.groups();
if groups.is_empty() {
by_groups
.entry(None)
.and_modify(|e| e.push(recipe_name))
.or_insert(vec![recipe_name]);
} else {
for group in groups {
by_groups
.entry(Some(group))
.and_modify(|e| e.push(recipe_name))
.or_insert(vec![recipe_name]);
for (group, recipes) in &by_groups {
if !no_recipes_in_group {
if let Some(group_name) = group {
println!("[group: {group_name}]");
} else {
println!("(no group)")
}
}
}

for recipe in public_recipes {
let aliases: &[&str] = recipe_aliases
.get(recipe.name())
.map_or(&[], |v| v.as_slice());
print_recipe(recipe, aliases, level, config, &line_widths, max_line_width);
for recipe in recipes {
let aliases: &[&str] = recipe_aliases
.get(recipe.name())
.map_or(&[], |v| v.as_slice());
print_recipe(recipe, aliases, level, config, &line_widths, max_line_width);
}
}

for (name, module) in &justfile.modules {
Expand Down
40 changes: 40 additions & 0 deletions tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,46 @@ b:
"#,
}

test! {
name: list_with_groups,
justfile: r#"
[group: alpha]
a:
# Doc comment
[group(alpha)]
[group(beta)]
b:
c:
[group:"multi word group"]
d:
[group("alpha")]
e:
[group: "beta"]
[group(alpha)]
f:
"#,
args: ("--list", "--list-heading", ""),
stdout: r#"
(no group)
c
[group: alpha]
a
b # Doc comment
e
f
[group: beta]
b # Doc comment
f
[group: multi word group]
d
"#,
}

test! {
name: run_suggestion,
justfile: r#"
Expand Down
2 changes: 2 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ fn test_round_trip(tmpdir: &Path) {
.output()
.expect("just invocation failed");

println!("{:?}", output);

if !output.status.success() {
panic!("reparse failed: {}", output.status);
}
Expand Down

0 comments on commit 77657a1

Please sign in to comment.