Skip to content

Commit

Permalink
Add the --choose subcommand (#680)
Browse files Browse the repository at this point in the history
The `--choose` subcommand runs a chooser to select a recipe to run. The
chooser should read lines containing recipe names from standard input,
and write one of those names to standard output.

The chooser defaults to `fzf`, a popular fuzzy finder, but can be
overridden by setting $JUST_CHOOSER or passing `--chooser <CHOOSER>`.
  • Loading branch information
casey authored Sep 18, 2020
1 parent 55985aa commit 9d02469
Show file tree
Hide file tree
Showing 17 changed files with 550 additions and 203 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ on:
branches:
- master

env:
# Increment to invalidate github actions caches if they become corrupt.
# Errors of the form "can't find crate for `snafu_derive` which `snafu` depends on"
# can usually be fixed by incrementing this value.
CACHE_KEY_PREFIX: 1

jobs:
all:
name: All
Expand Down Expand Up @@ -41,19 +47,19 @@ jobs:
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: 0-${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
key: ${{ env.CACHE_KEY_PREFIX }}-${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}

- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: 0-${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
key: ${{ env.CACHE_KEY_PREFIX }}-${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}

- name: Cache cargo build
uses: actions/cache@v1
with:
path: target
key: 0-${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
key: ${{ env.CACHE_KEY_PREFIX }}-${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}

- name: Install Main Toolchain
uses: actions-rs/toolchain@v1
Expand Down
37 changes: 37 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ $ just --summary --unsorted
test build
```
If you'd like `just` to default to listing the recipes in the justfile, you can
use this as your default recipe:
```make
default:
@just --list
```
=== Aliases
Aliases allow recipes to be invoked with alternative names:
Expand Down Expand Up @@ -958,6 +966,35 @@ echo 'Bar!'
Bar!
```

=== Selecting a Recipe to Run With an Interactive Chooser

The `--choose` subcommand makes just invoke a chooser to select which recipe to
run. Choosers should read lines containing recipe names from standard input and
print one of those names to standard output.

Because there is currenly no way to run a recipe that requires arguments with
`--choose`, such recipes will not be given to the chooser. Private recipes and
aliases are also skipped.

The chooser can be overridden with the `--chooser` flag. If `--chooser` is not
given, then `just` first checks if `$JUST_CHOOSER` is set. If it isn't, then
the chooser defaults to `fzf`, a popular fuzzy finder.

Arguments can be included in the chooser, i.e. `fzf --exact`.

The chooser is invoked in the same way as recipe lines. For example, if the
chooser is `fzf`, it will be invoked with `sh -cu 'fzf'`, and if the shell, or
the shell arguments are overridden, the chooser invocation will respect those
overrides.

If you'd like `just` to default to selecting a recipe with a chooser, you can
use this as your default recipe:

```make
default:
@just --choose
```

=== Invoking Justfiles in Other Directories

If the first argument passed to `just` contains a `/`, then the following occurs:
Expand Down
6 changes: 5 additions & 1 deletion completions/just.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ _just() {

case "${cmd}" in
just)
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>... "
opts=" -q -u -v -e -l -h -V -f -d -s --dry-run --highlight --no-dotenv --no-highlight --quiet --clear-shell-args --unsorted --verbose --choose --dump --edit --evaluate --init --list --summary --variables --help --version --chooser --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
fi
case "${prev}" in

--chooser)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--color)
COMPREPLY=($(compgen -W "auto always never" -- "${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 @@ -14,6 +14,7 @@ edit:completion:arg-completer[just] = [@words]{
}
completions = [
&'just'= {
cand --chooser 'Override binary invoked by `--choose`'
cand --color 'Print colorful output'
cand -f 'Use <JUSTFILE> as justfile.'
cand --justfile 'Use <JUSTFILE> as justfile.'
Expand All @@ -36,6 +37,7 @@ edit:completion:arg-completer[just] = [@words]{
cand --unsorted 'Return list and summary entries in source order'
cand -v 'Use verbose output'
cand --verbose 'Use verbose output'
cand --choose 'Select a recipe to run using a binary. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`'
cand --dump 'Print entire justfile'
cand -e 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`'
cand --edit 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`'
Expand Down
2 changes: 2 additions & 0 deletions completions/just.fish
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ complete -c just -n "__fish_is_first_arg" --no-files
complete -c just -a '(__fish_just_complete_recipes)'

# autogenerated completions
complete -c just -n "__fish_use_subcommand" -l chooser -d 'Override binary invoked by `--choose`'
complete -c just -n "__fish_use_subcommand" -l color -d 'Print colorful output' -r -f -a "auto always never"
complete -c just -n "__fish_use_subcommand" -s f -l justfile -d 'Use <JUSTFILE> as justfile.'
complete -c just -n "__fish_use_subcommand" -l set -d 'Override <VARIABLE> with <VALUE>'
Expand All @@ -25,6 +26,7 @@ complete -c just -n "__fish_use_subcommand" -s q -l quiet -d 'Suppress all outpu
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 choose -d 'Select a recipe to run using a binary. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`'
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`'
complete -c just -n "__fish_use_subcommand" -l evaluate -d 'Print evaluated variables'
Expand Down
2 changes: 2 additions & 0 deletions completions/just.powershell
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Register-ArgumentCompleter -Native -CommandName 'just' -ScriptBlock {

$completions = @(switch ($command) {
'just' {
[CompletionResult]::new('--chooser', 'chooser', [CompletionResultType]::ParameterName, 'Override binary invoked by `--choose`')
[CompletionResult]::new('--color', 'color', [CompletionResultType]::ParameterName, 'Print colorful output')
[CompletionResult]::new('-f', 'f', [CompletionResultType]::ParameterName, 'Use <JUSTFILE> as justfile.')
[CompletionResult]::new('--justfile', 'justfile', [CompletionResultType]::ParameterName, 'Use <JUSTFILE> as justfile.')
Expand All @@ -41,6 +42,7 @@ Register-ArgumentCompleter -Native -CommandName 'just' -ScriptBlock {
[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('--choose', 'choose', [CompletionResultType]::ParameterName, 'Select a recipe to run using a binary. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`')
[CompletionResult]::new('--dump', 'dump', [CompletionResultType]::ParameterName, 'Print entire justfile')
[CompletionResult]::new('-e', 'e', [CompletionResultType]::ParameterName, 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`')
[CompletionResult]::new('--edit', 'edit', [CompletionResultType]::ParameterName, 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`')
Expand Down
2 changes: 2 additions & 0 deletions completions/just.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ _just() {

local context curcontext="$curcontext" state line
local common=(
'--chooser=[Override binary invoked by `--choose`]' \
'--color=[Print colorful output]: :(auto always never)' \
'-f+[Use <JUSTFILE> as justfile.]' \
'--justfile=[Use <JUSTFILE> as justfile.]' \
Expand All @@ -37,6 +38,7 @@ _just() {
'--unsorted[Return list and summary entries in source order]' \
'*-v[Use verbose output]' \
'*--verbose[Use verbose output]' \
'--choose[Select a recipe to run using a binary. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`]' \
'--dump[Print entire justfile]' \
'-e[Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`]' \
'--edit[Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`]' \
Expand Down
3 changes: 2 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ pub(crate) use std::{
cmp,
collections::{BTreeMap, BTreeSet},
env,
ffi::OsString,
fmt::{self, Debug, Display, Formatter},
fs,
io::{self, Cursor, Write},
iter::{self, FromIterator},
ops::{Index, Range, RangeInclusive},
path::{Path, PathBuf},
process::{self, Command},
process::{self, Command, Stdio},
rc::Rc,
str::{self, Chars},
sync::{Mutex, MutexGuard},
Expand Down
Loading

0 comments on commit 9d02469

Please sign in to comment.