From b692e81562ea67718ebb4187a6966c0f0c04b1d8 Mon Sep 17 00:00:00 2001 From: Timo Reents Date: Thu, 23 Nov 2023 16:13:53 +0100 Subject: [PATCH 1/2] Add list command and tab completion for --- aiida_project/commands/main.py | 46 +++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/aiida_project/commands/main.py b/aiida_project/commands/main.py index fdcd450..12b595f 100644 --- a/aiida_project/commands/main.py +++ b/aiida_project/commands/main.py @@ -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 = """ @@ -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}") From 1cd7b020c230840c925b721f81ab01896e8d18fc Mon Sep 17 00:00:00 2001 From: Timo Reents Date: Thu, 23 Nov 2023 16:21:29 +0100 Subject: [PATCH 2/2] Update README. --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 78a27ba..2a496a4 100644 --- a/README.md +++ b/README.md @@ -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` @@ -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: