Debian Codegraph @

cylc/flow/etc/cylc-completion.bash

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #------------------------------------------------------------------------------
17 # USAGE
18 # Sets up bash auto-completion for cylc commands, workflows, tasks and more.
19 #
20 # Make a copy and source this file in your ~/.bash_profile file like this:
21 #
22 # if [[ $- =~ i && -f /path/to/cylc-completion.bash ]]; then
23 # . /path/to/cylc-completion.bash
24 # fi
25 #
26 # Cylc will launch a "completion server" to perform completions. The
27 # server will shut itself down when idle. When it does you may see a line
28 # like this in your console:
29 # [1]+ Done coproc COPROC cylc completion-server
30 #
31 # To adjust the timeout see the --timeout option of "cylc completion-server".
32 #
33 # If the completion-server fails to launch (e.g. your environment is
34 # configured to run Cylc 7 which doesn't have this completion command)
35 # you'll see a similar message:
36 # [1]+ Exit 1 coproc COPROC cylc completion-server
37 #
38 # Administrators may want to place this file in the
39 # /etc/bash_completion.d/ (or equivalent) directory.
40 #------------------------------------------------------------------------------
41
42 export CYLC_COMPLETION_LANG=bash
43 export CYLC_COMPLETION_SCRIPT_VERSION=1.0.0
44
45 _cylc_completion () {
46 # start the completion server if needed
47 if [[ -z ${COPROC[0]} ]]; then
48 {
49 coproc cylc completion-server
50 } >/dev/null 2>/dev/null # suppress cproc output (includes PID)
51 fi
52
53 sleep 0 # needed for COPROC variable updates in error cases
54
55 # send a completion request
56 {
57 echo "$(
58 IFS='|'
59 echo "${COMP_WORDS[*]}"
60 )" >&"${COPROC[1]}" # shellcheck disable=SC2005
61 # (shellcheck disable because subshell needed to set IFS)
62 } 2>/dev/null \
63 || return # write failed e.g. server failed to start
64
65
66 # read a completion response
67 read -a COMPREPLY <&"${COPROC[0]}" 2>/dev/null \
68 || return # read failed
69
70 # tell Bash whether or not to follow the completion with a space
71 if [[ ${COMPREPLY[0]} = */ ]]; then
72 compopt -o nospace
73 else
74 compopt +o nospace
75 fi
76 }
77
78 complete -F _cylc_completion cylc