Module: HlockeyCLI

Defined in:
lib/hlockey-cli.rb,
lib/hlockey-cli/actions.rb,
lib/hlockey-cli/version.rb,
lib/hlockey-cli/user_selection.rb

Overview

Hlockey CLI

Defined Under Namespace

Modules: Actions

Constant Summary collapse

VERSION =
"7".freeze

Class Method Summary collapse

Class Method Details

.mainObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hlockey-cli.rb', line 10

def self.main
  puts("Please wait...")

  Hlockey::Message.colorize = ->(color, string) { Paint[string, color] }

  league = Hlockey.new_current_league
  alerts = league.alerts.clone

  if Time.now < league.start_time
    puts(Hlockey::Message.new(:season_starts,
                              season: league.season,
                              time: league.start_time))
  end

  loop do
    league.update_state

    unless alerts == league.alerts
      alerts = league.alerts.clone
      alerts.each(&method(:puts))
      puts # newline
    end
    puts(Hlockey::Message.new(:season_day, season: league.season, day: league.day))

    selected_action = user_selection(
      Actions::ACTIONS,
      default: "Exit",
      str_process: ->(s) { s.to_s.capitalize.sub("_", " ") }
    )
    exit if selected_action.nil?

    Actions.send(selected_action, league)
  end
end

.user_selection(choices, default: "Back", str_process: ->(s) { s }) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/hlockey-cli/user_selection.rb', line 2

def self.user_selection(choices, default: "Back", str_process: ->(s) { s })
  puts("Please enter a number...")

  # 1 is added to i when printing choices
  # to avoid confusing a non-numerical input with the first choice
  choices.each_with_index do |choice, i|
    puts("#{(i + 1).to_s.rjust(2)} - #{str_process.call(choice)}")
  end
  puts("anything else - #{default}")

  # 1 is subracted to undo adding 1 earlier
  choice_idx = $stdin.gets.to_i - 1

  !choice_idx.negative? && choice_idx < choices.length ? choices[choice_idx] : nil
end