Improve micromamba bash completion (#1234)

This commit is contained in:
Adrien Delsalle 2021-10-25 14:59:45 +02:00 committed by GitHub
parent 357c3410ee
commit 8cdc82982b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 1 deletions

View File

@ -144,3 +144,29 @@ Windows
# and use micromamba directly
micromamba create -f ./test/env_win.yaml -y
micromamba activate yourenv
.. _shell_completion:
Shell completion
================
For now, only ``micromamba`` provides shell completion on ``bash``.
To activate it, it's as simple as running:
.. code::
micromamba shell completion
.. note::
This command will only work on ``bash``
The completion is now available in any new shell opened or in the current shell after running ``source ~/.bashrc`` to take modifications into account.
| Just hit ``<TAB><TAB>`` to get completion when typing your command.
| For example the following command will help you to pick a named environment to activate:
.. code::
micromamba activate <TAB><TAB>

View File

@ -5,6 +5,6 @@ _umamba_completions()
COMPREPLY=($(micromamba completer "${COMP_WORDS[@]:1}"))
}
complete -F _umamba_completions micromamba
complete -o default -F _umamba_completions micromamba
# <<< umamba completion <<<
)MAMBARAW"

View File

@ -83,6 +83,20 @@ overwrite_callbacks(std::vector<CLI::App*>& apps,
}
}
void
add_activate_completion(CLI::App* app, std::vector<std::string>& completer_args, bool& completed)
{
CLI::App* activate_subcom
= app->add_subcommand("activate", "Mock activate shell function for completion");
activate_subcom->callback([app, &completer_args, &completed]() {
if (completer_args.size() == 1)
{
completer_args = { "-n", completer_args.back() };
complete_options(app, completer_args, completed);
}
});
}
void
get_completions(CLI::App* app, int argc, char** argv)
{
@ -100,6 +114,7 @@ get_completions(CLI::App* app, int argc, char** argv)
std::vector<CLI::App*> apps = { app };
overwrite_callbacks(apps, completer_args, completed);
add_activate_completion(app, completer_args, completed);
argv[1] = argv[0];
try