completion-arduino.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. # bash completion V2 for arduino-cli -*- shell-script -*-
  2. # Generated using `arduino-cli completion bash`
  3. __arduino-cli_debug()
  4. {
  5. if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then
  6. echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
  7. fi
  8. }
  9. # Macs have bash3 for which the bash-completion package doesn't include
  10. # _init_completion. This is a minimal version of that function.
  11. __arduino-cli_init_completion()
  12. {
  13. COMPREPLY=()
  14. _get_comp_words_by_ref "$@" cur prev words cword
  15. }
  16. # This function calls the arduino-cli program to obtain the completion
  17. # results and the directive. It fills the 'out' and 'directive' vars.
  18. __arduino-cli_get_completion_results() {
  19. local requestComp lastParam lastChar args
  20. # Prepare the command to request completions for the program.
  21. # Calling ${words[0]} instead of directly arduino-cli allows to handle aliases
  22. args=("${words[@]:1}")
  23. requestComp="${words[0]} __complete ${args[*]}"
  24. lastParam=${words[$((${#words[@]}-1))]}
  25. lastChar=${lastParam:$((${#lastParam}-1)):1}
  26. __arduino-cli_debug "lastParam ${lastParam}, lastChar ${lastChar}"
  27. if [[ -z ${cur} && ${lastChar} != = ]]; then
  28. # If the last parameter is complete (there is a space following it)
  29. # We add an extra empty parameter so we can indicate this to the go method.
  30. __arduino-cli_debug "Adding extra empty parameter"
  31. requestComp="${requestComp} ''"
  32. fi
  33. # When completing a flag with an = (e.g., arduino-cli -n=<TAB>)
  34. # bash focuses on the part after the =, so we need to remove
  35. # the flag part from $cur
  36. if [[ ${cur} == -*=* ]]; then
  37. cur="${cur#*=}"
  38. fi
  39. __arduino-cli_debug "Calling ${requestComp}"
  40. # Use eval to handle any environment variables and such
  41. out=$(eval "${requestComp}" 2>/dev/null)
  42. # Extract the directive integer at the very end of the output following a colon (:)
  43. directive=${out##*:}
  44. # Remove the directive
  45. out=${out%:*}
  46. if [[ ${directive} == "${out}" ]]; then
  47. # There is not directive specified
  48. directive=0
  49. fi
  50. __arduino-cli_debug "The completion directive is: ${directive}"
  51. __arduino-cli_debug "The completions are: ${out}"
  52. }
  53. __arduino-cli_process_completion_results() {
  54. local shellCompDirectiveError=1
  55. local shellCompDirectiveNoSpace=2
  56. local shellCompDirectiveNoFileComp=4
  57. local shellCompDirectiveFilterFileExt=8
  58. local shellCompDirectiveFilterDirs=16
  59. local shellCompDirectiveKeepOrder=32
  60. if (((directive & shellCompDirectiveError) != 0)); then
  61. # Error code. No completion.
  62. __arduino-cli_debug "Received error from custom completion go code"
  63. return
  64. else
  65. if (((directive & shellCompDirectiveNoSpace) != 0)); then
  66. if [[ $(type -t compopt) == builtin ]]; then
  67. __arduino-cli_debug "Activating no space"
  68. compopt -o nospace
  69. else
  70. __arduino-cli_debug "No space directive not supported in this version of bash"
  71. fi
  72. fi
  73. if (((directive & shellCompDirectiveKeepOrder) != 0)); then
  74. if [[ $(type -t compopt) == builtin ]]; then
  75. # no sort isn't supported for bash less than < 4.4
  76. if [[ ${BASH_VERSINFO[0]} -lt 4 || ( ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 4 ) ]]; then
  77. __arduino-cli_debug "No sort directive not supported in this version of bash"
  78. else
  79. __arduino-cli_debug "Activating keep order"
  80. compopt -o nosort
  81. fi
  82. else
  83. __arduino-cli_debug "No sort directive not supported in this version of bash"
  84. fi
  85. fi
  86. if (((directive & shellCompDirectiveNoFileComp) != 0)); then
  87. if [[ $(type -t compopt) == builtin ]]; then
  88. __arduino-cli_debug "Activating no file completion"
  89. compopt +o default
  90. else
  91. __arduino-cli_debug "No file completion directive not supported in this version of bash"
  92. fi
  93. fi
  94. fi
  95. # Separate activeHelp from normal completions
  96. local completions=()
  97. local activeHelp=()
  98. __arduino-cli_extract_activeHelp
  99. if (((directive & shellCompDirectiveFilterFileExt) != 0)); then
  100. # File extension filtering
  101. local fullFilter filter filteringCmd
  102. # Do not use quotes around the $completions variable or else newline
  103. # characters will be kept.
  104. for filter in ${completions[*]}; do
  105. fullFilter+="$filter|"
  106. done
  107. filteringCmd="_filedir $fullFilter"
  108. __arduino-cli_debug "File filtering command: $filteringCmd"
  109. $filteringCmd
  110. elif (((directive & shellCompDirectiveFilterDirs) != 0)); then
  111. # File completion for directories only
  112. local subdir
  113. subdir=${completions[0]}
  114. if [[ -n $subdir ]]; then
  115. __arduino-cli_debug "Listing directories in $subdir"
  116. pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return
  117. else
  118. __arduino-cli_debug "Listing directories in ."
  119. _filedir -d
  120. fi
  121. else
  122. __arduino-cli_handle_completion_types
  123. fi
  124. __arduino-cli_handle_special_char "$cur" :
  125. __arduino-cli_handle_special_char "$cur" =
  126. # Print the activeHelp statements before we finish
  127. if ((${#activeHelp[*]} != 0)); then
  128. printf "\n";
  129. printf "%s\n" "${activeHelp[@]}"
  130. printf "\n"
  131. # The prompt format is only available from bash 4.4.
  132. # We test if it is available before using it.
  133. if (x=${PS1@P}) 2> /dev/null; then
  134. printf "%s" "${PS1@P}${COMP_LINE[@]}"
  135. else
  136. # Can't print the prompt. Just print the
  137. # text the user had typed, it is workable enough.
  138. printf "%s" "${COMP_LINE[@]}"
  139. fi
  140. fi
  141. }
  142. # Separate activeHelp lines from real completions.
  143. # Fills the $activeHelp and $completions arrays.
  144. __arduino-cli_extract_activeHelp() {
  145. local activeHelpMarker="_activeHelp_ "
  146. local endIndex=${#activeHelpMarker}
  147. while IFS='' read -r comp; do
  148. if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then
  149. comp=${comp:endIndex}
  150. __arduino-cli_debug "ActiveHelp found: $comp"
  151. if [[ -n $comp ]]; then
  152. activeHelp+=("$comp")
  153. fi
  154. else
  155. # Not an activeHelp line but a normal completion
  156. completions+=("$comp")
  157. fi
  158. done <<<"${out}"
  159. }
  160. __arduino-cli_handle_completion_types() {
  161. __arduino-cli_debug "__arduino-cli_handle_completion_types: COMP_TYPE is $COMP_TYPE"
  162. case $COMP_TYPE in
  163. 37|42)
  164. # Type: menu-complete/menu-complete-backward and insert-completions
  165. # If the user requested inserting one completion at a time, or all
  166. # completions at once on the command-line we must remove the descriptions.
  167. # https://github.com/spf13/cobra/issues/1508
  168. local tab=$'\t' comp
  169. while IFS='' read -r comp; do
  170. [[ -z $comp ]] && continue
  171. # Strip any description
  172. comp=${comp%%$tab*}
  173. # Only consider the completions that match
  174. if [[ $comp == "$cur"* ]]; then
  175. COMPREPLY+=("$comp")
  176. fi
  177. done < <(printf "%s\n" "${completions[@]}")
  178. ;;
  179. *)
  180. # Type: complete (normal completion)
  181. __arduino-cli_handle_standard_completion_case
  182. ;;
  183. esac
  184. }
  185. __arduino-cli_handle_standard_completion_case() {
  186. local tab=$'\t' comp
  187. # Short circuit to optimize if we don't have descriptions
  188. if [[ "${completions[*]}" != *$tab* ]]; then
  189. IFS=$'\n' read -ra COMPREPLY -d '' < <(compgen -W "${completions[*]}" -- "$cur")
  190. return 0
  191. fi
  192. local longest=0
  193. local compline
  194. # Look for the longest completion so that we can format things nicely
  195. while IFS='' read -r compline; do
  196. [[ -z $compline ]] && continue
  197. # Strip any description before checking the length
  198. comp=${compline%%$tab*}
  199. # Only consider the completions that match
  200. [[ $comp == "$cur"* ]] || continue
  201. COMPREPLY+=("$compline")
  202. if ((${#comp}>longest)); then
  203. longest=${#comp}
  204. fi
  205. done < <(printf "%s\n" "${completions[@]}")
  206. # If there is a single completion left, remove the description text
  207. if ((${#COMPREPLY[*]} == 1)); then
  208. __arduino-cli_debug "COMPREPLY[0]: ${COMPREPLY[0]}"
  209. comp="${COMPREPLY[0]%%$tab*}"
  210. __arduino-cli_debug "Removed description from single completion, which is now: ${comp}"
  211. COMPREPLY[0]=$comp
  212. else # Format the descriptions
  213. __arduino-cli_format_comp_descriptions $longest
  214. fi
  215. }
  216. __arduino-cli_handle_special_char()
  217. {
  218. local comp="$1"
  219. local char=$2
  220. if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then
  221. local word=${comp%"${comp##*${char}}"}
  222. local idx=${#COMPREPLY[*]}
  223. while ((--idx >= 0)); do
  224. COMPREPLY[idx]=${COMPREPLY[idx]#"$word"}
  225. done
  226. fi
  227. }
  228. __arduino-cli_format_comp_descriptions()
  229. {
  230. local tab=$'\t'
  231. local comp desc maxdesclength
  232. local longest=$1
  233. local i ci
  234. for ci in ${!COMPREPLY[*]}; do
  235. comp=${COMPREPLY[ci]}
  236. # Properly format the description string which follows a tab character if there is one
  237. if [[ "$comp" == *$tab* ]]; then
  238. __arduino-cli_debug "Original comp: $comp"
  239. desc=${comp#*$tab}
  240. comp=${comp%%$tab*}
  241. # $COLUMNS stores the current shell width.
  242. # Remove an extra 4 because we add 2 spaces and 2 parentheses.
  243. maxdesclength=$(( COLUMNS - longest - 4 ))
  244. # Make sure we can fit a description of at least 8 characters
  245. # if we are to align the descriptions.
  246. if ((maxdesclength > 8)); then
  247. # Add the proper number of spaces to align the descriptions
  248. for ((i = ${#comp} ; i < longest ; i++)); do
  249. comp+=" "
  250. done
  251. else
  252. # Don't pad the descriptions so we can fit more text after the completion
  253. maxdesclength=$(( COLUMNS - ${#comp} - 4 ))
  254. fi
  255. # If there is enough space for any description text,
  256. # truncate the descriptions that are too long for the shell width
  257. if ((maxdesclength > 0)); then
  258. if ((${#desc} > maxdesclength)); then
  259. desc=${desc:0:$(( maxdesclength - 1 ))}
  260. desc+="…"
  261. fi
  262. comp+=" ($desc)"
  263. fi
  264. COMPREPLY[ci]=$comp
  265. __arduino-cli_debug "Final comp: $comp"
  266. fi
  267. done
  268. }
  269. __start_arduino-cli()
  270. {
  271. local cur prev words cword split
  272. COMPREPLY=()
  273. # Call _init_completion from the bash-completion package
  274. # to prepare the arguments properly
  275. if declare -F _init_completion >/dev/null 2>&1; then
  276. _init_completion -n =: || return
  277. else
  278. __arduino-cli_init_completion -n =: || return
  279. fi
  280. __arduino-cli_debug
  281. __arduino-cli_debug "========= starting completion logic =========="
  282. __arduino-cli_debug "cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}, cword is $cword"
  283. # The user could have moved the cursor backwards on the command-line.
  284. # We need to trigger completion from the $cword location, so we need
  285. # to truncate the command-line ($words) up to the $cword location.
  286. words=("${words[@]:0:$cword+1}")
  287. __arduino-cli_debug "Truncated words[*]: ${words[*]},"
  288. local out directive
  289. __arduino-cli_get_completion_results
  290. __arduino-cli_process_completion_results
  291. }
  292. if [[ $(type -t compopt) = "builtin" ]]; then
  293. complete -o default -F __start_arduino-cli arduino-cli
  294. else
  295. complete -o default -o nospace -F __start_arduino-cli arduino-cli
  296. fi
  297. # ex: ts=4 sw=4 et filetype=sh