lib/time_overlap/presenter.rb in time_overlap-0.1.2 vs lib/time_overlap/presenter.rb in time_overlap-0.2.0

- old
+ new

@@ -1,16 +1,22 @@ require 'colorize' +require 'terminal-table' module TimeOverlap class Presenter - AM = "AM ".freeze - PM = " PM".freeze + AM = "AM " + PM = " PM" NOON = " 12:00 " SIX_AM = " 6:00 " SIX_PM = " 6:00 " + EARLY_BIRD = 'Early Bird' + NIGHT_OWL = 'Night Owl' + + WIDTH = 102 + AVAILABLE_SLOT = "|█| " EMPTY_SLOT = "[ ] " def initialize(data, format = '%T%:z') @data = data @@ -20,38 +26,96 @@ def self.generate_output(*args) new(*args).generate_output end def generate_output - puts "Original:" - puts "#{formated_time(@data[:original][:start], false)} - #{formated_time(@data[:original][:end])}".green - timeline(@data[:original][:start], @data[:original][:end]) - puts "Full overlap:" - puts "#{formated_time(@data[:full_overlap][:start], false)} - #{formated_time(@data[:full_overlap][:end])}".green - timeline(@data[:full_overlap][:start], @data[:full_overlap][:end]) - puts "Overlap 1:" - puts "#{formated_time(@data[:overlap_1][:start], false)} - #{formated_time(@data[:overlap_1][:end])}".green - timeline(@data[:overlap_1][:start], @data[:overlap_1][:end]) + rows = [] - if @data[:overlap_2] - puts "Overlap 2:" - puts "#{formated_time(@data[:overlap_2][:start], false)} - #{formated_time(@data[:overlap_2][:end])}".green - timeline(@data[:overlap_2][:start], @data[:overlap_2][:end]) - end + rows << [base_content] + rows << [min_overlap_content] + rows << [full_overlap_content] + table = Terminal::Table.new :rows => rows + puts table + @data end private - def separator - puts (" " * 102) + def duration + @data[:duration] end - def formated_time(time, zone=true) + def min_overlap + @data[:min_overlap] + end + + def my_time_zone + @data[:my_time_zone] + end + + def time_zone + @data[:time_zone] + end + + def original + @data[:original] + end + + def overlap_1 + @data[:overlap_1] + end + + def overlap_2 + @data[:overlap_2] + end + + def full_overlap + @data[:full_overlap] + end + + def base_content + return unless original + + output = "" + + output << "* #{time_zone} (Base)\n" + output << "#{formated_time(original[:start], true)} - #{formated_time(original[:end])}\n".green + + output << timeline(original[:start], original[:end]) + end + + def min_overlap_content + return unless overlap_1 + + output = "" + + output << "* #{my_time_zone} #{EARLY_BIRD} (#{min_overlap} hour(s) of overlap)\n" + output << "#{formated_time(overlap_1[:start], true)} - #{formated_time(overlap_1[:end])}\n".green + output << timeline(overlap_1[:start], overlap_1[:end]) + + if overlap_2 + output << "\n" + output << "* #{my_time_zone} #{NIGHT_OWL} (#{min_overlap} hour(s) of overlap)\n" + output << "#{formated_time(overlap_2[:start], true)} - #{formated_time(overlap_2[:end])}\n".green + output << timeline(overlap_2[:start], overlap_2[:end]) + end + + output + end + + def full_overlap_content + output = "" + output << "* #{my_time_zone} (#{duration} hours of overlap)\n" + output << "#{formated_time(full_overlap[:start], true)} - #{formated_time(full_overlap[:end])}\n".green + output << timeline(full_overlap[:start], full_overlap[:end]) + end + + def formated_time(time, with_zone=false) format = "%T" - format.concat(" (%:z)") if zone + format.prepend("TZ: %:z | ") if with_zone time.strftime(format) end def get_color(hour) case hour @@ -64,71 +128,77 @@ when 18..21 then :light_blue when 22..23 then :blue end - end - def with_color(string, hour) - string.colorize(get_color(hour)) - end - def timeline(start_time, end_time) - print " " - (0..23).each do |hour| - printf("%-4s", hour) - end + timeline = "" - puts "" + timeline << " " - print AM + (0..23).map do |hour| + timeline << "%-4s" % hour + end - (0..23).each do |hour| - # print NOON if hour == 12 - # print SIX_AM if hour == 6 - # print SIX_PM if hour == 18 + timeline << "\n" + timeline << AM + + (0..23).map do |hour| if start_time.hour < end_time.hour if (start_time.hour..end_time.hour).cover?(hour) if end_time.hour != hour - print with_color(AVAILABLE_SLOT, hour) + timeline << with_color(AVAILABLE_SLOT, hour) else - print with_color(EMPTY_SLOT, hour) + timeline << with_color(EMPTY_SLOT, hour) end else - print with_color(EMPTY_SLOT, hour) + timeline << with_color(EMPTY_SLOT, hour) end else if start_time.hour <= 12 if (end_time.hour..start_time.hour).cover?(hour) if end_time.hour != hour - print with_color(AVAILABLE_SLOT, hour) + timeline << with_color(AVAILABLE_SLOT, hour) else - print with_color(EMPTY_SLOT, hour) + timeline << with_color(EMPTY_SLOT, hour) end else - print with_color(EMPTY_SLOT, hour) + timeline << with_color(EMPTY_SLOT, hour) end else if (end_time.hour..start_time.hour).cover?(hour) if (start_time.hour == hour) - print with_color(AVAILABLE_SLOT, hour) + timeline << with_color(AVAILABLE_SLOT, hour) else - print with_color(EMPTY_SLOT, hour) + timeline << with_color(EMPTY_SLOT, hour) end else if end_time.hour != hour - print with_color(AVAILABLE_SLOT, hour) + timeline << with_color(AVAILABLE_SLOT, hour) else - print with_color(EMPTY_SLOT, hour) + timeline << with_color(EMPTY_SLOT, hour) end end end end end - print PM - puts "" - separator + + timeline << PM + timeline << "\n" + timeline << separator + + + timeline + end + + def with_color(string, hour) + string.colorize(get_color(hour)) + end + + def separator + " " * WIDTH end end end