Module: NewsScraper::CLI

Extended by:
CLI
Included in:
CLI
Defined in:
lib/news_scraper/cli.rb

Constant Summary collapse

DEFAULT_COLOR =
"\x1b[36m".freeze

Instance Method Summary collapse

Instance Method Details

#confirm(msg, color: DEFAULT_COLOR) ⇒ Object



19
20
21
22
# File 'lib/news_scraper/cli.rb', line 19

def confirm(msg, color: DEFAULT_COLOR)
  print "#{color}┃\x1b[0m #{msg} (y/n) "
  $stdin.gets.chomp =~ /[Yy]/
end

#get_input(msg = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/news_scraper/cli.rb', line 24

def get_input(msg = nil)
  log(msg) if msg
  Readline.completion_append_character = " "
  Readline.completion_proc = nil
  result = begin
    Readline.readline("\x1b[34m┃ > \x1b[33m", true)
  rescue Interrupt
    nil
  end
  print "\e[0m" # reset colour
  result
end

#log(message, color: DEFAULT_COLOR) ⇒ Object



9
10
11
# File 'lib/news_scraper/cli.rb', line 9

def log(message, color: DEFAULT_COLOR)
  $stdout.puts "#{color}┃\x1b[0m " + message
end

#log_lines(message, color: DEFAULT_COLOR) ⇒ Object



13
14
15
16
17
# File 'lib/news_scraper/cli.rb', line 13

def log_lines(message, color: DEFAULT_COLOR)
  message.split("\n").each do |line|
    log(line, color: color)
  end
end

#prompt_with_options(question, options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/news_scraper/cli.rb', line 37

def prompt_with_options(question, options)
  log(question)
  log("Your options are:")
  options.each.with_index(1) do |v, idx|
    log("#{idx}) #{v}")
  end
  log("Choose a number between 1 and #{options.length}")

  Readline.completion_append_character = " "
  Readline.completion_proc = nil

  buf = -1
  available = (1..options.length).to_a
  until available.include?(buf.to_i)
    buf = begin
      Readline.readline("\x1b[34m┃ > \x1b[33m", true)
    rescue Interrupt
      nil
    end

    if buf.nil?
      STDERR.puts
      next
    end

    buf = buf.chomp
    buf = -1 if buf.empty?
    buf = -1 if buf.to_i.to_s != buf
  end

  print "\e[0m" # reset colour
  options[buf.to_i - 1]
end


77
78
79
# File 'lib/news_scraper/cli.rb', line 77

def put_footer(color: DEFAULT_COLOR)
  put_edge(color, "", "")
end

#put_header(text = "", color: DEFAULT_COLOR) ⇒ Object

Fancy Headers and Footers



73
74
75
# File 'lib/news_scraper/cli.rb', line 73

def put_header(text = "", color: DEFAULT_COLOR)
  put_edge(color, "┏━━ ", text)
end