-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogme2DO.rb
executable file
·53 lines (47 loc) · 1.68 KB
/
logme2DO.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
#!/usr/bin/ruby
# logtodayone.rb
# Brett Terpstra (http://brettterpstra.com)
# Use and modify freely, attribution appreciated
#
# This script works with the Day One[1] command line utility
# It parses an input string for an exclamation point prefix to mark starred
# and/or a [date string] at the beginning to parse natural language dates
#
# Requirements:
# Chronic ruby gem
#
# Example usage:
# logtodayone.rb "! This is a starred entry."
# logtodayone.rb "[yesterday 3pm] Something I did yesterday at 3:00PM"
# logtodayone.rb "! [-2 1:30am] A starred entry about something I did two days ago"
require 'rubygems'
require 'chronic' # (`gem install chronic`)
if ARGV.length > 0
input = ARGV.join(" ").strip
else
print "Log entry: "
input = gets.strip
end
# If the input starts with an exclamation point, make it starred
starred = input =~ /^!/ ? "true" : "false"
# remove the bang from the input string
input = input.gsub(/^!\s*/,'')
# if there's a [date] specified, parse it
if input =~ /^\[(.*?)\]/
datestring = $1
# if the date starts with -X, assume it means X days ago
if datestring =~ /^\-(\d+)/
datestring.sub!(/\-(\d+)/,"\\1 days ago ")
end
# Replace a single 'y' within the date brackets with "Yesterday" for parsing
datestring.sub!(/\by\b/,'yesterday')
# Parse the resulting date string with Chronic
d = Chronic.parse(datestring, {:context => :past, :ambiguous_time_range => 8})
d = DateTime.now if d.nil?
else
# if no [date] specified, make it right now
d = DateTime.now
end
date = d.strftime("%m/%d/%Y %l:%M%p") # dayone formatted
input = input.gsub(/^\[.*?\]\s*/,'') # remove [date] from input
%x{echo "#{input}"|/usr/local/bin/dayone -d="#{date}" -s=#{starred} new}