Class: Remedy::Interaction

Inherits:
Object
  • Object
show all
Defined in:
lib/remedy/interaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil) ⇒ Interaction

Returns a new instance of Interaction.



6
7
8
# File 'lib/remedy/interaction.rb', line 6

def initialize message = nil
  @message = message
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



10
11
12
# File 'lib/remedy/interaction.rb', line 10

def message
  @message
end

Instance Method Details

#confirm(message = 'Confirm?') ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/remedy/interaction.rb', line 12

def confirm message = 'Confirm?'
  ANSI.cursor.home!
  ANSI.command.clear_line!

  print message, ' y/n '
  if Keyboard.get === :y then
    yield if block_given?
    true
  else
    false
  end
end

#debug!Object



36
37
38
39
# File 'lib/remedy/interaction.rb', line 36

def debug!
  require 'pry'
  binding.pry
end

#display(key) ⇒ Object



41
42
43
44
# File 'lib/remedy/interaction.rb', line 41

def display key
  ANSI.command.clear_line!
  print " -- You pressed: #{key.inspect}"
end

#get_keyObject



67
68
69
70
71
72
# File 'lib/remedy/interaction.rb', line 67

def get_key
  print " -- #{message}" if message

  ANSI.cursor.hide!
  key = Keyboard.get
end

#loopObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/remedy/interaction.rb', line 46

def loop
  Keyboard.raise_on_control_c!

  super do
    print " -- #{message}" if message

    ANSI.cursor.hide!
    key = Keyboard.get

    if key == ?\C-q then
      display key
      quit!
    elsif key == ?\C-d and defined? Pry then
      display key
      debug!
    end

    yield key
  end
end

#quit!Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/remedy/interaction.rb', line 25

def quit!
  confirm 'Are you sure you want to quit? You will lose everything. :(' do
    ANSI.cursor.home!
    ANSI.command.clear_down!
    ANSI.cursor.show!

    puts " -- Bye!"
    exit
  end
end