UNPKG

3.21 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const script = `#!/usr/bin/env bash
4
5# This function joins an array using a character passed in
6# e.g. ARRAY=(one two three) -> join_by ":" \${ARRAY[@]} -> "one:two:three"
7function join_by { local IFS="$1"; shift; echo "$*"; }
8
9_<CLI_BIN>_autocomplete()
10{
11
12 local cur="\${COMP_WORDS[COMP_CWORD]}" opts normalizedCommand colonPrefix IFS=$' \\t\\n'
13 COMPREPLY=()
14
15 local commands="
16<BASH_COMMANDS_WITH_FLAGS_LIST>
17"
18
19 function __trim_colon_commands()
20 {
21 # Turn $commands into an array
22 commands=("\${commands[@]}")
23
24 if [[ -z "$colonPrefix" ]]; then
25 colonPrefix="$normalizedCommand:"
26 fi
27
28 # Remove colon-word prefix from $commands
29 commands=( "\${commands[@]/$colonPrefix}" )
30
31 for i in "\${!commands[@]}"; do
32 if [[ "\${commands[$i]}" == "$normalizedCommand" ]]; then
33 # If the currently typed in command is a topic command we need to remove it to avoid suggesting it again
34 unset "\${commands[$i]}"
35 else
36 # Trim subcommands from each command
37 commands[$i]="\${commands[$i]%%:*}"
38 fi
39 done
40 }
41
42 if [[ "$cur" != "-"* ]]; then
43 # Command
44 __COMP_WORDS=( "\${COMP_WORDS[@]:1}" )
45
46 # The command typed by the user but separated by colons (e.g. "mycli command subcom" -> "command:subcom")
47 normalizedCommand="$( printf "%s" "$(join_by ":" "\${__COMP_WORDS[@]}")" )"
48
49 # The command hirarchy, with colons, leading up to the last subcommand entered (e.g. "mycli com subcommand subsubcom" -> "com:subcommand:")
50 colonPrefix="\${normalizedCommand%"\${normalizedCommand##*:}"}"
51
52 if [[ -z "$normalizedCommand" ]]; then
53 # If there is no normalizedCommand yet the user hasn't typed in a full command
54 # So we should trim all subcommands & flags from $commands so we can suggest all top level commands
55 opts=$(printf "%s " "\${commands[@]}" | grep -Eo '^[a-zA-Z0-9_-]+')
56 else
57 # Filter $commands to just the ones that match the $normalizedCommand and turn into an array
58 commands=( $(compgen -W "$commands" -- "\${normalizedCommand}") )
59 # Trim higher level and subcommands from the subcommands to suggest
60 __trim_colon_commands "$colonPrefix"
61
62 opts=$(printf "%s " "\${commands[@]}") # | grep -Eo '^[a-zA-Z0-9_-]+'
63 fi
64 else
65 # Flag
66
67 # The full CLI command separated by colons (e.g. "mycli command subcommand --fl" -> "command:subcommand")
68 # This needs to be defined with $COMP_CWORD-1 as opposed to above because the current "word" on the command line is a flag and the command is everything before the flag
69 normalizedCommand="$( printf "%s" "$(join_by ":" "\${COMP_WORDS[@]:1:($COMP_CWORD - 1)}")" )"
70
71 # The line below finds the command in $commands using grep
72 # Then, using sed, it removes everything from the found command before the --flags (e.g. "command:subcommand:subsubcom --flag1 --flag2" -> "--flag1 --flag2")
73 opts=$(printf "%s " "\${commands[@]}" | grep "\${normalizedCommand}" | sed -n "s/^\${normalizedCommand} //p")
74 fi
75
76 COMPREPLY=($(compgen -W "$opts" -- "\${cur}"))
77}
78
79complete -F _<CLI_BIN>_autocomplete <CLI_BIN>
80`;
81exports.default = script;