File: todo_completion

package info (click to toggle)
todotxt-cli 2.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,312 kB
  • sloc: sh: 5,393; makefile: 67
file content (113 lines) | stat: -rwxr-xr-x 5,520 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash source-this-script
[ "$BASH_VERSION" ] || return

_todo()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    local -r OPTS="-@ -@@ -+ -++ -d -f -h -p -P -PP -a -n -t -v -vv -V -x"
    local -r COMMANDS="\
        add a addto addm append app archive command del  \
        rm depri dp do help list ls listaddons listall lsa listcon  \
        lsc listfile lf listpri lsp listproj lsprj move \
        mv prepend prep pri p replace report shorthelp"
    local -r MOVE_COMMAND_PATTERN='^(move|mv)$'

    local _todo_sh=${_todo_sh:-todo-txt}
    local completions
    if [ $COMP_CWORD -eq 1 ]; then
        completions="$COMMANDS $(eval TODOTXT_VERBOSE=0 $_todo_sh command listaddons 2>/dev/null) $OPTS"
    elif [[ $COMP_CWORD -gt 2 && ( \
        "${COMP_WORDS[COMP_CWORD-2]}" =~ $MOVE_COMMAND_PATTERN || \
        "${COMP_WORDS[COMP_CWORD-3]}" =~ $MOVE_COMMAND_PATTERN ) ]]; then
        # "move ITEM# DEST [SRC]" has file arguments on positions 2 and 3.
        completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listfile 2>/dev/null)
    else
        case "$prev" in
            command)
                completions=$COMMANDS;;
            help)
                completions="$COMMANDS $(eval TODOTXT_VERBOSE=0 $_todo_sh command listaddons 2>/dev/null)";;
            addto|listfile|lf)
                completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listfile 2>/dev/null);;
            -*) completions="$COMMANDS $(eval TODOTXT_VERBOSE=0 $_todo_sh command listaddons 2>/dev/null) $OPTS";;
            *)  case "$cur" in
                    +*) completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listproj 2>/dev/null)
                        COMPREPLY=( $( compgen -W "$completions" -- $cur ))
                        [ ${#COMPREPLY[@]} -gt 0 ] && return 0
                        # Fall back to projects extracted from done tasks.
                        completions=$(eval 'TODOTXT_VERBOSE=0 TODOTXT_SOURCEVAR=\$DONE_FILE' $_todo_sh command listproj 2>/dev/null)
                        ;;
                    @*) completions=$(eval TODOTXT_VERBOSE=0 $_todo_sh command listcon 2>/dev/null)
                        COMPREPLY=( $( compgen -W "$completions" -- $cur ))
                        [ ${#COMPREPLY[@]} -gt 0 ] && return 0
                        # Fall back to contexts extracted from done tasks.
                        completions=$(eval 'TODOTXT_VERBOSE=0 TODOTXT_SOURCEVAR=\$DONE_FILE' $_todo_sh command listcon 2>/dev/null)
                        ;;
                    *)  if [[ "$cur" =~ ^[0-9]+$ ]]; then
                            # Remove the (padded) task number; we prepend the
                            # user-provided $cur instead.
                            # Remove the timestamp prepended by the -t option,
                            # and the done date (for done tasks); there's no
                            # todo.txt option for that yet.
                            # But keep priority and "x"; they're short and may
                            # provide useful context.
                            # Remove any trailing whitespace; the Bash
                            # completion inserts a trailing space itself.
                            # Finally, limit the output to a single line just as
                            # a safety check of the ls action output.
                            local todo=$( \
                                eval TODOTXT_VERBOSE=0 $_todo_sh '-@ -+ -p -x command ls "^ *${cur} "' 2>/dev/null | \
                                sed -e 's/^ *[0-9]\{1,\} //' -e 's/^\((.) \)\{0,1\}[0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} /\1/' \
                                    -e 's/^\([xX] \)\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{1,2\}/\1/' \
                                    -e 's/[[:space:]]*$//' \
                                    -e '1q' \
                            )
                            # Append task text as a shell comment. This
                            # completion can be a safety check before a
                            # destructive todo.txt operation.
                            [ "$todo" ] && COMPREPLY[0]="$cur # $todo"
                            return 0
                        else
                            return 0
                        fi
                        ;;
                esac
                ;;
        esac
    fi

    COMPREPLY=( $( compgen -W "$completions" -- $cur ))
    return 0
}
complete -F _todo todo-txt

# If you define an alias (e.g. "t") to todo-txt, you need to explicitly enable
# completion for it, too:
#complete -F _todo t
# It is recommended to put this line next to your alias definition in your
# ~/.bashrc (or wherever else you're defining your alias). If you simply
# uncomment it here, you will need to redo this on every todo.txt update!

# If you have renamed the todo-txt executable, or if it is not accessible through
# PATH, you need to add and use a wrapper completion function, like this:
#_todoElsewhere()
#{
#    local _todo_sh='/path/to/todo2.sh'
#    _todo "$@"
#}
#complete -F _todoElsewhere /path/to/todo2.sh

# If you use aliases to use different configuration(s), you need to add and use
# a wrapper completion function for each configuration if you want to complete
# from the actual configured task locations:
#alias todo2='todo-txt -d "$HOME/todo2.cfg"'
#_todo2()
#{
#    local _todo_sh='todo-txt -d "$HOME/todo2.cfg"'
#    _todo "$@"
#}
#complete -F _todo2 todo2