From 5b722f067fd922881dc69047ce2a4edcbb875243 Mon Sep 17 00:00:00 2001 From: mashehu Date: Tue, 29 Nov 2022 09:45:12 +0100 Subject: [PATCH 1/6] add shell completion option --- .nf-core-complete.bash | 29 +++++++++++++++++++++++++++ .nf-core-complete.zsh | 35 ++++++++++++++++++++++++++++++++ README.md | 4 ++++ nf_core/__main__.py | 45 +++++++++++++++++++++--------------------- 4 files changed, 91 insertions(+), 22 deletions(-) create mode 100644 .nf-core-complete.bash create mode 100644 .nf-core-complete.zsh diff --git a/.nf-core-complete.bash b/.nf-core-complete.bash new file mode 100644 index 0000000000..2d6f3de0d7 --- /dev/null +++ b/.nf-core-complete.bash @@ -0,0 +1,29 @@ +_nf_core_completion() { + local IFS=$'\n' + local response + + response=$(env COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD=$COMP_CWORD _NF_CORE_COMPLETE=bash_complete $1) + + for completion in $response; do + IFS=',' read type value <<< "$completion" + + if [[ $type == 'dir' ]]; then + COMPREPLY=() + compopt -o dirnames + elif [[ $type == 'file' ]]; then + COMPREPLY=() + compopt -o default + elif [[ $type == 'plain' ]]; then + COMPREPLY+=($value) + fi + done + + return 0 +} + +_nf_core_completion_setup() { + complete -o nosort -F _nf_core_completion nf-core +} + +_nf_core_completion_setup; + diff --git a/.nf-core-complete.zsh b/.nf-core-complete.zsh new file mode 100644 index 0000000000..ccd8b774b0 --- /dev/null +++ b/.nf-core-complete.zsh @@ -0,0 +1,35 @@ +#compdef nf-core + +_nf_core_completion() { + local -a completions + local -a completions_with_descriptions + local -a response + (( ! $+commands[nf-core] )) && return 1 + + response=("${(@f)$(env COMP_WORDS="${words[*]}" COMP_CWORD=$((CURRENT-1)) _NF_CORE_COMPLETE=zsh_complete nf-core --no-header)}") + + for type key descr in ${response}; do + if [[ "$type" == "plain" ]]; then + if [[ "$descr" == "_" ]]; then + completions+=("$key") + else + completions_with_descriptions+=("$key":"$descr") + fi + elif [[ "$type" == "dir" ]]; then + _path_files -/ + elif [[ "$type" == "file" ]]; then + _path_files -f + fi + done + + if [ -n "$completions_with_descriptions" ]; then + _describe -V unsorted completions_with_descriptions -U + fi + + if [ -n "$completions" ]; then + compadd -U -V unsorted -a completions + fi +} + +compdef _nf_core_completion nf-core; + diff --git a/README.md b/README.md index 0f21412215..b8d21d9e2c 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,10 @@ pip install --upgrade nf-core Please refer to the respective documentation for further details to manage packages, as for example [conda](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-pkgs.html#updating-packages) or [pip](https://packaging.python.org/en/latest/tutorials/installing-packages/#upgrading-packages). +### Activate shell completions for nf-core/tools + +Auto-completion for the `nf-core` command is available for bash and zsh. To activate it, you have to download the completion scripts (`.nf-core-complete.bash` or `.nf-core-complete.zsh`) and source it in your shell configuration file (`.bashrc` or `.zshrc`). + ## Listing pipelines The command `nf-core list` shows all available nf-core pipelines along with their latest version, when that was published and how recently the pipeline code was pulled to your local system (if at all). diff --git a/nf_core/__main__.py b/nf_core/__main__.py index 47c6e99362..25e318948f 100644 --- a/nf_core/__main__.py +++ b/nf_core/__main__.py @@ -79,27 +79,6 @@ def run_nf_core(): - # Print nf-core header - stderr.print(f"\n[green]{' ' * 42},--.[grey39]/[green],-.", highlight=False) - stderr.print("[blue] ___ __ __ __ ___ [green]/,-._.--~\\", highlight=False) - stderr.print(r"[blue] |\ | |__ __ / ` / \ |__) |__ [yellow] } {", highlight=False) - stderr.print(r"[blue] | \| | \__, \__/ | \ |___ [green]\`-._,-`-,", highlight=False) - stderr.print("[green] `._,._,'\n", highlight=False) - stderr.print( - f"[grey39] nf-core/tools version {nf_core.__version__} - [link=https://nf-co.re]https://nf-co.re[/]", - highlight=False, - ) - try: - is_outdated, _, remote_vers = nf_core.utils.check_if_outdated() - if is_outdated: - stderr.print( - f"[bold bright_yellow] There is a new version of nf-core/tools available! ({remote_vers})", - highlight=False, - ) - except Exception as e: - log.debug(f"Could not check latest version: {e}") - stderr.print("\n") - # Launch the click cli nf_core_cli(auto_envvar_prefix="NFCORE") @@ -107,15 +86,37 @@ def run_nf_core(): @click.group(context_settings=dict(help_option_names=["-h", "--help"])) @click.version_option(nf_core.__version__) @click.option("-v", "--verbose", is_flag=True, default=False, help="Print verbose output to the console.") +@click.option("--no-header", is_flag=True, default=False, help="Don't print the header.") @click.option("--hide-progress", is_flag=True, default=False, help="Don't show progress bars.") @click.option("-l", "--log-file", help="Save a verbose log to a file.", metavar="") @click.pass_context -def nf_core_cli(ctx, verbose, hide_progress, log_file): +def nf_core_cli(ctx, verbose, no_header, hide_progress, log_file): """ nf-core/tools provides a set of helper tools for use with nf-core Nextflow pipelines. It is designed for both end-users running pipelines and also developers creating new pipelines. """ + if not no_header: + # Print nf-core header + stderr.print(f"\n[green]{' ' * 42},--.[grey39]/[green],-.", highlight=False) + stderr.print("[blue] ___ __ __ __ ___ [green]/,-._.--~\\", highlight=False) + stderr.print(r"[blue] |\ | |__ __ / ` / \ |__) |__ [yellow] } {", highlight=False) + stderr.print(r"[blue] | \| | \__, \__/ | \ |___ [green]\`-._,-`-,", highlight=False) + stderr.print("[green] `._,._,'\n", highlight=False) + stderr.print( + f"[grey39] nf-core/tools version {nf_core.__version__} - [link=https://nf-co.re]https://nf-co.re[/]", + highlight=False, + ) + try: + is_outdated, _, remote_vers = nf_core.utils.check_if_outdated() + if is_outdated: + stderr.print( + f"[bold bright_yellow] There is a new version of nf-core/tools available! ({remote_vers})", + highlight=False, + ) + except Exception as e: + log.debug(f"Could not check latest version: {e}") + stderr.print("\n") # Set the base logger to output DEBUG log.setLevel(logging.DEBUG) From 58b6e8f7be721e9b5dd4b99fd89a268ceaa88dc9 Mon Sep 17 00:00:00 2001 From: mashehu Date: Tue, 29 Nov 2022 15:55:06 +0100 Subject: [PATCH 2/6] switch to `eval` initalisation --- README.md | 9 ++++++++- nf_core/__main__.py | 36 ++++++++++++++++++------------------ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index b8d21d9e2c..21d3012186 100644 --- a/README.md +++ b/README.md @@ -216,7 +216,14 @@ Please refer to the respective documentation for further details to manage packa ### Activate shell completions for nf-core/tools -Auto-completion for the `nf-core` command is available for bash and zsh. To activate it, you have to download the completion scripts (`.nf-core-complete.bash` or `.nf-core-complete.zsh`) and source it in your shell configuration file (`.bashrc` or `.zshrc`). +Auto-completion for the `nf-core` command is available for bash, zsh and fish. To activate it, add the following line to the respective shell config files. +shell | shell config file | command +--- | --- | --- +bash | ~/.bashrc | `eval "$(_NF_CORE_COMPLETE=bash_source nf-core)"` +Zsh | ~/.zshrc | `eval "$(_NF_CORE_COMPLETE=zsh_source nf-core)"` +fish | ~/.config/fish/completions/nf-core.fish | `eval (env _NF_CORE_COMPLETE=fish_source nf-core)` + +After a restart of the shell session you should have auto-completion for the `nf-core` command and all its sub-commands and options. ## Listing pipelines diff --git a/nf_core/__main__.py b/nf_core/__main__.py index 25e318948f..be44620f35 100644 --- a/nf_core/__main__.py +++ b/nf_core/__main__.py @@ -79,24 +79,8 @@ def run_nf_core(): - # Launch the click cli - nf_core_cli(auto_envvar_prefix="NFCORE") - - -@click.group(context_settings=dict(help_option_names=["-h", "--help"])) -@click.version_option(nf_core.__version__) -@click.option("-v", "--verbose", is_flag=True, default=False, help="Print verbose output to the console.") -@click.option("--no-header", is_flag=True, default=False, help="Don't print the header.") -@click.option("--hide-progress", is_flag=True, default=False, help="Don't show progress bars.") -@click.option("-l", "--log-file", help="Save a verbose log to a file.", metavar="") -@click.pass_context -def nf_core_cli(ctx, verbose, no_header, hide_progress, log_file): - """ - nf-core/tools provides a set of helper tools for use with nf-core Nextflow pipelines. - - It is designed for both end-users running pipelines and also developers creating new pipelines. - """ - if not no_header: + # print nf-core header if environment variable is not set + if os.environ.get("_NF_CORE_COMPLETE") is None: # Print nf-core header stderr.print(f"\n[green]{' ' * 42},--.[grey39]/[green],-.", highlight=False) stderr.print("[blue] ___ __ __ __ ___ [green]/,-._.--~\\", highlight=False) @@ -117,6 +101,22 @@ def nf_core_cli(ctx, verbose, no_header, hide_progress, log_file): except Exception as e: log.debug(f"Could not check latest version: {e}") stderr.print("\n") + # Launch the click cli + nf_core_cli(auto_envvar_prefix="NFCORE") + + +@click.group(context_settings=dict(help_option_names=["-h", "--help"])) +@click.version_option(nf_core.__version__) +@click.option("-v", "--verbose", is_flag=True, default=False, help="Print verbose output to the console.") +@click.option("--hide-progress", is_flag=True, default=False, help="Don't show progress bars.") +@click.option("-l", "--log-file", help="Save a verbose log to a file.", metavar="") +@click.pass_context +def nf_core_cli(ctx, verbose, hide_progress, log_file): + """ + nf-core/tools provides a set of helper tools for use with nf-core Nextflow pipelines. + + It is designed for both end-users running pipelines and also developers creating new pipelines. + """ # Set the base logger to output DEBUG log.setLevel(logging.DEBUG) From db0f969b054f8a54708dfe2fa630a7cc3eced493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20H=C3=B6rtenhuber?= Date: Wed, 30 Nov 2022 12:10:36 +0000 Subject: [PATCH 3/6] add warning about sourcing nf-core correctly --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 21d3012186..21883acb65 100644 --- a/README.md +++ b/README.md @@ -216,7 +216,7 @@ Please refer to the respective documentation for further details to manage packa ### Activate shell completions for nf-core/tools -Auto-completion for the `nf-core` command is available for bash, zsh and fish. To activate it, add the following line to the respective shell config files. +Auto-completion for the `nf-core` command is available for bash, zsh and fish. To activate it, add the following lines to the respective shell config files. shell | shell config file | command --- | --- | --- bash | ~/.bashrc | `eval "$(_NF_CORE_COMPLETE=bash_source nf-core)"` @@ -225,6 +225,8 @@ fish | ~/.config/fish/completions/nf-core.fish | `eval (env _NF_CORE_COMPLETE=fi After a restart of the shell session you should have auto-completion for the `nf-core` command and all its sub-commands and options. +> **NB:** The added line will run the command `nf-core`. You must therefore have the nf-core/tools installed globally. +> You can also wrap it inside `if type nf-core > /dev/null; then ` \ `fi` for bash and zsh or `if command -v nf-core &> /dev/null eval (env _NF_CORE_COMPLETE=fish_source nf-core) end` for fish. You need to then source the config in your environment for the completions to be activated. ## Listing pipelines The command `nf-core list` shows all available nf-core pipelines along with their latest version, when that was published and how recently the pipeline code was pulled to your local system (if at all). From 09c0272e66a7bd7a26804f5f2b723bd6bdf1abac Mon Sep 17 00:00:00 2001 From: nf-core-bot Date: Wed, 30 Nov 2022 12:14:16 +0000 Subject: [PATCH 4/6] [automated] Fix code linting --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 21883acb65..425b7b5a2a 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,7 @@ After a restart of the shell session you should have auto-completion for the `nf > **NB:** The added line will run the command `nf-core`. You must therefore have the nf-core/tools installed globally. > You can also wrap it inside `if type nf-core > /dev/null; then ` \ `fi` for bash and zsh or `if command -v nf-core &> /dev/null eval (env _NF_CORE_COMPLETE=fish_source nf-core) end` for fish. You need to then source the config in your environment for the completions to be activated. + ## Listing pipelines The command `nf-core list` shows all available nf-core pipelines along with their latest version, when that was published and how recently the pipeline code was pulled to your local system (if at all). From 5957d4ecc8c4dd0c46374bb6257f98656dfa75a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20H=C3=B6rtenhuber?= Date: Thu, 1 Dec 2022 09:29:41 +0000 Subject: [PATCH 5/6] remove files which are not needed anymore --- .nf-core-complete.bash | 29 ----------------------------- .nf-core-complete.zsh | 35 ----------------------------------- 2 files changed, 64 deletions(-) delete mode 100644 .nf-core-complete.bash delete mode 100644 .nf-core-complete.zsh diff --git a/.nf-core-complete.bash b/.nf-core-complete.bash deleted file mode 100644 index 2d6f3de0d7..0000000000 --- a/.nf-core-complete.bash +++ /dev/null @@ -1,29 +0,0 @@ -_nf_core_completion() { - local IFS=$'\n' - local response - - response=$(env COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD=$COMP_CWORD _NF_CORE_COMPLETE=bash_complete $1) - - for completion in $response; do - IFS=',' read type value <<< "$completion" - - if [[ $type == 'dir' ]]; then - COMPREPLY=() - compopt -o dirnames - elif [[ $type == 'file' ]]; then - COMPREPLY=() - compopt -o default - elif [[ $type == 'plain' ]]; then - COMPREPLY+=($value) - fi - done - - return 0 -} - -_nf_core_completion_setup() { - complete -o nosort -F _nf_core_completion nf-core -} - -_nf_core_completion_setup; - diff --git a/.nf-core-complete.zsh b/.nf-core-complete.zsh deleted file mode 100644 index ccd8b774b0..0000000000 --- a/.nf-core-complete.zsh +++ /dev/null @@ -1,35 +0,0 @@ -#compdef nf-core - -_nf_core_completion() { - local -a completions - local -a completions_with_descriptions - local -a response - (( ! $+commands[nf-core] )) && return 1 - - response=("${(@f)$(env COMP_WORDS="${words[*]}" COMP_CWORD=$((CURRENT-1)) _NF_CORE_COMPLETE=zsh_complete nf-core --no-header)}") - - for type key descr in ${response}; do - if [[ "$type" == "plain" ]]; then - if [[ "$descr" == "_" ]]; then - completions+=("$key") - else - completions_with_descriptions+=("$key":"$descr") - fi - elif [[ "$type" == "dir" ]]; then - _path_files -/ - elif [[ "$type" == "file" ]]; then - _path_files -f - fi - done - - if [ -n "$completions_with_descriptions" ]; then - _describe -V unsorted completions_with_descriptions -U - fi - - if [ -n "$completions" ]; then - compadd -U -V unsorted -a completions - fi -} - -compdef _nf_core_completion nf-core; - From 38b222f08057276ec5144db487d84ceb4d9daebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20H=C3=B6rtenhuber?= Date: Thu, 1 Dec 2022 10:13:31 +0000 Subject: [PATCH 6/6] add comment about `compinit` --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 425b7b5a2a..aa985cd471 100644 --- a/README.md +++ b/README.md @@ -225,9 +225,11 @@ fish | ~/.config/fish/completions/nf-core.fish | `eval (env _NF_CORE_COMPLETE=fi After a restart of the shell session you should have auto-completion for the `nf-core` command and all its sub-commands and options. -> **NB:** The added line will run the command `nf-core`. You must therefore have the nf-core/tools installed globally. +> **NB:** The added line will run the command `nf-core` (which will also slow down startup time of your shell). You should therefore either have the nf-core/tools installed globally. > You can also wrap it inside `if type nf-core > /dev/null; then ` \ `fi` for bash and zsh or `if command -v nf-core &> /dev/null eval (env _NF_CORE_COMPLETE=fish_source nf-core) end` for fish. You need to then source the config in your environment for the completions to be activated. +> **NB:** If you see the error `command not found compdef` , be sure that your config file contains the line `autoload -Uz compinit && compinit` before the eval line. + ## Listing pipelines The command `nf-core list` shows all available nf-core pipelines along with their latest version, when that was published and how recently the pipeline code was pulled to your local system (if at all).