_task 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #compdef task
  2. #
  3. # Copyright 2010 - 2016 Johannes Schlatow
  4. # Copyright 2009 P.C. Shyamshankar
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included
  14. # in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. #
  24. # https://www.opensource.org/licenses/mit-license.php
  25. #
  26. typeset -g _task_cmds _task_projects _task_tags _task_config _task_modifiers
  27. _task_projects=($(task _projects))
  28. _task_tags=($(task _tags))
  29. _task_zshids=( ${(f)"$(task _zshids)"} )
  30. _task_config=($(task _config))
  31. _task_columns=($(task _columns))
  32. _task_modifiers=(
  33. 'before' \
  34. 'after' \
  35. 'none' \
  36. 'any' \
  37. 'is' \
  38. 'isnt' \
  39. 'has' \
  40. 'hasnt' \
  41. 'startswith' \
  42. 'endswith' \
  43. 'word' \
  44. 'noword'
  45. )
  46. _task_conjunctions=(
  47. 'and' \
  48. 'or' \
  49. 'xor' \
  50. '\)' \
  51. '\(' \
  52. '<' \
  53. '<=' \
  54. '=' \
  55. '!=' \
  56. '>=' \
  57. '>'
  58. )
  59. _task_cmds=($(task _commands; task _aliases))
  60. _task_zshcmds=( ${(f)"$(task _zshcommands)"} sentinel:sentinel:sentinel )
  61. _task_aliases=($(task _aliases))
  62. _task() {
  63. _arguments -s -S \
  64. "*::task default:_task_default"
  65. return 0
  66. }
  67. local -a reply args word
  68. word=$'[^\0]#\0'
  69. # priorities
  70. local -a task_priorities
  71. _regex_words values 'task priorities' \
  72. 'H:High' \
  73. 'M:Middle' \
  74. 'L:Low'
  75. task_priorities=("$reply[@]")
  76. # projects
  77. local -a task_projects
  78. task_projects=(
  79. /"$word"/
  80. ":values:task projects:compadd -a _task_projects"
  81. )
  82. local -a _task_dates
  83. _regex_words values 'task dates' \
  84. 'tod*ay:Today' \
  85. 'yes*terday:Yesterday' \
  86. 'tom*orrow:Tomorrow' \
  87. 'sow:Start of week' \
  88. 'soww:Start of work week' \
  89. 'socw:Start of calendar week' \
  90. 'som:Start of month' \
  91. 'soq:Start of quarter' \
  92. 'soy:Start of year' \
  93. 'eow:End of week' \
  94. 'eoww:End of work week' \
  95. 'eocw:End of calendar week' \
  96. 'eom:End of month' \
  97. 'eoq:End of quarter' \
  98. 'eoy:End of year' \
  99. 'mon:Monday' \
  100. 'tue:Tuesday'\
  101. 'wed:Wednesday' \
  102. 'thu:Thursday' \
  103. 'fri:Friday' \
  104. 'sat:Saturday' \
  105. 'sun:Sunday' \
  106. 'good*friday:Good Friday' \
  107. 'easter:Easter' \
  108. 'eastermonday:Easter Monday' \
  109. 'ascension:Ascension' \
  110. 'pentecost:Pentecost' \
  111. 'midsommar:Midsommar' \
  112. 'midsommarafton:Midsommarafton' \
  113. 'later:Later' \
  114. 'someday:Some Day'
  115. _task_dates=("$reply[@]")
  116. local -a _task_reldates
  117. _regex_words values 'task reldates' \
  118. 'hrs:n hours' \
  119. 'day:n days' \
  120. '1st:first' \
  121. '2nd:second' \
  122. '3rd:third' \
  123. 'th:4th, 5th, etc.' \
  124. 'wks:weeks'
  125. _task_reldates=("$reply[@]")
  126. task_dates=(
  127. \( "$_task_dates[@]" \|
  128. \( /$'[0-9][0-9]#'/- \( "$_task_reldates[@]" \) \)
  129. \)
  130. )
  131. local -a task_zshids
  132. if (( $#_task_zshids )); then
  133. _regex_words values 'task IDs' $_task_zshids
  134. task_zshids=("$reply[@]")
  135. fi
  136. _regex_words values 'task frequencies' \
  137. 'daily:Every day' \
  138. 'day:Every day' \
  139. 'weekdays:Every day skipping weekend days' \
  140. 'weekly:Every week' \
  141. 'biweekly:Every two weeks' \
  142. 'fortnight:Every two weeks' \
  143. 'monthly:Every month' \
  144. 'quarterly:Every three months' \
  145. 'semiannual:Every six months' \
  146. 'annual:Every year' \
  147. 'yearly:Every year' \
  148. 'biannual:Every two years' \
  149. 'biyearly:Every two years'
  150. _task_freqs=("$reply[@]")
  151. local -a _task_frequencies
  152. _regex_words values 'task frequencies' \
  153. 'd:days' \
  154. 'w:weeks' \
  155. 'q:quarters' \
  156. 'y:years'
  157. _task_frequencies=("$reply[@]")
  158. task_freqs=(
  159. \( "$_task_freqs[@]" \|
  160. \( /$'[0-9][0-9]#'/- \( "$_task_frequencies[@]" \) \)
  161. \)
  162. )
  163. # attributes
  164. local -a task_attributes
  165. _regex_words -t ':' default 'task attributes' \
  166. 'des*cription:Task description text' \
  167. 'status:Status of task - pending, completed, deleted, waiting' \
  168. 'pro*ject:Project name:$task_projects' \
  169. 'pri*ority:priority:$task_priorities' \
  170. 'du*e:Due date:$task_dates' \
  171. 're*cur:Recurrence frequency:$task_freqs' \
  172. 'un*til:Expiration date:$task_dates' \
  173. 'li*mit:Desired number of rows in report' \
  174. 'wa*it:Date until task becomes pending:$task_dates' \
  175. 'ent*ry:Date task was created:$task_dates' \
  176. 'end:Date task was completed/deleted:$task_dates' \
  177. 'st*art:Date task was started:$task_dates' \
  178. 'sc*heduled:Date task is scheduled to start:$task_dates' \
  179. 'dep*ends:Other tasks that this task depends upon:$task_zshids'
  180. task_attributes=("$reply[@]")
  181. args=(
  182. \( "$task_attributes[@]" \|
  183. \( /'(project|description|status|entry|end|start|scheduled|depends|due|wait|recur|priority|until|limit).'/- \( /$'[^:]#:'/ ":default:modifiers:compadd -S ':' -a _task_modifiers" \) \) \|
  184. \( /'(rc).'/- \( /$'[^:]#:'/ ":arguments:config:compadd -S ':' -a _task_config" \) \) \|
  185. \( /'(+|-)'/- \( /"$word"/ ":values:remove tag:compadd -a _task_tags" \) \) \|
  186. \( /"$word"/ \)
  187. \) \#
  188. )
  189. _regex_arguments _task_attributes "${args[@]}"
  190. ## task commands
  191. # filter completion
  192. (( $+functions[_task_filter] )) ||
  193. _task_filter() {
  194. _task_attributes "$@"
  195. # TODO complete conjunctions only if the previous word is a filter expression, i.e. attribute, ID, any non-command
  196. _describe -t default 'task conjunctions' _task_conjunctions
  197. }
  198. # execute completion
  199. (( $+functions[_task_execute] )) ||
  200. _task_execute() {
  201. _files
  202. }
  203. # id-only completion
  204. (( $+functions[_task_id] )) ||
  205. _task_id() {
  206. _describe -t values 'task IDs' _task_zshids
  207. }
  208. # subcommand-only function
  209. (( $+functions[_task_subcommands] )) ||
  210. _task_subcommands() {
  211. local -a subcommands
  212. local _zshcmd
  213. local cmd category desc
  214. local lastcategory=''
  215. # The list is sorted by category, in the right order.
  216. for _zshcmd in "$_task_zshcmds[@]"; do
  217. # Parse out the three fields
  218. cmd=${_zshcmd%%:*}
  219. category=${${_zshcmd#*:}%%:*}
  220. desc=${_zshcmd#*:*:}
  221. # Present each category as soon as the first entry in the *next* category
  222. # is seen.
  223. if [[ $category != $lastcategory && -n $lastcategory ]]; then
  224. _describe -t ${lastcategory}-commands "task ${lastcategory} command" subcommands
  225. subcommands=()
  226. fi
  227. # Log the subcommand; we will process it in some future iteration.
  228. subcommands+=( "$cmd:$desc" )
  229. lastcategory=$category
  230. done
  231. }
  232. ## first level completion => task sub-command completion
  233. (( $+functions[_task_default] )) ||
  234. _task_default() {
  235. local cmd ret=1
  236. integer i=1
  237. while (( i < $#words ))
  238. do
  239. cmd="${_task_cmds[(r)$words[$i]]}"
  240. if (( $#cmd )); then
  241. _call_function ret _task_${cmd} ||
  242. _call_function ret _task_filter ||
  243. _message "No command remaining."
  244. return ret
  245. fi
  246. (( i++ ))
  247. done
  248. # update IDs
  249. _task_zshids=( ${(f)"$(task _zshids)"} )
  250. _task_subcommands
  251. _describe -t tasks 'task IDs' _task_zshids
  252. _describe -t aliases 'task aliases' _task_aliases
  253. _call_function ret _task_filter
  254. return ret
  255. }
  256. _task "$@"