Class: Inspector::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/inspector/console.rb

Overview

Console provides functions for user input

Instance Method Summary collapse

Instance Method Details

#char_if_pressedObject

Listens for key presses and returns the pressed key without pressing return

:call-seq:

char_if_pressed


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/inspector/console.rb', line 21

def char_if_pressed
  begin
    system("stty raw -echo")
    c = nil
    if $stdin.ready?
        c = $stdin.getc
    end
    c.chr if c
  ensure
    system "stty -raw echo"
  end
end

#prompt(choice_line) ⇒ Object

Prompts the user for input.

:call-seq:

prompt(choice_line) -> char

choice_line is the prompt string. If the prompt string contains a (x) sequence x is a valid choice the is relized when pressed and returned.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/inspector/console.rb', line 41

def prompt(choice_line)
  pattern = /(?<=\()./
  choices = choice_line.scan(pattern)

  choice = nil

  while choices.find_index(choice).nil?
    print choice_line 
    choice = nil
    choice = char_if_pressed while choice == nil
    sleep 0.1
    puts
  end

  choice
end