#! /usr/bin/env ruby # coding: utf-8 # Command 'sculd' can show events and tasks. # Events include schedule and deadline(start and end). # Tasks include deadline, reminder and todo. SCULD_CONF = "#{ENV['HOME']}/.sculd" require "pp" require "optparse" require "sculd" require "yaml" # option analysis OPTIONS = {} op = OptionParser.new op.on("-a day", "--after days" , "Event after n days."){|v| OPTIONS[:after] = v.to_i} op.on("-b day", "--before days", "Event before n days."){|v| OPTIONS[:before] = v.to_i} op.on("-c day", "--center days", "Event days between n days."){|v| OPTIONS[:center] = v.to_i} op.on("-t date", "--till days", "Event till date."){|v| OPTIONS[:till] = v} op.on("-w weekdays", "--weekdays=wdays", "Event days only on weekdays. E.g., --weekday='Sun,Mon'"){|v| OPTIONS[:weekdays] = v.split ','} op.on("-t num", "--task num" , "Show tasks on today." ){|v| OPTIONS[:task ] = v.to_i} op.parse!(ARGV) ## ~/.sculd check begin configurations = YAML.load_file SCULD_CONF rescue puts "'sculd' command requires ~/.sculd file with YAML format; not directory." puts "key as sculd_dir and value as the path to directory should be indicated in ~/.sculd." exit end sculd_dir = configurations['sculd_dir'] ## dates if ARGV.empty? dates = [Date.today] else dates = [] ARGV.each do |v| begin date = (Date.parse v) rescue ArgumentError next end dates << date end end if OPTIONS[:till] Date.today.upto(Date.parse(OPTIONS[:till])) do |d| dates << d end end #pp dates; exit if OPTIONS[:center] OPTIONS[:after] ||= OPTIONS[:center] OPTIONS[:before] ||= OPTIONS[:center] end if OPTIONS[:after] results = [] dates.each do |date| 0.upto OPTIONS[:after] do |i| results << date + i end end dates = results end if OPTIONS[:before] results = [] dates.each do |date| 0.upto OPTIONS[:before] do |i| results << date - i end end dates = results end if OPTIONS[:weekdays] weekdays = OPTIONS[:weekdays].map {|v| Sculd::Plan.wday v} dates.select!{|date| weekdays.include? date.wday} end #pp dates dates = dates.sort.uniq unless FileTest.exist? sculd_dir puts "Not found #{sculd_dir}. Exit." exit end begin sm = Sculd::Manager.new(sculd_dir) sm.set_weekdays configurations['weekdays'] if configurations['weekdays'] rescue Sculd::Manager::LoadError => message puts message puts "Exit." exit end sm.show_events(dates) sm.show_tasks(OPTIONS[:task ]) if OPTIONS[:task]