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

Improve micromamba bash completion #1234

Merged
merged 2 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>
2 changes: 1 addition & 1 deletion src/micromamba/completer.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 15 additions & 0 deletions src/micromamba/completer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
Expand Down