Class: Printer

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

Instance Method Summary collapse

Instance Method Details

#pad(max, str, char = " ") ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/apod_cli/printer.rb', line 38

def pad(max, str, char=" ")
  pad = char
  padding_amount = ((max - str.length) / 2).ceil
  padding_amount.times do |n|
    pad += char
  end
  centered = "#{pad}#{str}#{pad}"
end


9
10
11
12
13
14
15
16
# File 'lib/apod_cli/printer.rb', line 9

def print_link(link_hash, start="")
  lines = start.colorize(:red) + "The astronomy picture of the day on " + link_hash[:date].colorize(:green) + " was " + link_hash[:name].colorize(:green) + ".\n"
  start.length.times do |n|
    lines += " "
  end
  lines += "The link is " + link_hash[:link].colorize(:green) + "."
  puts lines
end


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/apod_cli/printer.rb', line 17

def print_page(page_hash)
  words_arr = page_hash[:expl].split(" ")
  line = ""
  lines_arr = []
  max_line_length = 0
  words_arr.each_with_index do |word, idx|
    line += word + " "
    if line.length >= 100 || idx == words_arr.length - 1
      lines_arr << line.strip
      if line.strip.length > max_line_length then max_line_length = line.strip.length end
      line = ""
    end
  end
  puts ""
  puts pad(max_line_length, page_hash[:name], "-").colorize(:green)
  puts pad(max_line_length, page_hash[:link], "-").colorize(:green)
  lines_arr.each do |line|
    puts pad(max_line_length, line)
  end
  puts ""
end