forked from thoughtafter/vpim
/
reminder.rb
executable file
·209 lines (168 loc) · 4.11 KB
/
reminder.rb
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
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env ruby
$-w = true
require 'ubygems' rescue "ignored"
require 'getoptlong'
require 'pp'
require 'plist'
require 'vpim/repo'
$stdout.sync = true
$stderr.sync = true
HELP =<<EOF
Usage: #{$0} [where]
Shows events and todos occuring soon.
By default, the Apple iCal v3 calendars are used, but if a location where .ics
files can be found is specified, any calendars found there will be used.
Options
-h,--help Print this helpful message.
-n,--days N How many of the next days are considered to be "soon", default
is seven.
-v,--verbose Print more information about upcoming events.
EOF
opt_debug = nil
opt_dump = nil
opt_verbose = nil
opt_days = 7
opts = GetoptLong.new(
[ "--help", "-h", GetoptLong::NO_ARGUMENT ],
[ "--days", "-n", GetoptLong::REQUIRED_ARGUMENT ],
[ "--verbose", "-v", GetoptLong::NO_ARGUMENT ],
[ "--debug", "-d", GetoptLong::NO_ARGUMENT ]
)
opts.each do |opt, arg|
case opt
when "--help" then
puts HELP
exit 0
when "--days" then
opt_days = arg.to_i
when "--verbose" then
opt_verbose = true
when "--debug" then
opt_verbose = true
if opt_debug
opt_dump = true
end
opt_debug = true
end
end
calendars = []
if ARGV.length > 0
Vpim::Repo::Directory.each(ARGV.first) do |cal|
calendars << cal
end
else
Vpim::Repo::Apple3.new.each() do |cal|
calendars << cal
end
end
if opt_dump
pp ARGV
pp calendars
end
SECSPERDAY = (24 * 60 * 60)
t0 = Time.new.to_a
t0[0] = t0[1] = t0[2] = 0 # sec,min,hour = 0
t0 = Time.local(*t0)
t1 = t0 + opt_days * SECSPERDAY
if opt_dump
puts "to: #{t0}"
puts "t1: #{t1}"
end
if opt_verbose
puts "Events in the next #{opt_days} days:"
end
# Collect all events, then all todos.
all_events = []
all_todos = []
calendars.each do |cal|
if opt_debug; puts "Calendar: #{cal.name}"; end
# TODO - mv collection algorithm to library
begin
cal.events.each do |e|
begin
if opt_dump; pp e; end
if e.occurs_in?(t0, t1)
if e.summary
all_events.push(e)
end
end
rescue
$stderr.puts "error in #{cal.name} (\"#{e.summary}\"): #{$!.to_s}"
end
end
all_todos.concat(cal.todos.to_a)
end
end
puts
# TODO - mv sorting algorithm to library
def start_of_first_occurrence(t0, t1, e)
e.occurrences(t1) do |t|
# An event might start before t0, but end after it..., in which case
# we are still interested.
if (t + (e.duration || 0)) >= t0
return t
end
end
nil
end
all_events.sort! do |lhs, rhs|
start_of_first_occurrence(t0, t1, lhs) <=> start_of_first_occurrence(t0, t1, rhs)
end
all_events.each do |e|
puts "#{e.summary}:"
if opt_verbose
if e.description; puts " description=#{e.description}"; end
if e.comments.any?; puts " comment=#{e.comments.first}"; end
if e.location; puts " location=#{e.location}"; end
if e.status; puts " status=#{e.status}"; end
if e.dtstart; puts " dtstart=#{e.dtstart}"; end
if e.duration; puts " duration=#{Vpim::Duration.new(e.duration).to_s}"; end
end
i = 1
e.occurrences(t1) do |t|
# An event might start before t0, but end after it..., in which case
# we are still interested.
dstr = ''
if e.duration
dstr = " for #{Vpim::Duration.new(e.duration).to_s}"
end
# TODO - mv to library, as variant of occurs_in?
if (t + (e.duration || 0)) >= t0
puts " ##{i} on #{t}#{dstr}"
i += 1
end
end
end
=begin
def fix_priority(vtodo)
p = vtodo.priority
if !p
p = 10
end
=end
all_todos.sort! do |x,y|
x = x.priority
y = y.priority
# 0 means no priority, put these last, not first
x = 10 if x == 0
y = 10 if y == 0
x <=> y
end
priorities = [
'no importance',
'very important',
'very important',
'very important',
'important',
'important',
'important',
'not important',
'not important',
'not important'
]
all_todos.each do |e|
status = e.status || 'Todo'
if status != 'COMPLETED'
puts "#{status.capitalize}: #{e.summary}" # (#{priorities[e.priority]})"
end
end