-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert
executable file
·114 lines (88 loc) · 2.16 KB
/
alert
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
#!/usr/bin/env ruby -w
require 'time'
# Usage:
#
# alert
# alert yellow
# alert at 17:00
# alert red at 11:29
# alert green to "Green tea has finished brewing" in 2:45
#
# If no time is specified, alert immediately.
# If no color is specified, default to white.
# TODO:
# - Add an actual help message.
# - Move Alert to its own class.
# Check whether any blink(1)s are attached, by checking the exit status of the
# blink1-tool `--list` command.
def blink1_present?
%x(blink1-tool --list)
$?.success?
end
# Take a duration of the form
#
# [<hours>:][<minutes>:]seconds
#
# e.g. '2:30', '15:00', '1:30:00', etc.
#
# Return the equivalent number of seconds.
#
# TODO: Add support for "5m", "2m3s", etc.
def parse_delay(delay)
values = delay.split(':').map(&:to_i)
total = 0
offset = 1
until values.empty? do
value = values.pop
total += value * offset
offset *= 60
end
total
end
def parse_args
time = 0
message = ""
color = 'white'
until ARGV.empty? do
arg = ARGV.shift
now = Time.now
if arg.eql?('at')
later = Time.parse(ARGV.shift)
# If the specified time was earlier today, push it to tomorrow,
# and alert the user that we're doing so.
if now > later
later += (24 * 3600)
puts "Alert set for #{later.strftime('%H:%M:%S')} tomorrow."
end
time = later - now
elsif arg.eql?('in')
time = parse_delay(ARGV.shift)
elsif arg.eql?('to')
message = ARGV.shift
else
color = arg
end
end
[time, message, color]
end
# ------------------
abort 'No blink(1) detected!' unless blink1_present?
time, message, color = parse_args
# fork() won't work on Windows or NetBSD. (File under "Boo frickin' hoo".)
fork do
actions = [
%Q(blink1-tool -q -m 0 -t 250 --#{color} --blink 10)
]
unless message.empty?
actions.concat [
# I didn't pick Daniel because my ego likes the sound of my own name.
# I just liked the sound of the voice.
%Q(say -v Daniel "#{message}"),
%Q(terminal-notifier -title "Alert" -message "#{message}")
]
end
sleep(time)
actions.each do |action|
fork { system(action) }
end
end