Dotfiles for my tiling window manager + terminal workflow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
9.7 KiB

4 weeks ago
  1. # fish completion for timetrace -*- shell-script -*-
  2. function __timetrace_debug
  3. set -l file "$BASH_COMP_DEBUG_FILE"
  4. if test -n "$file"
  5. echo "$argv" >> $file
  6. end
  7. end
  8. function __timetrace_perform_completion
  9. __timetrace_debug "Starting __timetrace_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. __timetrace_debug "args: $args"
  15. __timetrace_debug "last arg: $lastArg"
  16. # Disable ActiveHelp which is not supported for fish shell
  17. set -l requestComp "TIMETRACE_ACTIVE_HELP=0 $args[1] __complete $args[2..-1] $lastArg"
  18. __timetrace_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. __timetrace_debug "Comps: $comps"
  38. __timetrace_debug "DirectiveLine: $directiveLine"
  39. __timetrace_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 __timetrace_perform_completion, by caching the result behind $__timetrace_perform_completion_once_result
  46. function __timetrace_perform_completion_once
  47. __timetrace_debug "Starting __timetrace_perform_completion_once"
  48. if test -n "$__timetrace_perform_completion_once_result"
  49. __timetrace_debug "Seems like a valid result already exists, skipping __timetrace_perform_completion"
  50. return 0
  51. end
  52. set --global __timetrace_perform_completion_once_result (__timetrace_perform_completion)
  53. if test -z "$__timetrace_perform_completion_once_result"
  54. __timetrace_debug "No completions, probably due to a failure"
  55. return 1
  56. end
  57. __timetrace_debug "Performed completions and set __timetrace_perform_completion_once_result"
  58. return 0
  59. end
  60. # this function is used to clear the $__timetrace_perform_completion_once_result variable after completions are run
  61. function __timetrace_clear_perform_completion_once_result
  62. __timetrace_debug ""
  63. __timetrace_debug "========= clearing previously set __timetrace_perform_completion_once_result variable =========="
  64. set --erase __timetrace_perform_completion_once_result
  65. __timetrace_debug "Succesfully erased the variable __timetrace_perform_completion_once_result"
  66. end
  67. function __timetrace_requires_order_preservation
  68. __timetrace_debug ""
  69. __timetrace_debug "========= checking if order preservation is required =========="
  70. __timetrace_perform_completion_once
  71. if test -z "$__timetrace_perform_completion_once_result"
  72. __timetrace_debug "Error determining if order preservation is required"
  73. return 1
  74. end
  75. set -l directive (string sub --start 2 $__timetrace_perform_completion_once_result[-1])
  76. __timetrace_debug "Directive is: $directive"
  77. set -l shellCompDirectiveKeepOrder 32
  78. set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) % 2)
  79. __timetrace_debug "Keeporder is: $keeporder"
  80. if test $keeporder -ne 0
  81. __timetrace_debug "This does require order preservation"
  82. return 0
  83. end
  84. __timetrace_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 __timetrace_comp_results
  89. # - Return false if file completion should be performed
  90. function __timetrace_prepare_completions
  91. __timetrace_debug ""
  92. __timetrace_debug "========= starting completion logic =========="
  93. # Start fresh
  94. set --erase __timetrace_comp_results
  95. __timetrace_perform_completion_once
  96. __timetrace_debug "Completion results: $__timetrace_perform_completion_once_result"
  97. if test -z "$__timetrace_perform_completion_once_result"
  98. __timetrace_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 $__timetrace_perform_completion_once_result[-1])
  103. set --global __timetrace_comp_results $__timetrace_perform_completion_once_result[1..-2]
  104. __timetrace_debug "Completions are: $__timetrace_comp_results"
  105. __timetrace_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. __timetrace_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. __timetrace_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. __timetrace_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. __timetrace_debug "prefix: $prefix"
  138. set -l completions (string match -r -- "^$prefix.*" $__timetrace_comp_results)
  139. set --global __timetrace_comp_results $completions
  140. __timetrace_debug "Filtered completions are: $__timetrace_comp_results"
  141. # Important not to quote the variable for count to work
  142. set -l numComps (count $__timetrace_comp_results)
  143. __timetrace_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 $__timetrace_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. __timetrace_debug "Adding second completion to perform nospace directive"
  157. set --global __timetrace_comp_results $split[1] $split[1].
  158. __timetrace_debug "Completions are now: $__timetrace_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. __timetrace_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 "timetrace"
  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 "timetrace " > /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 timetrace -e
  182. # this will get called after the two calls below and clear the $__timetrace_perform_completion_once_result global
  183. complete -c timetrace -n '__timetrace_clear_perform_completion_once_result'
  184. # The call to __timetrace_prepare_completions will setup __timetrace_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 timetrace -n 'not __timetrace_requires_order_preservation && __timetrace_prepare_completions' -f -a '$__timetrace_comp_results'
  188. # otherwise we use the -k flag
  189. complete -k -c timetrace -n '__timetrace_requires_order_preservation && __timetrace_prepare_completions' -f -a '$__timetrace_comp_results'