Class: Omaship::HarborPicker

Inherits:
Object
  • Object
show all
Includes:
LinearPickerControls
Defined in:
lib/omaship/harbor_picker.rb

Instance Method Summary collapse

Constructor Details

#initialize(harbors:, out: $stdout, input: $stdin) ⇒ HarborPicker

Returns a new instance of HarborPicker.



7
8
9
10
11
12
# File 'lib/omaship/harbor_picker.rb', line 7

def initialize(harbors:, out: $stdout, input: $stdin)
  @harbors = Array(harbors)
  @out = out
  @input = input
  @index = 0
end

Instance Method Details

#pickObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/omaship/harbor_picker.rb', line 14

def pick
  render
  loop do
    key = read_linear_navigation_key(@input)

    case key
    when :previous
      @index = (@index - 1) % @harbors.size
      render
    when :next
      @index = (@index + 1) % @harbors.size
      render
    when :enter, "q"
      return @harbors.fetch(@index).fetch("id")
    when :ctrl_c
      raise Interrupt
    end
  end
ensure
  @out.print "\e[?25h"
  @out.puts if @rendered_lines
end