-
Notifications
You must be signed in to change notification settings - Fork 8
/
again
executable file
·155 lines (134 loc) · 3.7 KB
/
again
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
#!/usr/bin/env bash
#Copyright (C) 2013 Niklas Thorne.
#
#This file is part of again.
#
#again is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#again is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with again. If not, see <http://www.gnu.org/licenses/>.
source `dirname $(realpath $0 || echo $0)`/againHelpers.sh
readonly SED_DATE_RE="[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}"
readonly BASH_DATE_RE="[0-9]{4}-[0-9]{2}-[0-9]{2}"
readonly AGAIN_TAG="${TODO_AGAIN_TAG:-again}"
function usage()
{
echo " again N"
echo " Mark N as complete and recreate with any due date set as today."
echo " again N ADJUST"
echo " Mark N as complete and recreate with any due date and deferral date"
echo " set as ADJUST from _today_."
echo " again N +ADJUST"
echo " Mark N as complete and recreate with any due date and deferral date"
echo " set as ADJUST from _their original values_."
echo ""
exit
}
function parse_options()
{
local action=$1
shift
[ "$action" = "usage" ] && usage
# Retrieve the item number from the arguments
ITEM=$1
if [[ ! "$ITEM" =~ ^[0-9]+$ ]]
then
error "$ITEM: invalid item number"
exit
fi
shift
ADJUST=$1
}
# Retrieve line number $ITEM from the file $TODO_FILE
function get_line()
{
[ -f "$TODO_FILE" ] || error "$TODO_FILE: no such file"
# Get the line from the todo file
LINE=$(sed "$ITEM!d" "$TODO_FILE")
[ -z "$LINE" ] && error "$ITEM: no such line"
}
# Find $ADJUST in the task
function get_days_from_line()
{
if [[ $LINE == *" $AGAIN_TAG:"* ]]
then
ADJUST=`sed "s/.* $AGAIN_TAG:\([^ ]*\).*/\1/" <<< "$LINE"`
fi
}
# Replace any creation date of the item in $LINE to $TODAY
function replace_creation_date()
{
if [[ "$LINE" =~ ^(\([A-Z]\) )*$BASH_DATE_RE.* ]]
then
LINE=$(echo "$LINE" | sed "s/^\(([A-Z]) \)*\($SED_DATE_RE \)*\(.*\)/\1$TODAY \3/")
fi
}
# Remove any creation date of the item in $LINE
function remove_creation_date()
{
LINE=$(echo "$LINE" | sed "s/^\(([A-Z]) \)*$SED_DATE_RE /\1/g")
}
# Replace any date with $TAG in $LINE by $ADJUST
function replace_tagged_date()
{
if [[ "$LINE" =~ .*$TAG:$BASH_DATE_RE.* ]]
then
if [[ -z $ADJUST ]]
then
# ADJUST not detailed; adjust to today.
NEW_DATE=$TODAY
else
if [[ "$ADJUST" =~ ^\+[0-9]+ ]]
then
# Adjust to ADJUST from original values.
ORIGINAL=$(echo "$LINE" | sed "s/.*$TAG:\($SED_DATE_RE\).*/\1/")
else
# Adjust to ADJUST from today.
ORIGINAL=$TODAY
fi
adjust_date
fi
LINE=$(echo "$LINE" | sed "s/\(.*$TAG:\)\($SED_DATE_RE\)\(.*\)/\1$NEW_DATE\3/")
fi
}
# Replace any due date (due:DATE) of the item in $LINE by $ADJUST
function replace_due_date()
{
TAG=due
replace_tagged_date
}
# Replace any deferral date (t:DATE) of the task in $LINE by $ADJUST
function replace_deferral_date()
{
TAG=t
replace_tagged_date
}
parse_options "$@"
determine_date_version
today
get_line
[ "$ADJUST" == "" ] && get_days_from_line
if [[ "$TODOTXT_DATE_ON_ADD" != 1 ]]
then
replace_creation_date
else
remove_creation_date
fi
replace_due_date
replace_deferral_date
if [[ "$LINE" != "" ]]
then
"$TODO_FULL_SH" command do "$ITEM"
if [[ -z "$TODO_NO_AGAIN_IF_NOT_TAGGED" || "$LINE" =~ .*" $AGAIN_TAG:" ]]
then
"$TODO_FULL_SH" command add "$LINE"
fi
fi