Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a --list-paths to output list of recipe paths only #2241

Closed
oivvio opened this issue Jul 11, 2024 · 4 comments
Closed

Add a --list-paths to output list of recipe paths only #2241

oivvio opened this issue Jul 11, 2024 · 4 comments

Comments

@oivvio
Copy link

oivvio commented Jul 11, 2024

I wanted an option to run just --choose with executing the choosen recipe but instead output it to the prompt so that it can be edited before excution. This is great for recipes that take arguments and also gives a more valuable shell history. Failing to do that in rust that I don't know a lot about, it hacked together something with python + zsh

First here's a python script to extract all recipe paths (recursing into submodules)

#!/usr/bin/env python3
import json
import subprocess
from json.decoder import JSONDecodeError


def get_just_json():
    try:
        return json.loads(
            subprocess.run(
                ["just", "--dump", "--dump-format", "json"],
                capture_output=True,
                text=True,
            ).stdout
        )
    except JSONDecodeError:
        pass


def walk_data(data):

    results = []

    def _walk_data(data, module_path):
        try:
            recipes = data["recipes"]
            for recipe in [r for r in recipes if not r.startswith("_")]:
                doc = recipes[recipe]["doc"]
                namepath = recipes[recipe]["namepath"].replace("::", " ")
                results.append((namepath, doc))

        except AttributeError as err:
            pass

        try:
            for module in data["modules"].keys():
                new_data = data["modules"][module]
                new_module_path = [*module_path, module]
                _walk_data(new_data, new_module_path)
        except AttributeError as err:
            pass

    _walk_data(data, [])

    return results


if data := get_just_json():
    lines = walk_data(data)
    n_chars = max([len(l[0]) for l in lines])
    for path, doc in walk_data(data):
        print(f"{path:<{n_chars}} -- {doc}")

Then I put together this zsh function to invoke it, pass the output to fzf and output the chosen recipe to the prompt for editing.

juc () {
    OUTPUT=$(python3 ~/ltbin/justchoose.py|fzf --delimiter='--' --preview 'just --unstable --show {+1}' --preview-window="down")
    SANS_DOCS=$(echo $OUTPUT|awk -F'--' '{print $1}')
    STRIPPED=$(echo $SANS_DOCS | sed 's/[[:space:]]*$//')
    tmux rename-window "[$STRIPPED]"
    print -z "just $STRIPPED"
}

Halfway through the process I realized that maybe just wouldn't be able to ouput the the chosen recipe to the prompt for editing ( i.e. to do what "print -z" does in zsh) so just won't be able to do the entire thing that these to scripts do together. But it would be nice if just could list available recipe paths (i.e. the thing the python script does).

@casey
Copy link
Owner

casey commented Jul 11, 2024

Can you use just --summary? This will print out a space-separated list of full recipe paths.

@oivvio
Copy link
Author

oivvio commented Jul 11, 2024

Thanks! Another case of me saving myself from reading docs for five minutes by writing my own thing for an hour :)

@oivvio
Copy link
Author

oivvio commented Jul 11, 2024

Here's a version of the zsh function that does the whole thing without the need for any python script. The python version had the nice bonus that it would output the doc string next to the recipe path, but this is a lot simpler.

juc () {
    OUTPUT=$(just --summary|tr ' ' '\n'|fzf -i  --preview 'just --unstable --show {}' --preview-window="down")
    tmux rename-window "[$OUTPUT]"
    print -z "just $OUTPUT"
}

@casey
Copy link
Owner

casey commented Jul 12, 2024

Nice, glad summary works!

@casey casey closed this as completed Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants