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

Adding cda tab completion and list command #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ or simply open a new terminal.

This will also add the `cda` function to your shell startup file, so you can easily switch projects.
Note that you'll have to source your e.g. `.zshrc` file for this function to be accessible!
In order to activate tab-completion, you'll have to run `aiida-project --install-completion` and restart the terminal afterwards.

### `create`

Expand All @@ -66,6 +67,16 @@ You can then activate the project using the `cda` command described above:
```console
cda firstproject
```
The `cda` command supports tab-completion and lists the available projects. Moreover, you can use the `list` command to show all available projects grouped by the corresponding environment (either conda or venv).
```console
(firstproject) ~$ aiida-project list
The following projects are available:

venv
firstproject
hp_tests

```

Next to activating the Python virtual environment, this will also change the directory to the one for the project.
`aiida-project` automatically sets up a directory structure, which we intend to be made configurable globally:
Expand Down
46 changes: 45 additions & 1 deletion aiida_project/commands/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,32 @@
from ..config import ShellType
from ..project import EngineType, load_project_class

CDA_FUNCTION = """
CDA_FUNCTION = r"""
cda () {
source $aiida_venv_dir/$1/bin/activate
cd $aiida_project_dir/$1
}

_cda_complete() {
local cur prev projects
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

# Complete only the first argument
if [[ $COMP_CWORD -eq 1 ]]; then
# Get the list of available project filenames with ".json" extension
projects=(
$(find "$aiida_project_dir/.aiida_projects" -type f -name "*.json" -exec basename {} \;)
)

projects=("${projects[@]%.json}")

COMPREPLY=($(compgen -W "${projects[*]}" -- "$cur"))
fi
}

complete -F _cda_complete cda
"""

ACTIVATE_AIIDA_SH = """
Expand Down Expand Up @@ -217,3 +238,26 @@ def destroy(
project.destroy()
project_dict.remove_project(name)
print(f"[bold green]Succes:[/bold green] Project with name {name} has been destroyed.")


@app.command()
def list():
"""List all existing projects."""
from ..config import ProjectConfig, ProjectDict

if ProjectConfig().is_not_initialised():
return

projects = ProjectDict().projects
project_names = sorted(projects.keys())

project_engine_name = {}

for project_name in project_names:
project_engine_name.setdefault(projects[project_name]._engine, []).append(project_name)

print("The following projects are available:")
for engine, project_names in project_engine_name.items():
print(f"\n [bold blue]{engine}[/]")
for project_name in project_names:
print(f" {project_name}")