nimpretty.bash-completion 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # bash completion for nimpretty -*- shell-script -*-
  2. __is_short_or_long()
  3. {
  4. local actual short long
  5. actual="$1"
  6. short="$2"
  7. long="$3"
  8. [[ ! -z $short && $actual == $short ]] && return 0
  9. [[ ! -z $long && $actual == $long ]] && return 0
  10. return 1
  11. }
  12. __ask_for_subcmd_or_subopts()
  13. {
  14. local args cmd subcmd words sub_words word_first word_last word_lastlast
  15. local len ilast ilastlast i ele sub_len n_nopts
  16. args=("$@")
  17. ask_for_what="${args[0]}"
  18. cmd="${args[1]}"
  19. subcmd="${args[2]}"
  20. ilast="${args[3]}"
  21. words=("${args[@]:4}")
  22. len=${#words[@]}
  23. ilastlast=$((ilast - 1))
  24. sub_words=("${words[@]:0:ilast}")
  25. sub_len=${#sub_words[@]}
  26. word_first=${words[0]}
  27. word_last=${words[ilast]}
  28. word_lastlast=${words[ilastlast]}
  29. n_nopts=0
  30. # printf "\n[DBUG] word_first:${word_first}|ilast:${ilast}|words(${len}):${words[*]}|sub_words(${sub_len}):${sub_words[*]}\n"
  31. if [[ $word_first != $cmd ]]
  32. then
  33. return 1
  34. fi
  35. i=0
  36. while [[ $i -lt $len ]]
  37. do
  38. ele=${words[i]}
  39. if [[ ! $ele =~ ^- ]]
  40. then
  41. if [[ $ele == $cmd || $ele == $subcmd ]]
  42. then
  43. ((n_nopts+=1))
  44. elif [[ $i -eq $ilast && $ele =~ ^[a-zA-Z] ]]
  45. then
  46. ((i=i))
  47. elif [[ -z $ele ]]
  48. then
  49. ((i=i))
  50. elif [[ $ele =~ ^: ]]
  51. then
  52. ((i+=1))
  53. else
  54. return 1
  55. fi
  56. fi
  57. ((i+=1))
  58. done
  59. case $ask_for_what in
  60. 1)
  61. if [[ n_nopts -eq 1 ]]
  62. then
  63. if [[ -z $word_last || $word_last =~ ^[a-zA-Z] ]] && [[ $word_lastlast != : ]]
  64. then
  65. return 0
  66. fi
  67. fi
  68. ;;
  69. 2)
  70. if [[ n_nopts -eq 2 ]]
  71. then
  72. if [[ -z $word_last ]] || [[ $word_last =~ ^[-:] ]]
  73. then
  74. return 0
  75. fi
  76. fi
  77. esac
  78. return 1
  79. }
  80. __ask_for_subcmd()
  81. {
  82. __ask_for_subcmd_or_subopts 1 "$@"
  83. }
  84. __ask_for_subcmd_opts()
  85. {
  86. __ask_for_subcmd_or_subopts 2 "$@"
  87. }
  88. _nimpretty()
  89. {
  90. local curr prev prevprev words
  91. local i_curr n_words i_prev i_prevprev
  92. COMPREPLY=()
  93. i_curr=$COMP_CWORD
  94. n_words=$((i_curr+1))
  95. i_prev=$((i_curr-1))
  96. i_prevprev=$((i_curr-2))
  97. curr="${COMP_WORDS[i_curr]}"
  98. prev="${COMP_WORDS[i_prev]}"
  99. prevprev="${COMP_WORDS[i_prevprev]}"
  100. words=("${COMP_WORDS[@]:0:n_words}")
  101. local subcmds opts candids
  102. # printf "\n[DBUG] curr:$curr|prev:$prev|words(${#words[*]}):${words[*]}\n"
  103. # Asking for a subcommand
  104. if false && __ask_for_subcmd nimpretty nimpretty $i_curr "${words[@]}"
  105. then
  106. subcmds=""
  107. return 0
  108. fi
  109. # Prioritize subcmd over opt
  110. if false
  111. then
  112. return 124
  113. elif false && __ask_for_subcmd_opts nimpretty compileToC $i_curr "${words[@]}"
  114. then
  115. opts=() \
  116. && candids=()
  117. else
  118. opts=() \
  119. && candids=()
  120. opts+=("" "--out" "file") \
  121. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  122. opts+=("" "--outDir" "DIR") \
  123. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  124. opts+=("" "--stdin" "") \
  125. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  126. opts+=("" "--indent" "N") \
  127. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  128. opts+=("" "--maxLineLen" "N") \
  129. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  130. opts+=("" "--version" "") \
  131. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  132. opts+=("" "--help" "") \
  133. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  134. fi
  135. local c_short c_long c_accvals
  136. local len i idx0 idx1 idx2
  137. case $curr in
  138. # Asking for accepted optvalues, e.g., `out:`
  139. :)
  140. len=${#opts[@]}
  141. i=0
  142. while [[ $i -lt $len ]]
  143. do
  144. idx0=$((i / 3 * 3))
  145. idx1=$((idx0 + 1))
  146. idx2=$((idx1 + 1))
  147. c_short=${opts[idx0]}
  148. c_long=${opts[idx1]}
  149. c_accvals=${opts[idx2]}
  150. (false \
  151. || __is_short_or_long $prev ${c_short} ${c_long} \
  152. || false) \
  153. && COMPREPLY=( $(compgen -W "${c_accvals}" --) ) \
  154. && return 0
  155. ((i+=3))
  156. done
  157. return 124
  158. ;;
  159. *)
  160. # When in a incomplete opt value, e.g., `--check:of`
  161. if [[ $prev == : ]]
  162. then
  163. len=${#opts[@]}
  164. i=0
  165. while [[ $i -lt $len ]]
  166. do
  167. idx0=$((i / 3 * 3))
  168. idx1=$((idx0 + 1))
  169. idx2=$((idx1 + 1))
  170. c_short=${opts[idx0]}
  171. c_long=${opts[idx1]}
  172. c_accvals=${opts[idx2]}
  173. (false \
  174. || __is_short_or_long $prevprev ${c_short} ${c_long} \
  175. || false) \
  176. && COMPREPLY=( $(compgen -W "${c_accvals}" -- ${curr}) ) \
  177. && return 0
  178. ((i+=3))
  179. done
  180. return 124
  181. fi
  182. # When in a complete optname, might need optvalue, e.g., `--check`
  183. if [[ $curr =~ ^--?[:()a-zA-Z]+$ ]]
  184. then
  185. len=${#opts[@]}
  186. i=0
  187. while [[ $i -lt $len ]]
  188. do
  189. idx0=$(((i / 3 * 3)))
  190. idx1=$((idx0 + 1))
  191. idx2=$((idx1 + 1))
  192. c_short=${opts[idx0]}
  193. c_long=${opts[idx1]}
  194. c_accvals=${opts[idx2]}
  195. if __is_short_or_long $curr ${c_short} ${c_long}
  196. then
  197. if [[ ! -z $c_accvals ]]
  198. then
  199. COMPREPLY=( $(compgen -W "${curr}:" -- ${curr}) ) \
  200. && compopt -o nospace \
  201. && return 0
  202. else
  203. COMPREPLY=( $(compgen -W "${curr}" -- ${curr}) ) \
  204. && return 0
  205. fi
  206. fi
  207. ((i+=3))
  208. done # while
  209. if true
  210. then
  211. COMPREPLY=( $(compgen -W "${candids[*]}" -- "$curr") )
  212. compopt -o nospace
  213. return 0
  214. fi
  215. # When in an incomplete optname, e.g., `--chec`
  216. elif [[ $curr =~ ^--?[^:]* ]]
  217. then
  218. if true
  219. then
  220. COMPREPLY=( $(compgen -W "${candids[*]}" -- "$curr") )
  221. compopt -o nospace
  222. return 0
  223. fi
  224. fi
  225. if true
  226. then
  227. compopt -o filenames
  228. COMPREPLY=( $(compgen -f -- "$curr") )
  229. compopt -o nospace
  230. return 0
  231. fi
  232. ;;
  233. esac
  234. return 0
  235. } &&
  236. complete -F _nimpretty nimpretty
  237. # ex: filetype=sh