Created
August 4, 2012 21:01
-
-
Save statonjr/3259910 to your computer and use it in GitHub Desktop.
Log to Day One
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# logtodayone.rb | |
# Brett Terpstra (http://brettterpstra.com) | |
# Larry Staton Jr. (https://github.com/statonjr) | |
# 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 'logger' | |
# Set up logging | |
log = Logger.new(File.expand_path("~/Library/Logs/logtodayone.log")) | |
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 | |
begin | |
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 | |
require 'chronic' | |
d = Chronic.parse(datestring, {:context => :past, :ambiguous_time_range => 8}) | |
d = Time.now if d.nil? | |
else | |
# if no [date] specified, make it right now | |
d = Time.now | |
end | |
rescue Exception => e | |
log.fatal "Houston, we have a problem: #{e}" | |
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} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment