Class: CalendarAssistant::CLI::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/calendar_assistant/cli/printer.rb

Defined Under Namespace

Classes: LaunchUrlException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = STDOUT) ⇒ Printer

Returns a new instance of Printer.



10
11
12
# File 'lib/calendar_assistant/cli/printer.rb', line 10

def initialize io = STDOUT
  @io = io
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



8
9
10
# File 'lib/calendar_assistant/cli/printer.rb', line 8

def io
  @io
end

Instance Method Details

#launch(url) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/calendar_assistant/cli/printer.rb', line 14

def launch url
  begin
    Launchy.open(url)
  rescue Exception => e
    raise LaunchUrlException.new(e)
  end
end


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/calendar_assistant/cli/printer.rb', line 47

def print_available_blocks ca, event_set, omit_title: false
  ers = ca.config.calendar_ids.map {|calendar_id| ca.event_repository calendar_id}
  time_zones = ers.map {|er| er.calendar.time_zone}.uniq

  unless omit_title
    puts Rainbow(ers.map {|er| er.calendar.id}.join(", ")).italic
    puts Rainbow(sprintf("- looking for blocks at least %s long",
                         ChronicDuration.output(
                             ChronicDuration.parse(
                                 ca.config.setting(Config::Keys::Settings::MEETING_LENGTH))))).italic
    time_zones.each do |time_zone|
      puts Rainbow(sprintf("- between %s and %s in %s",
                           ca.config.setting(Config::Keys::Settings::START_OF_DAY),
                           ca.config.setting(Config::Keys::Settings::END_OF_DAY),
                           time_zone,
                   )).italic
    end
    puts
  end

  if event_set.is_a?(EventSet::Hash)
    event_set.events.each do |key, value|
      puts(sprintf(Rainbow("Availability on %s:\n").bold,
                   key.strftime("%A, %B %-d")))
      print_available_blocks ca, event_set.new(value), omit_title: true
      puts
    end
    return
  end

  events = Array(event_set.events)
  if events.empty?
    puts "  (No available blocks in this time range.)"
    return
  end

  events.each do |event|
    line = []
    time_zones.each do |time_zone|
      line << sprintf("%s - %s",
                      event.start_time.in_time_zone(time_zone).strftime("%l:%M%P"),
                      event.end_time.in_time_zone(time_zone).strftime("%l:%M%P %Z"))
    end
    line.uniq!
    puts "" + line.join(" / ") + Rainbow(" (" + event.duration + ")").italic
    pp event if ca.config.debug?
  end
end


42
43
44
45
# File 'lib/calendar_assistant/cli/printer.rb', line 42

def print_events ca, event_set, presenter_class: CLI::EventSetPresenter
  puts presenter_class.new(event_set, config: ca.config).to_s
  puts
end

#prompt(query, default = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/calendar_assistant/cli/printer.rb', line 26

def prompt query, default = nil
  loop do
    message = query
    message += " [#{default}]" if default
    message += ": "
    print Rainbow(message).bold
    answer = STDIN.gets.chomp.strip
    if answer.empty?
      return default if default
      puts Rainbow("Please provide an answer.").red
    else
      return answer
    end
  end
end

#puts(*args) ⇒ Object



22
23
24
# File 'lib/calendar_assistant/cli/printer.rb', line 22

def puts *args
  io.puts(*args)
end