testMathZshCompletionScript().zsh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #compdef math
  2. __math_complete() {
  3. local -ar non_empty_completions=("${@:#(|:*)}")
  4. local -ar empty_completions=("${(M)@:#(|:*)}")
  5. _describe -V '' non_empty_completions -- empty_completions -P $'\'\''
  6. }
  7. __math_custom_complete() {
  8. local -a completions
  9. completions=("${(@f)"$("${command_name}" "${@}" "${command_line[@]}")"}")
  10. if [[ "${#completions[@]}" -gt 1 ]]; then
  11. __math_complete "${completions[@]:0:-1}"
  12. fi
  13. }
  14. __math_cursor_index_in_current_word() {
  15. if [[ -z "${QIPREFIX}${IPREFIX}${PREFIX}" ]]; then
  16. printf 0
  17. else
  18. printf %s "${#${(z)LBUFFER}[-1]}"
  19. fi
  20. }
  21. _math() {
  22. emulate -RL zsh -G
  23. setopt extendedglob nullglob numericglobsort
  24. unsetopt aliases banghist
  25. local -xr SAP_SHELL=zsh
  26. local -x SAP_SHELL_VERSION
  27. SAP_SHELL_VERSION="$(builtin emulate zsh -c 'printf %s "${ZSH_VERSION}"')"
  28. local -r SAP_SHELL_VERSION
  29. local context state state_descr line
  30. local -A opt_args
  31. local -r command_name="${words[1]}"
  32. local -ar command_line=("${words[@]}")
  33. local -ir current_word_index="$((CURRENT - 1))"
  34. local -i ret=1
  35. local -ar arg_specs=(
  36. '--version[Show the version.]'
  37. '(-h --help)'{-h,--help}'[Show help information.]'
  38. '(-): :->command'
  39. '(-)*:: :->arg'
  40. )
  41. _arguments -w -s -S : "${arg_specs[@]}" && ret=0
  42. case "${state}" in
  43. command)
  44. local -ar subcommands=(
  45. 'add:Print the sum of the values.'
  46. 'multiply:Print the product of the values.'
  47. 'stats:Calculate descriptive statistics.'
  48. 'help:Show subcommand help information.'
  49. )
  50. _describe -V subcommand subcommands && ret=0
  51. ;;
  52. arg)
  53. case "${words[1]}" in
  54. add|multiply|stats|help)
  55. "_math_${words[1]}" && ret=0
  56. ;;
  57. esac
  58. ;;
  59. esac
  60. return "${ret}"
  61. }
  62. _math_add() {
  63. local -i ret=1
  64. local -ar arg_specs=(
  65. '(--hex-output -x)'{--hex-output,-x}'[Use hexadecimal notation for the result.]'
  66. '*:values:'
  67. '--version[Show the version.]'
  68. '(-h --help)'{-h,--help}'[Show help information.]'
  69. )
  70. _arguments -w -s -S : "${arg_specs[@]}" && ret=0
  71. return "${ret}"
  72. }
  73. _math_multiply() {
  74. local -i ret=1
  75. local -ar arg_specs=(
  76. '(--hex-output -x)'{--hex-output,-x}'[Use hexadecimal notation for the result.]'
  77. '*:values:'
  78. '--version[Show the version.]'
  79. '(-h --help)'{-h,--help}'[Show help information.]'
  80. )
  81. _arguments -w -s -S : "${arg_specs[@]}" && ret=0
  82. return "${ret}"
  83. }
  84. _math_stats() {
  85. local -i ret=1
  86. local -ar arg_specs=(
  87. '--version[Show the version.]'
  88. '(-h --help)'{-h,--help}'[Show help information.]'
  89. '(-): :->command'
  90. '(-)*:: :->arg'
  91. )
  92. _arguments -w -s -S : "${arg_specs[@]}" && ret=0
  93. case "${state}" in
  94. command)
  95. local -ar subcommands=(
  96. 'average:Print the average of the values.'
  97. 'stdev:Print the standard deviation of the values.'
  98. 'quantiles:Print the quantiles of the values (TBD).'
  99. )
  100. _describe -V subcommand subcommands && ret=0
  101. ;;
  102. arg)
  103. case "${words[1]}" in
  104. average|stdev|quantiles)
  105. "_math_stats_${words[1]}" && ret=0
  106. ;;
  107. esac
  108. ;;
  109. esac
  110. return "${ret}"
  111. }
  112. _math_stats_average() {
  113. local -i ret=1
  114. local -ar ___kind=('mean' 'median' 'mode')
  115. local -ar arg_specs=(
  116. '--kind[The kind of average to provide.]:kind:{__math_complete "${___kind[@]}"}'
  117. '*:values:'
  118. '--version[Show the version.]'
  119. '(-h --help)'{-h,--help}'[Show help information.]'
  120. )
  121. _arguments -w -s -S : "${arg_specs[@]}" && ret=0
  122. return "${ret}"
  123. }
  124. _math_stats_stdev() {
  125. local -i ret=1
  126. local -ar arg_specs=(
  127. '*:values:'
  128. '--version[Show the version.]'
  129. '(-h --help)'{-h,--help}'[Show help information.]'
  130. )
  131. _arguments -w -s -S : "${arg_specs[@]}" && ret=0
  132. return "${ret}"
  133. }
  134. _math_stats_quantiles() {
  135. local -i ret=1
  136. local -ar _one_of_four=('alphabet' 'alligator' 'branch' 'braggart')
  137. local -ar arg_specs=(
  138. ':one-of-four:{__math_complete "${_one_of_four[@]}"}'
  139. ':custom-arg:{__math_custom_complete ---completion stats quantiles -- positional@1 "${current_word_index}" "$(__math_cursor_index_in_current_word)"}'
  140. ':custom-deprecated-arg:{__math_custom_complete ---completion stats quantiles -- positional@2}'
  141. '*:values:'
  142. '--file:file:_files -g '\''*.txt *.md'\'''
  143. '--directory:directory:_files -/'
  144. '--shell:shell:{local -a list;list=(${(f)"$(head -100 '\''/usr/share/dict/words'\'' | tail -50)"});_describe -V "" list}'
  145. '--custom:custom:{__math_custom_complete ---completion stats quantiles -- --custom "${current_word_index}" "$(__math_cursor_index_in_current_word)"}'
  146. '--custom-deprecated:custom-deprecated:{__math_custom_complete ---completion stats quantiles -- --custom-deprecated}'
  147. '--version[Show the version.]'
  148. '(-h --help)'{-h,--help}'[Show help information.]'
  149. )
  150. _arguments -w -s -S : "${arg_specs[@]}" && ret=0
  151. return "${ret}"
  152. }
  153. _math_help() {
  154. local -i ret=1
  155. local -ar arg_specs=(
  156. '*:subcommands:'
  157. '--version[Show the version.]'
  158. )
  159. _arguments -w -s -S : "${arg_specs[@]}" && ret=0
  160. return "${ret}"
  161. }
  162. if [[ "${funcstack[1]}" = _math ]]; then
  163. _math "${@}"
  164. else
  165. compdef _math math
  166. fi