sing-box.fish 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # fish completion for sing-box -*- shell-script -*-
  2. function __sing_box_debug
  3. set -l file "$BASH_COMP_DEBUG_FILE"
  4. if test -n "$file"
  5. echo "$argv" >> $file
  6. end
  7. end
  8. function __sing_box_perform_completion
  9. __sing_box_debug "Starting __sing_box_perform_completion"
  10. # Extract all args except the last one
  11. set -l args (commandline -opc)
  12. # Extract the last arg and escape it in case it is a space
  13. set -l lastArg (string escape -- (commandline -ct))
  14. __sing_box_debug "args: $args"
  15. __sing_box_debug "last arg: $lastArg"
  16. # Disable ActiveHelp which is not supported for fish shell
  17. set -l requestComp "SING_BOX_ACTIVE_HELP=0 $args[1] __complete $args[2..-1] $lastArg"
  18. __sing_box_debug "Calling $requestComp"
  19. set -l results (eval $requestComp 2> /dev/null)
  20. # Some programs may output extra empty lines after the directive.
  21. # Let's ignore them or else it will break completion.
  22. # Ref: https://github.com/spf13/cobra/issues/1279
  23. for line in $results[-1..1]
  24. if test (string trim -- $line) = ""
  25. # Found an empty line, remove it
  26. set results $results[1..-2]
  27. else
  28. # Found non-empty line, we have our proper output
  29. break
  30. end
  31. end
  32. set -l comps $results[1..-2]
  33. set -l directiveLine $results[-1]
  34. # For Fish, when completing a flag with an = (e.g., <program> -n=<TAB>)
  35. # completions must be prefixed with the flag
  36. set -l flagPrefix (string match -r -- '-.*=' "$lastArg")
  37. __sing_box_debug "Comps: $comps"
  38. __sing_box_debug "DirectiveLine: $directiveLine"
  39. __sing_box_debug "flagPrefix: $flagPrefix"
  40. for comp in $comps
  41. printf "%s%s\n" "$flagPrefix" "$comp"
  42. end
  43. printf "%s\n" "$directiveLine"
  44. end
  45. # this function limits calls to __sing_box_perform_completion, by caching the result behind $__sing_box_perform_completion_once_result
  46. function __sing_box_perform_completion_once
  47. __sing_box_debug "Starting __sing_box_perform_completion_once"
  48. if test -n "$__sing_box_perform_completion_once_result"
  49. __sing_box_debug "Seems like a valid result already exists, skipping __sing_box_perform_completion"
  50. return 0
  51. end
  52. set --global __sing_box_perform_completion_once_result (__sing_box_perform_completion)
  53. if test -z "$__sing_box_perform_completion_once_result"
  54. __sing_box_debug "No completions, probably due to a failure"
  55. return 1
  56. end
  57. __sing_box_debug "Performed completions and set __sing_box_perform_completion_once_result"
  58. return 0
  59. end
  60. # this function is used to clear the $__sing_box_perform_completion_once_result variable after completions are run
  61. function __sing_box_clear_perform_completion_once_result
  62. __sing_box_debug ""
  63. __sing_box_debug "========= clearing previously set __sing_box_perform_completion_once_result variable =========="
  64. set --erase __sing_box_perform_completion_once_result
  65. __sing_box_debug "Successfully erased the variable __sing_box_perform_completion_once_result"
  66. end
  67. function __sing_box_requires_order_preservation
  68. __sing_box_debug ""
  69. __sing_box_debug "========= checking if order preservation is required =========="
  70. __sing_box_perform_completion_once
  71. if test -z "$__sing_box_perform_completion_once_result"
  72. __sing_box_debug "Error determining if order preservation is required"
  73. return 1
  74. end
  75. set -l directive (string sub --start 2 $__sing_box_perform_completion_once_result[-1])
  76. __sing_box_debug "Directive is: $directive"
  77. set -l shellCompDirectiveKeepOrder 32
  78. set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) % 2)
  79. __sing_box_debug "Keeporder is: $keeporder"
  80. if test $keeporder -ne 0
  81. __sing_box_debug "This does require order preservation"
  82. return 0
  83. end
  84. __sing_box_debug "This doesn't require order preservation"
  85. return 1
  86. end
  87. # This function does two things:
  88. # - Obtain the completions and store them in the global __sing_box_comp_results
  89. # - Return false if file completion should be performed
  90. function __sing_box_prepare_completions
  91. __sing_box_debug ""
  92. __sing_box_debug "========= starting completion logic =========="
  93. # Start fresh
  94. set --erase __sing_box_comp_results
  95. __sing_box_perform_completion_once
  96. __sing_box_debug "Completion results: $__sing_box_perform_completion_once_result"
  97. if test -z "$__sing_box_perform_completion_once_result"
  98. __sing_box_debug "No completion, probably due to a failure"
  99. # Might as well do file completion, in case it helps
  100. return 1
  101. end
  102. set -l directive (string sub --start 2 $__sing_box_perform_completion_once_result[-1])
  103. set --global __sing_box_comp_results $__sing_box_perform_completion_once_result[1..-2]
  104. __sing_box_debug "Completions are: $__sing_box_comp_results"
  105. __sing_box_debug "Directive is: $directive"
  106. set -l shellCompDirectiveError 1
  107. set -l shellCompDirectiveNoSpace 2
  108. set -l shellCompDirectiveNoFileComp 4
  109. set -l shellCompDirectiveFilterFileExt 8
  110. set -l shellCompDirectiveFilterDirs 16
  111. if test -z "$directive"
  112. set directive 0
  113. end
  114. set -l compErr (math (math --scale 0 $directive / $shellCompDirectiveError) % 2)
  115. if test $compErr -eq 1
  116. __sing_box_debug "Received error directive: aborting."
  117. # Might as well do file completion, in case it helps
  118. return 1
  119. end
  120. set -l filefilter (math (math --scale 0 $directive / $shellCompDirectiveFilterFileExt) % 2)
  121. set -l dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) % 2)
  122. if test $filefilter -eq 1; or test $dirfilter -eq 1
  123. __sing_box_debug "File extension filtering or directory filtering not supported"
  124. # Do full file completion instead
  125. return 1
  126. end
  127. set -l nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) % 2)
  128. set -l nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) % 2)
  129. __sing_box_debug "nospace: $nospace, nofiles: $nofiles"
  130. # If we want to prevent a space, or if file completion is NOT disabled,
  131. # we need to count the number of valid completions.
  132. # To do so, we will filter on prefix as the completions we have received
  133. # may not already be filtered so as to allow fish to match on different
  134. # criteria than the prefix.
  135. if test $nospace -ne 0; or test $nofiles -eq 0
  136. set -l prefix (commandline -t | string escape --style=regex)
  137. __sing_box_debug "prefix: $prefix"
  138. set -l completions (string match -r -- "^$prefix.*" $__sing_box_comp_results)
  139. set --global __sing_box_comp_results $completions
  140. __sing_box_debug "Filtered completions are: $__sing_box_comp_results"
  141. # Important not to quote the variable for count to work
  142. set -l numComps (count $__sing_box_comp_results)
  143. __sing_box_debug "numComps: $numComps"
  144. if test $numComps -eq 1; and test $nospace -ne 0
  145. # We must first split on \t to get rid of the descriptions to be
  146. # able to check what the actual completion will be.
  147. # We don't need descriptions anyway since there is only a single
  148. # real completion which the shell will expand immediately.
  149. set -l split (string split --max 1 \t $__sing_box_comp_results[1])
  150. # Fish won't add a space if the completion ends with any
  151. # of the following characters: @=/:.,
  152. set -l lastChar (string sub -s -1 -- $split)
  153. if not string match -r -q "[@=/:.,]" -- "$lastChar"
  154. # In other cases, to support the "nospace" directive we trick the shell
  155. # by outputting an extra, longer completion.
  156. __sing_box_debug "Adding second completion to perform nospace directive"
  157. set --global __sing_box_comp_results $split[1] $split[1].
  158. __sing_box_debug "Completions are now: $__sing_box_comp_results"
  159. end
  160. end
  161. if test $numComps -eq 0; and test $nofiles -eq 0
  162. # To be consistent with bash and zsh, we only trigger file
  163. # completion when there are no other completions
  164. __sing_box_debug "Requesting file completion"
  165. return 1
  166. end
  167. end
  168. return 0
  169. end
  170. # Since Fish completions are only loaded once the user triggers them, we trigger them ourselves
  171. # so we can properly delete any completions provided by another script.
  172. # Only do this if the program can be found, or else fish may print some errors; besides,
  173. # the existing completions will only be loaded if the program can be found.
  174. if type -q "sing-box"
  175. # The space after the program name is essential to trigger completion for the program
  176. # and not completion of the program name itself.
  177. # Also, we use '> /dev/null 2>&1' since '&>' is not supported in older versions of fish.
  178. complete --do-complete "sing-box " > /dev/null 2>&1
  179. end
  180. # Remove any pre-existing completions for the program since we will be handling all of them.
  181. complete -c sing-box -e
  182. # this will get called after the two calls below and clear the $__sing_box_perform_completion_once_result global
  183. complete -c sing-box -n '__sing_box_clear_perform_completion_once_result'
  184. # The call to __sing_box_prepare_completions will setup __sing_box_comp_results
  185. # which provides the program's completion choices.
  186. # If this doesn't require order preservation, we don't use the -k flag
  187. complete -c sing-box -n 'not __sing_box_requires_order_preservation && __sing_box_prepare_completions' -f -a '$__sing_box_comp_results'
  188. # otherwise we use the -k flag
  189. complete -k -c sing-box -n '__sing_box_requires_order_preservation && __sing_box_prepare_completions' -f -a '$__sing_box_comp_results'