Skip to content

Instantly share code, notes, and snippets.

@Apexal
Created February 16, 2016 01:14
Show Gist options
  • Save Apexal/84f5b77c9586a1436109 to your computer and use it in GitHub Desktop.
Save Apexal/84f5b77c9586a1436109 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "date"
require 'fileutils'
require 'yaml'
VERSION = "1.0"
NOW = Date.today
@root = Dir.home+"/Recollect"
def init
# Create directory(s)
if File.file?("#{Dir.home}/.recollect") == false
file = File.open("#{Dir.home}/.recollect", "w")
file.puts "settings:"
file.puts " path: #{Dir.home}/Recollect"
file.puts " format: md"
file.close
end
begin
config = YAML::load_file("#{Dir.home}/.recollect")
@root = config["settings"]["path"]
rescue
puts "Invalid config file at #{Dir.home}/.recollect. It was reset."
file = File.open("#{Dir.home}/.recollect", "w")
file.puts "settings:"
file.puts " path: #{Dir.home}/Recollect"
file.puts " format: md"
file.close
config = YAML::load_file("#{Dir.home}/.recollect")
@root = config["settings"]["path"]
end
FileUtils::mkdir_p @root
end
def decide args
date = nil
full = (args.join " ").strip.downcase
if args.length == 1
if full == "yesterday"
date = (NOW - 1)
elsif full == "today"
date = NOW
else
date = Date.parse args[0]
if date > NOW
raise "Can't pass future date."
end
end
elsif args.length == 3 && (args[0].to_i.to_s == args[0]) && args[1] == "days" && args[2] == "ago"
date = (NOW - args[0].to_i)
else
raise "Invalid arguments."
end
return date
end
def recall date
dir = "#{@root}/#{date.year}/#{date.strftime('%B')}"
FileUtils::mkdir_p dir
path = "#{dir}/#{date.strftime('%d')}.md"
if File.file?(path) == false
file = File.open(path, "w")
file.puts "# #{date.strftime('%A, %B %d %Y')} #\n"
file.close
end
system "nano", path
end
def main
begin
init()
date = decide (if ARGV.length != 0 then ARGV else ["today"] end)
recall date
rescue Exception => e
puts e
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment