-
Notifications
You must be signed in to change notification settings - Fork 4
/
addto
executable file
·82 lines (74 loc) · 2.66 KB
/
addto
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
#!/bin/bash
# Modifies the behavior of the addto command in the following ways:
# - If the file doesn't exist, but it is in the form YYYY-MM-DD, then create
# the file before running the original addto.
# - Automatically adds .txt to the end of the filename
# - Allow 'tomorrow' as a filename
#
# Copyright (C) 2013 Mark Harrison
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
ACTION=$1
if [[ $ACTION == "usage" ]]; then
echo ' addto DEST "TEXT TO ADD"'
echo " Adds a line of text to a different todo file"
exit
fi
shift
FILE=$1
shift
# Linux uses GNU date, while OS X uses BSD date.
_get_tomorrow_file () {
DAY=$(date +%w)
if [[ "$OSTYPE" == "linux-gnu" ]]; then
if [[ $DAY == "5" ]]; then
# Friday, skip to next Monday
MODIFIER="next monday"
else
MODIFIER="tomorrow"
fi
FILE=$(date --date="$MODIFIER" +%Y-%m-%d)
elif [[ "$OSTYPE" == "darwin"* ]]; then
if [[ $DAY == "5" ]]; then
# Friday, skip to next Monday
MODIFIER="+Mon"
else
MODIFIER="+1d"
fi
FILE=$(date -v$MODIFIER +%Y-%m-%d)
else
# Unknown.
echo "Only Linux and OS X are supported currently";
exit 1;
fi
}
# Deal with 'tomorrow' as a filename
if [[ $FILE == 'tomorrow' ]]; then
# Set $FILE
_get_tomorrow_file
fi
# Make sure the filename has a .txt extension on the end
FILE=${FILE/.txt}.txt
if [[ ! -f $TODO_DIR/$FILE ]]; then
if [[ $FILE =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}\.txt$ ]]; then
# File looks like a date, create it
touch $TODO_DIR/$FILE
fi
fi
$TODO_FULL_SH command addto $FILE "$@"