Complete attributes dynamically incl UDAs

This is the major refinement of the completion: Most of it just moved
the global variables from up above to inside _task_filter so every array
of attributes will be calculated dynamically there.
This commit is contained in:
Doron Behar
2019-05-19 23:02:42 +03:00
committed by Paul Beckingham
parent f04d66e10b
commit e1998346e1

View File

@@ -22,69 +22,22 @@
# SOFTWARE. # SOFTWARE.
# #
# https://www.opensource.org/licenses/mit-license.php # https://www.opensource.org/licenses/mit-license.php
#
# TODO: environment leakage! + these should be used dynamically and not only when this file is loaded # filter completion
typeset -g _task_cmds _task_projects _task_tags _task_config _task_modifiers (( $+functions[_task_filter] )) ||
_task_projects=($(task _projects)) _task_filter() {
_task_tags=($(task _tags))
_task_zshids=( ${(f)"$(task _zshids)"} )
_task_config=($(task _config))
_task_columns=($(task _columns))
# TODO: The 2 following can be static but not leak to environment as well
_task_modifiers=(
'before' \
'after' \
'none' \
'any' \
'is' \
'isnt' \
'has' \
'hasnt' \
'startswith' \
'endswith' \
'word' \
'noword'
)
_task_conjunctions=(
'and' \
'or' \
'xor' \
'\)' \
'\(' \
'<' \
'<=' \
'=' \
'!=' \
'>=' \
'>'
)
# TODO: as before
_task_cmds=($(task _commands; task _aliases))
_task_zshcmds=( ${(f)"$(task _zshcommands)"} sentinel:sentinel:sentinel )
_task_aliases=($(task _aliases)) local -a reply
local word=$'[^\0]#\0'
local -a reply args word # projects
word=$'[^\0]#\0' local _task_projects=($(task _projects))
local task_projects=(
# priorities
local -a task_priorities
_regex_words values 'task priorities' \
'H:High' \
'M:Middle' \
'L:Low'
task_priorities=("$reply[@]")
# projects
local -a task_projects
task_projects=(
/"$word"/ /"$word"/
":values:task projects:compadd -a _task_projects" ":values:task projects:compadd -a _task_projects"
) )
local -a _task_dates _regex_words values 'task dates' \
_regex_words values 'task dates' \
'tod*ay:Today' \ 'tod*ay:Today' \
'yes*terday:Yesterday' \ 'yes*terday:Yesterday' \
'tom*orrow:Tomorrow' \ 'tom*orrow:Tomorrow' \
@@ -116,10 +69,9 @@ _regex_words values 'task dates' \
'midsommarafton:Midsommarafton' \ 'midsommarafton:Midsommarafton' \
'later:Later' \ 'later:Later' \
'someday:Some Day' 'someday:Some Day'
_task_dates=("$reply[@]") local _task_dates=("$reply[@]")
local -a _task_reldates _regex_words values 'task reldates' \
_regex_words values 'task reldates' \
'hrs:n hours' \ 'hrs:n hours' \
'day:n days' \ 'day:n days' \
'1st:first' \ '1st:first' \
@@ -127,21 +79,25 @@ _regex_words values 'task reldates' \
'3rd:third' \ '3rd:third' \
'th:4th, 5th, etc.' \ 'th:4th, 5th, etc.' \
'wks:weeks' 'wks:weeks'
_task_reldates=("$reply[@]") local _task_reldates=("$reply[@]")
task_dates=( local task_dates=(
\( "$_task_dates[@]" \| \( "$_task_dates[@]" \|
\( /$'[0-9][0-9]#'/- \( "$_task_reldates[@]" \) \) \( /$'[0-9][0-9]#'/- \( "$_task_reldates[@]" \) \)
\) \)
) )
local -a task_zshids # This data is a little bit differnt, it may contain any kind of character so
if (( $#_task_zshids )); then # we parse it manually and quote using '${(q)_desc}' the descriptions
local -a _task_zshids
local _id desc
task _zshids | while IFS=':' read _id desc; do
_task_zshids+=("$_id:${(qq)desc}")
done
_regex_words values 'task IDs' $_task_zshids _regex_words values 'task IDs' $_task_zshids
task_zshids=("$reply[@]") local task_zshids=("$reply[@]")
fi
_regex_words values 'task frequencies' \ _regex_words values 'task frequencies' \
'daily:Every day' \ 'daily:Every day' \
'day:Every day' \ 'day:Every day' \
'weekdays:Every day skipping weekend days' \ 'weekdays:Every day skipping weekend days' \
@@ -155,74 +111,116 @@ _regex_words values 'task frequencies' \
'yearly:Every year' \ 'yearly:Every year' \
'biannual:Every two years' \ 'biannual:Every two years' \
'biyearly:Every two years' 'biyearly:Every two years'
_task_freqs=("$reply[@]") local _task_freqs=("$reply[@]")
local -a _task_frequencies _regex_words values 'task frequencies' \
_regex_words values 'task frequencies' \
'd:days' \ 'd:days' \
'w:weeks' \ 'w:weeks' \
'q:quarters' \ 'q:quarters' \
'y:years' 'y:years'
_task_frequencies=("$reply[@]") local _task_frequencies=("$reply[@]")
# TODO: environment leakage! + values not calculated dynamically local task_freqs=(
task_freqs=(
\( "$_task_freqs[@]" \| \( "$_task_freqs[@]" \|
\( /$'[0-9][0-9]#'/- \( "$_task_frequencies[@]" \) \) \( /$'[0-9][0-9]#'/- \( "$_task_frequencies[@]" \) \)
\) \)
) )
# attributes # task statuses
# TODO: This should include UDAs as well local _task_statuses=(
local -a task_attributes pending
_regex_words -t ':' default 'task attributes' \ completed
'des*cription:Task description text' \ deleted
'status:Status of task - pending, completed, deleted, waiting' \ waiting
'pro*ject:Project name:$task_projects' \ )
'pri*ority:priority:$task_priorities' \ _regex_words statuses 'task statuses' \
'du*e:Due date:$task_dates' \ "${_task_statuses}"
're*cur:Recurrence frequency:$task_freqs' \ local task_statuses=("${reply[@]}")
'un*til:Expiration date:$task_dates' \
'li*mit:Desired number of rows in report' \
'wa*it:Date until task becomes pending:$task_dates' \
'ent*ry:Date task was created:$task_dates' \
'end:Date task was completed/deleted:$task_dates' \
'st*art:Date task was started:$task_dates' \
'sc*heduled:Date task is scheduled to start:$task_dates' \
'dep*ends:Other tasks that this task depends upon:$task_zshids'
task_attributes=("$reply[@]")
args=( # attributes
\( "$task_attributes[@]" \| local _task_all_attributes=(
\( /'(project|description|status|entry|end|start|scheduled|depends|due|wait|recur|priority|until|limit).'/- \( /$'[^:]#:'/ ":default:modifiers:compadd -S ':' -a _task_modifiers" \) \) \| 'des*cription:Task description text'
\( /'(rc).'/- \( /$'[^:]#:'/ ":arguments:config:compadd -S ':' -a _task_config" \) \) \| 'status:Status of task:$task_statuses'
\( /'(+|-)'/- \( /"$word"/ ":values:remove tag:compadd -a _task_tags" \) \) \| 'pro*ject:Project name:$task_projects'
\( /"$word"/ \) 'pri*ority:priority:$task_priorities'
'du*e:Due date:$task_dates'
're*cur:Recurrence frequency:$task_freqs'
'un*til:Expiration date:$task_dates'
'li*mit:Desired number of rows in report'
'wa*it:Date until task becomes pending:$task_dates'
'ent*ry:Date task was created:$task_dates'
'end:Date task was completed/deleted:$task_dates'
'st*art:Date task was started:$task_dates'
'sc*heduled:Date task is scheduled to start:$task_dates'
'depends:Other tasks that this task depends upon:$task_zshids'
)
## add UDAs as attributes
local uda_name uda_label uda_values
local -a udas_spec
task _udas | while read uda_name; do
uda_label="$(task _get rc.uda."$uda_name".label)"
# TODO: we could have got the values of every uda and try to complete that
# but that can become extremly slow with a lot of udas
#uda_values=(${(@s:,:)"$(task _get rc.uda."$uda_name".values)"})
udas_spec+=("${uda_name}:${(q)uda_label}")
done
_task_all_attributes=("${_task_all_attributes[@]}" "${udas_spec[@]}")
_regex_words -t ':' default 'task attributes' "${_task_all_attributes[@]}"
local task_attributes=("$reply[@]")
local _task_tags=($(task _tags))
local _task_config=($(task _config))
local _task_modifiers=(
'before'
'after'
'none'
'any'
'is'
'isnt'
'has'
'hasnt'
'startswith'
'endswith'
'word'
'noword'
)
local _task_conjunctions=(
'and'
'or'
'xor'
'\)'
'\('
'<'
'<='
'='
'!='
'>='
'>'
)
_regex_arguments _task_attributes \
\( "${task_attributes[@]}" \| \
\( /'(project|description|status|entry|end|start|scheduled|depends|due|wait|recur|priority|until|limit).'/- \( /$'[^:]#:'/ ":default:modifiers:compadd -S ':' -a _task_modifiers" \) \) \| \
\( /'(rc).'/- \( /$'[^:]#:'/ ":arguments:config:compadd -S ':' -a _task_config" \) \) \| \
\( /'(+|-)'/- \( /"$word"/ ":values:remove tag:compadd -a _task_tags" \) \) \| \
\( /"$word"/ \) \
\) \# \) \#
)
_regex_arguments _task_attributes "${args[@]}"
## task commands
# filter completion
(( $+functions[_task_filter] )) ||
_task_filter() {
_task_attributes "$@" _task_attributes "$@"
# TODO complete conjunctions only if the previous word is a filter expression, i.e. attribute, ID, any non-command # TODO complete conjunctions only if the previous word is a filter expression, i.e. attribute, ID, any non-command
_describe -t default 'task conjunctions' _task_conjunctions _describe -t default 'task conjunctions' _task_conjunctions
} }
# execute completion
(( $+functions[_task_execute] )) ||
_task_execute() {
_files
}
# id-only completion # id-only completion
(( $+functions[_task_id] )) || (( $+functions[_task_ids] )) ||
_task_id() { _task_ids() {
_describe -t values 'task IDs' _task_zshids local _ids=( ${(f)"$(task _zshids)"} )
_describe 'task ids' _ids
}
(( $+functions[_task_aliases] )) ||
_task_aliases() {
local _aliases=( ${(f)"$(task _aliases)"} )
_describe 'task aliases' _aliases
} }
# subcommand-only function # subcommand-only function
@@ -233,6 +231,7 @@ _task_subcommands() {
local cmd category desc local cmd category desc
local lastcategory='' local lastcategory=''
# The list is sorted by category, in the right order. # The list is sorted by category, in the right order.
local _task_zshcmds=( ${(f)"$(task _zshcommands)"} sentinel:sentinel:sentinel )
for _zshcmd in "$_task_zshcmds[@]"; do for _zshcmd in "$_task_zshcmds[@]"; do
# Parse out the three fields # Parse out the three fields
cmd=${_zshcmd%%:*} cmd=${_zshcmd%%:*}
@@ -253,17 +252,28 @@ _task_subcommands() {
done done
} }
## contexts
(( $+functions[_task_context] )) ||
_task_context() {
local _contexts=(${(f)"$(task _context)"})
_describe 'task contexts' _contexts
}
## first level completion => task sub-command completion ## first level completion => task sub-command completion
(( $+functions[_task_default] )) || (( $+functions[_task_default] )) ||
_task_default() { _task_default() {
local cmd ret=1 local cmd ret=1
integer i=1 integer i=1
local _task_cmds=($(task _commands; task _aliases))
while (( i < $#words )) while (( i < $#words ))
do do
cmd="${_task_cmds[(r)$words[$i]]}" cmd="${_task_cmds[(r)$words[$i]]}"
if (( $#cmd )); then if (( $#cmd )); then
_call_function ret _task_${cmd} || if (( $+functions[_task_${cmd}] )); then
_task_${cmd}
return ret
fi
_call_function ret _task_filter || _call_function ret _task_filter ||
_message "No command remaining." _message "No command remaining."
return ret return ret
@@ -271,12 +281,9 @@ _task_default() {
(( i++ )) (( i++ ))
done done
# update IDs
_task_zshids=( ${(f)"$(task _zshids)"} )
_task_subcommands _task_subcommands
_describe -t tasks 'task IDs' _task_zshids # _task_ids
_describe -t aliases 'task aliases' _task_aliases _task_aliases
_call_function ret _task_filter _call_function ret _task_filter
return ret return ret