Skip to content

Commit

Permalink
Add option to print recipes in source order (#669)
Browse files Browse the repository at this point in the history
Add an `--unsorted` flag, which makes `--list` and `--summary` print
their entries in source order.
  • Loading branch information
casey authored Aug 21, 2020
1 parent 75898e2 commit 9ecfd2b
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 15 deletions.
2 changes: 1 addition & 1 deletion completions/just.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ _just() {

case "${cmd}" in
just)
opts=" -q -v -e -l -h -V -f -d -s --dry-run --highlight --no-dotenv --no-highlight --quiet --clear-shell-args --verbose --dump --edit --evaluate --init --list --summary --variables --help --version --color --justfile --set --shell --shell-arg --working-directory --completions --show <ARGUMENTS>... "
opts=" -q -u -v -e -l -h -V -f -d -s --dry-run --highlight --no-dotenv --no-highlight --quiet --clear-shell-args --unsorted --verbose --dump --edit --evaluate --init --list --summary --variables --help --version --color --justfile --set --shell --shell-arg --working-directory --completions --show <ARGUMENTS>... "
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down
2 changes: 2 additions & 0 deletions completions/just.elvish
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ edit:completion:arg-completer[just] = [@words]{
cand -q 'Suppress all output'
cand --quiet 'Suppress all output'
cand --clear-shell-args 'Clear shell arguments'
cand -u 'Return list and summary entries in source order'
cand --unsorted 'Return list and summary entries in source order'
cand -v 'Use verbose output'
cand --verbose 'Use verbose output'
cand --dump 'Print entire justfile'
Expand Down
1 change: 1 addition & 0 deletions completions/just.fish
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ complete -c just -n "__fish_use_subcommand" -l no-dotenv -d 'Don\'t load `.env`
complete -c just -n "__fish_use_subcommand" -l no-highlight -d 'Don\'t highlight echoed recipe lines in bold'
complete -c just -n "__fish_use_subcommand" -s q -l quiet -d 'Suppress all output'
complete -c just -n "__fish_use_subcommand" -l clear-shell-args -d 'Clear shell arguments'
complete -c just -n "__fish_use_subcommand" -s u -l unsorted -d 'Return list and summary entries in source order'
complete -c just -n "__fish_use_subcommand" -s v -l verbose -d 'Use verbose output'
complete -c just -n "__fish_use_subcommand" -l dump -d 'Print entire justfile'
complete -c just -n "__fish_use_subcommand" -s e -l edit -d 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`'
Expand Down
2 changes: 2 additions & 0 deletions completions/just.powershell
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Register-ArgumentCompleter -Native -CommandName 'just' -ScriptBlock {
[CompletionResult]::new('-q', 'q', [CompletionResultType]::ParameterName, 'Suppress all output')
[CompletionResult]::new('--quiet', 'quiet', [CompletionResultType]::ParameterName, 'Suppress all output')
[CompletionResult]::new('--clear-shell-args', 'clear-shell-args', [CompletionResultType]::ParameterName, 'Clear shell arguments')
[CompletionResult]::new('-u', 'u', [CompletionResultType]::ParameterName, 'Return list and summary entries in source order')
[CompletionResult]::new('--unsorted', 'unsorted', [CompletionResultType]::ParameterName, 'Return list and summary entries in source order')
[CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'Use verbose output')
[CompletionResult]::new('--verbose', 'verbose', [CompletionResultType]::ParameterName, 'Use verbose output')
[CompletionResult]::new('--dump', 'dump', [CompletionResultType]::ParameterName, 'Print entire justfile')
Expand Down
2 changes: 2 additions & 0 deletions completions/just.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ _just() {
'(--dry-run)-q[Suppress all output]' \
'(--dry-run)--quiet[Suppress all output]' \
'--clear-shell-args[Clear shell arguments]' \
'-u[Return list and summary entries in source order]' \
'--unsorted[Return list and summary entries in source order]' \
'*-v[Use verbose output]' \
'*--verbose[Use verbose output]' \
'--dump[Print entire justfile]' \
Expand Down
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub(crate) use edit_distance::edit_distance;
pub(crate) use libc::EXIT_FAILURE;
pub(crate) use log::{info, warn};
pub(crate) use snafu::{ResultExt, Snafu};
pub(crate) use unicode_width::UnicodeWidthChar;
pub(crate) use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

// modules
pub(crate) use crate::{config_error, keyword, setting};
Expand Down
57 changes: 44 additions & 13 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::common::*;

use clap::{App, AppSettings, Arg, ArgGroup, ArgMatches, ArgSettings};
use unicode_width::UnicodeWidthStr;

pub(crate) const DEFAULT_SHELL: &str = "sh";
pub(crate) const DEFAULT_SHELL_ARG: &str = "-cu";
Expand All @@ -20,6 +19,7 @@ pub(crate) struct Config {
pub(crate) shell_args: Vec<String>,
pub(crate) shell_present: bool,
pub(crate) subcommand: Subcommand,
pub(crate) unsorted: bool,
pub(crate) verbosity: Verbosity,
}

Expand Down Expand Up @@ -71,6 +71,7 @@ mod arg {
pub(crate) const SET: &str = "SET";
pub(crate) const SHELL: &str = "SHELL";
pub(crate) const SHELL_ARG: &str = "SHELL-ARG";
pub(crate) const UNSORTED: &str = "UNSORTED";
pub(crate) const VERBOSE: &str = "VERBOSE";
pub(crate) const WORKING_DIRECTORY: &str = "WORKING-DIRECTORY";

Expand Down Expand Up @@ -165,6 +166,12 @@ impl Config {
.overrides_with(arg::SHELL_ARG)
.help("Clear shell arguments"),
)
.arg(
Arg::with_name(arg::UNSORTED)
.long("unsorted")
.short("u")
.help("Return list and summary entries in source order"),
)
.arg(
Arg::with_name(arg::VERBOSE)
.short("v")
Expand Down Expand Up @@ -407,6 +414,7 @@ impl Config {
quiet: matches.is_present(arg::QUIET),
shell: matches.value_of(arg::SHELL).unwrap().to_owned(),
load_dotenv: !matches.is_present(arg::NO_DOTENV),
unsorted: matches.is_present(arg::UNSORTED),
color,
invocation_directory,
search_config,
Expand Down Expand Up @@ -461,7 +469,7 @@ impl Config {
overrides,
} => self.run(justfile, &search, overrides, arguments),
Show { ref name } => Self::show(&name, justfile),
Summary => Self::summary(justfile),
Summary => self.summary(justfile),
Variables => Self::variables(justfile),
Completions { .. } | Edit | Init => unreachable!(),
}
Expand Down Expand Up @@ -562,14 +570,14 @@ impl Config {
let doc_color = self.color.stdout().doc();
println!("Available recipes:");

for (name, recipe) in &justfile.recipes {
for recipe in justfile.recipes(self.unsorted) {
let name = recipe.name();

if recipe.private {
continue;
}

let alias_doc = format!("alias for `{}`", recipe.name);

for (i, name) in iter::once(name)
for (i, name) in iter::once(&name)
.chain(recipe_aliases.get(name).unwrap_or(&Vec::new()))
.enumerate()
{
Expand Down Expand Up @@ -599,7 +607,10 @@ impl Config {
match (i, recipe.doc) {
(0, Some(doc)) => print_doc(doc),
(0, None) => (),
_ => print_doc(&alias_doc),
_ => {
let alias_doc = format!("alias for `{}`", recipe.name);
print_doc(&alias_doc);
},
}
println!();
}
Expand Down Expand Up @@ -646,17 +657,16 @@ impl Config {
}
}

fn summary(justfile: Justfile) -> Result<(), i32> {
fn summary(&self, justfile: Justfile) -> Result<(), i32> {
if justfile.count() == 0 {
eprintln!("Justfile contains no recipes.");
} else {
let summary = justfile
.recipes
.recipes(self.unsorted)
.iter()
.filter(|&(_, recipe)| !recipe.private)
.map(|(name, _)| name)
.cloned()
.collect::<Vec<_>>()
.filter(|recipe| recipe.public())
.map(|recipe| recipe.name())
.collect::<Vec<&str>>()
.join(" ");
println!("{}", summary);
}
Expand Down Expand Up @@ -707,6 +717,7 @@ FLAGS:
--no-highlight Don't highlight echoed recipe lines in bold
-q, --quiet Suppress all output
--summary List names of available recipes
-u, --unsorted Return list and summary entries in source order
--variables List names of variables
-v, --verbose Use verbose output
Expand Down Expand Up @@ -753,6 +764,7 @@ ARGS:
$(shell_args: $shell_args:expr,)?
$(shell_present: $shell_present:expr,)?
$(subcommand: $subcommand:expr,)?
$(unsorted: $unsorted:expr,)?
$(verbosity: $verbosity:expr,)?
} => {
#[test]
Expand All @@ -772,6 +784,7 @@ ARGS:
$(shell_args: $shell_args,)?
$(shell_present: $shell_present,)?
$(subcommand: $subcommand,)?
$(unsorted: $unsorted,)?
$(verbosity: $verbosity,)?
..testing::config(&[])
};
Expand Down Expand Up @@ -936,6 +949,24 @@ ARGS:
highlight: false,
}

test! {
name: unsorted_default,
args: [],
unsorted: false,
}

test! {
name: unsorted_long,
args: ["--unsorted"],
unsorted: true,
}

test! {
name: unsorted_short,
args: ["-u"],
unsorted: true,
}

test! {
name: quiet_default,
args: [],
Expand Down
14 changes: 14 additions & 0 deletions src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,20 @@ impl<'src> Justfile<'src> {
ran.insert(invocation);
Ok(())
}

pub(crate) fn recipes(&self, source_order: bool) -> Vec<&Recipe<Dependency>> {
let mut recipes = self
.recipes
.values()
.map(AsRef::as_ref)
.collect::<Vec<&Recipe<Dependency>>>();

if source_order {
recipes.sort_by_key(|recipe| recipe.name.offset);
}

recipes
}
}

impl<'src> Display for Justfile<'src> {
Expand Down
4 changes: 4 additions & 0 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ impl<'src, D> Recipe<'src, D> {
self.name.line
}

pub(crate) fn public(&self) -> bool {
!self.private
}

pub(crate) fn run<'run>(
&self,
context: &RecipeContext<'src, 'run>,
Expand Down
54 changes: 54 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,28 @@ _y:
stdout: "a b c d\n",
}

test! {
name: summary_sorted,
justfile: "
b:
c:
a:
",
args: ("--summary"),
stdout: "a b c\n",
}

test! {
name: summary_unsorted,
justfile: "
b:
c:
a:
",
args: ("--summary", "--unsorted"),
stdout: "b c a\n",
}

test! {
name: select,
justfile: "b:
Expand Down Expand Up @@ -1235,6 +1257,38 @@ _private-recipe:
"#,
}

test! {
name: list_sorted,
justfile: r#"
alias c := b
b:
a:
"#,
args: ("--list"),
stdout: r#"
Available recipes:
a
b
c # alias for `b`
"#,
}

test! {
name: list_unsorted,
justfile: r#"
alias c := b
b:
a:
"#,
args: ("--list", "--unsorted"),
stdout: r#"
Available recipes:
b
c # alias for `b`
a
"#,
}

test! {
name: show_suggestion,
justfile: r#"
Expand Down

0 comments on commit 9ecfd2b

Please sign in to comment.