bin/sculd in sculd-0.1.3 vs bin/sculd in sculd-0.2.0

- old
+ new

@@ -3,58 +3,103 @@ # Command 'sculd' can show events and tasks. # Events include schedule and deadline(start and end). # Tasks include deadline, reminder and todo. -#SCULD_DAT = "#{ENV['HOME']}/.sculd.dat" -SCULD_DIR = "#{ENV['HOME']}/.sculd" +SCULD_CONF = "#{ENV['HOME']}/.sculd" require "pp" require "optparse" -require "rubygems" -gem "sculd" require "sculd" +require "yaml" # option analysis -OPTS = {} +OPTIONS = {} op = OptionParser.new -op.on("-e day", "--event days", "Show events."){|v| OPTS[:event] = v.to_i} -op.on("-t num", "--task num" , "Show tasks." ){|v| OPTS[:task ] = v.to_i} -#op.on("-f file", "--file datafile", "Indicate file."){|v| OPTS[:file] = v} +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("-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) -#num_day = ARGV.shift.to_i -#num_task = ARGV.shift.to_i -OPTS[:event] ||= 0 -OPTS[:task ] ||= 0 -if (( OPTS[:event] == 0) &&( OPTS[:task ] == 0)) - puts "USAGE: sculd -e 7 -t 10" +## ~/.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'] -#file = OPTS[:file] -#file ||= SCULD_DAT +## 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 -unless FileTest.exist? SCULD_DIR -# lines = File.open(file, "r").readlines -#rescue - puts "Not found #{SCULD_DIR}. 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) -rescue Sculd::Manager::LoadError + 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 -io = $stdout -sm.show(OPTS[:event], OPTS[:task], io) +sm.show_events(dates) +sm.show_tasks(OPTIONS[:task ]) if OPTIONS[:task] -#while (true) -# s.show(OPTS[:event], OPTS[:task]) -# command = STDIN.gets -# case -# when command == -# end -#end