"Fossies" - the Fresh Open Source Software Archive

Member "todo.txt_cli-2.12.0/todo_completion" (12 Aug 2020, 6194 Bytes) of package /linux/privat/todo.txt_cli-2.12.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "todo_completion": 2.11.0_vs_2.12.0.

    1 #!/bin/bash source-this-script
    2 [ "$BASH_VERSION" ] || return
    3 
    4 _todo()
    5 {
    6     local cur prev opts
    7     COMPREPLY=()
    8     cur="${COMP_WORDS[COMP_CWORD]}"
    9     prev="${COMP_WORDS[COMP_CWORD-1]}"
   10 
   11     local -r OPTS="-@ -@@ -+ -++ -d -f -h -p -P -PP -a -n -t -v -vv -V -x"
   12     local -r COMMANDS="\
   13         add a addto addm append app archive command del  \
   14         rm depri dp do help list ls listaddons listall lsa listcon  \
   15         lsc listfile lf listpri lsp listproj lsprj move \
   16         mv prepend prep pri p replace report shorthelp"
   17     local -r MOVE_COMMAND_PATTERN='move|mv'
   18 
   19     local _todo_sh=${_todo_sh:-todo.sh}
   20     local completions
   21     if [ $COMP_CWORD -eq 1 ]; then
   22         completions="$COMMANDS $(eval TODOTXT_VERBOSE=0 $_todo_sh command listaddons 2>/dev/null) $OPTS"
   23     elif [[ $COMP_CWORD -gt 2 && ( \
   24         "${COMP_WORDS[COMP_CWORD-2]}" =~ ^($MOVE_COMMAND_PATTERN${_todo_file2_actions:+|${_todo_file2_actions}})$ || \
   25         "${COMP_WORDS[COMP_CWORD-3]}" =~ ^($MOVE_COMMAND_PATTERN${_todo_file3_actions:+|${_todo_file3_actions}})$ ) ]]; then
   26         # "move ITEM# DEST [SRC]" has file arguments on positions 2 and 3.
   27         completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listfile 2>/dev/null)
   28     else
   29         case "$prev" in
   30             command)
   31                 completions=$COMMANDS;;
   32             help)
   33                 completions="$COMMANDS $(eval TODOTXT_VERBOSE=0 $_todo_sh command listaddons 2>/dev/null)";;
   34             -*) completions="$COMMANDS $(eval TODOTXT_VERBOSE=0 $_todo_sh command listaddons 2>/dev/null) $OPTS";;
   35             *)  if [[ "$prev" =~ ^(addto|listfile|lf${_todo_file1_actions:+|${_todo_file1_actions}})$ ]]; then
   36                     completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listfile 2>/dev/null)
   37                 else
   38                     case "$cur" in
   39                         +*) completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listproj 2>/dev/null)
   40                             COMPREPLY=( $( compgen -W "$completions" -- $cur ))
   41                             [ ${#COMPREPLY[@]} -gt 0 ] && return 0
   42                             # Fall back to projects extracted from done tasks.
   43                             completions=$(eval 'TODOTXT_VERBOSE=0 TODOTXT_SOURCEVAR=\$DONE_FILE' $_todo_sh command listproj 2>/dev/null)
   44                             ;;
   45                         @*) completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listcon 2>/dev/null)
   46                             COMPREPLY=( $( compgen -W "$completions" -- $cur ))
   47                             [ ${#COMPREPLY[@]} -gt 0 ] && return 0
   48                             # Fall back to contexts extracted from done tasks.
   49                             completions=$(eval 'TODOTXT_VERBOSE=0 TODOTXT_SOURCEVAR=\$DONE_FILE' $_todo_sh command listcon 2>/dev/null)
   50                             ;;
   51                         *)  if [[ "$cur" =~ ^[0-9]+$ ]]; then
   52                                 declare -a sedTransformations=(
   53                                     # Remove the (padded) task number; we prepend the
   54                                     # user-provided $cur instead.
   55                                     -e 's/^ *[0-9]\{1,\} //'
   56                                     # Remove the timestamp prepended by the -t option,
   57                                     # but keep any priority (as it's short and may
   58                                     # provide useful context).
   59                                     -e 's/^\((.) \)\{0,1\}[0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} /\1/'
   60                                     # Remove the done date and (if there) the timestamp.
   61                                     # Keep the "x" (as it's short and may provide useful
   62                                     # context)
   63                                     -e 's/^\([xX] \)\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{1,2\}/\1/'
   64                                     # Remove any trailing whitespace; the Bash
   65                                     # completion inserts a trailing space itself.
   66                                     -e 's/[[:space:]]*$//'
   67                                     # Finally, limit the output to a single line just as
   68                                     # a safety check of the ls action output.
   69                                     -e '1q'
   70                                 )
   71                                 local todo=$( \
   72                                     eval TODOTXT_VERBOSE=0 $_todo_sh '-@ -+ -p -x command ls "^ *${cur} "' 2>/dev/null | \
   73                                     sed "${sedTransformations[@]}" \
   74                                 )
   75                                 # Append task text as a shell comment. This
   76                                 # completion can be a safety check before a
   77                                 # destructive todo.txt operation.
   78                                 [ "$todo" ] && COMPREPLY[0]="$cur # $todo"
   79                                 return 0
   80                             else
   81                                 return 0
   82                             fi
   83                             ;;
   84                     esac
   85                 fi
   86                 ;;
   87         esac
   88     fi
   89 
   90     COMPREPLY=( $( compgen -W "$completions" -- $cur ))
   91     return 0
   92 }
   93 complete -F _todo todo.sh
   94 
   95 # If you define an alias (e.g. "t") to todo.sh, you need to explicitly enable
   96 # completion for it, too:
   97 #complete -F _todo t
   98 # It is recommended to put this line next to your alias definition in your
   99 # ~/.bashrc (or wherever else you're defining your alias). If you simply
  100 # uncomment it here, you will need to redo this on every todo.txt update!
  101 
  102 # If you have renamed the todo.sh executable, or if it is not accessible through
  103 # PATH, you need to add and use a wrapper completion function, like this:
  104 #_todoElsewhere()
  105 #{
  106 #    local _todo_sh='/path/to/todo2.sh'
  107 #    _todo "$@"
  108 #}
  109 #complete -F _todoElsewhere /path/to/todo2.sh
  110 
  111 # If you use aliases to use different configuration(s), you need to add and use
  112 # a wrapper completion function for each configuration if you want to complete
  113 # from the actual configured task locations:
  114 #alias todo2='todo.sh -d "$HOME/todo2.cfg"'
  115 #_todo2()
  116 #{
  117 #    local _todo_sh='todo.sh -d "$HOME/todo2.cfg"'
  118 #    _todo "$@"
  119 #}
  120 #complete -F _todo2 todo2