File: timew-completion.bash

package info (click to toggle)
timew 1.9.1%2Bds.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,436 kB
  • sloc: cpp: 30,358; python: 6,829; sh: 706; makefile: 15
file content (195 lines) | stat: -rw-r--r-- 3,808 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Bash completion for Timewarrior
#
# Copyright (C) 2017 - 2020, 2023, Thomas Lauf
#
function __get_commands()
{
  echo "annotate cancel config continue day delete diagnostics export extensions gaps get help join lengthen modify month move report resize retag shorten show split start stop summary tag tags track undo untag week"
}

function __get_subcommands()
{
  case "${1}" in
    modify)
      echo -e "end start"
      ;;
    *)
      echo ""
      ;;
  esac
}

function __get_help_items()
{
  echo -e "$( __get_commands ) interval hints date duration dom"
}

function __get_options()
{
  echo -e "--help --verbose --version"
}

function __get_ids()
{
  local count
  count="$( timew get dom.tracked.count )"
  if [[ "${count}" -eq "0" ]] ; then
    echo ""
  else
    seq -f "@%g" 1 "${count}"
  fi
}

function __get_tags()
{
  tags=$(timew get dom.tracked.tags "${TIMEW_COMPLETION_TAGS_RANGE:-":all"}")
  # return each tag on its own line
  while [[ "${tags}" ]]; do
    if [[ "${tags}" =~ ^\"[^\"]*\" ]]; then
      tag="${BASH_REMATCH[0]:1:-1}"
      tags="${tags#\"${tag}\"}"
    else
      tag="${tags%% *}"
      tags="${tags#$tag}"
    fi
    echo "${tag}"
    tags="${tags# }"
  done
}

function __get_extensions()
{
  timew extensions | awk '{if(NR>6)print $1}'
}

function __has_entered_id()
{
  for word in "${COMP_WORDS[@]}" ; do
    if [[ "${word}" =~ ^@[0-9] ]] ; then
      return 0
    fi
  done

  return 1
}

function __has_entered_subcommand()
{
  local subcommands
  subcommands=$( __get_subcommands "${1}" )

  for word in "${COMP_WORDS[@]}" ; do
    for cmd in ${subcommands} ; do
      if [[ "${word}" == "${cmd}" ]] ; then
        return 0
      fi
    done
  done

  return 1
}

function __has_entered_help_item()
{
  local items
  items=$( __get_help_items )

  for word in "${COMP_WORDS[@]:2}" ; do
    for item in ${items} ; do
      if [[ "${word}" == "${item}" ]] ; then
        return 0
      fi
    done
  done

  return 1
}

function __is_entering_id()
{
  if [[ "${COMP_WORDS[COMP_CWORD]}" =~ @[0-9]* ]] ; then
    return 0
  else
    return 1
  fi
}

function __complete_tag()
{
  COMPREPLY=()
  cur="${COMP_WORDS[COMP_CWORD]}"
  local line wordlist

  declare -a wordlist
  while IFS=$'\n' read -r line ; do
    wordlist+=( "${line}" )
  done <<< "$( __get_tags )"

  declare -a completions
  while read -r line ; do
    completions+=( "${line}" )
  done < <( compgen -W "$(printf '%q ' "${wordlist[@]}")" -- "${cur}" 2>/dev/null )

  for completion in "${completions[@]}" ; do
    COMPREPLY+=( "$(printf "%q" "${completion}")" )
  done
}

function _timew()
{
  local cur first wordlist

  COMPREPLY=()
  cur="${COMP_WORDS[COMP_CWORD]}"
  first="${COMP_WORDS[1]}"

  case "${first}" in
    cancel|config|diagnostics|day|extensions|get|month|show|undo|week)
      wordlist=""
      ;;
    annotate|continue|delete|join|lengthen|move|resize|shorten|split)
      wordlist=$( __get_ids )
      ;;
    export|gaps|start|stop|summary|tags|track)
      __complete_tag
      return
      ;;
    modify)
      if __has_entered_subcommand "${first}" ; then
        wordlist=$( __get_ids )
      else
        wordlist=$( __get_subcommands "${first}" )
      fi
      ;;
    tag|untag)
      if __is_entering_id ; then
        wordlist=$( __get_ids )
      elif __has_entered_id ; then
        __complete_tag
        return
      else
        wordlist=$( __get_ids )
      fi
      ;;
    report)
      wordlist=$( __get_extensions )
      ;;
    help)
      if __has_entered_help_item ; then
        wordlist=""
      else
        wordlist=$( __get_help_items )
      fi
      ;;
    -*)
      wordlist=$( __get_options )
      ;;
    *)
      wordlist=$( __get_commands )
      ;;
  esac

  COMPREPLY=( $( compgen -W "${wordlist}" -- "${cur}" ) )
}

complete -F _timew timew