Class: CWG::KeyInput

Inherits:
Object
  • Object
show all
Defined in:
/Users/martyn/cw/cw_clone/lib/cw/key_input.rb

Instance Method Summary (collapse)

Constructor Details

- (KeyInput) initialize

Returns a new instance of KeyInput



7
8
9
# File '/Users/martyn/cw/cw_clone/lib/cw/key_input.rb', line 7

def initialize
  @quit_count = 0
end

Instance Method Details

- (Object) char



11
12
13
# File '/Users/martyn/cw/cw_clone/lib/cw/key_input.rb', line 11

def char
  @chr
end

- (Boolean) is_letter?

Returns:

  • (Boolean)


25
26
27
# File '/Users/martyn/cw/cw_clone/lib/cw/key_input.rb', line 25

def is_letter?
  @chr >= 'a' && @chr <= 'z'
end

- (Boolean) is_number?

Returns:

  • (Boolean)


29
30
31
# File '/Users/martyn/cw/cw_clone/lib/cw/key_input.rb', line 29

def is_number?
  @chr >= '0' && @chr <= '9'
end

- (Boolean) is_punctuation?

Returns:

  • (Boolean)


33
34
35
# File '/Users/martyn/cw/cw_clone/lib/cw/key_input.rb', line 33

def is_punctuation?
  [' ', ',', '.', '=', '?'].detect{|letr| letr == @chr}
end

- (Boolean) is_quit?

Returns:

  • (Boolean)


54
55
56
# File '/Users/martyn/cw/cw_clone/lib/cw/key_input.rb', line 54

def is_quit?
  @quit_count >= 4
end

- (Boolean) is_relevant_char?

Returns:

  • (Boolean)


37
38
39
# File '/Users/martyn/cw/cw_clone/lib/cw/key_input.rb', line 37

def is_relevant_char?
  is_letter? || is_number? || is_punctuation? ? true : false
end

- (Object) push_to_quit_maybe



46
47
48
49
50
51
52
# File '/Users/martyn/cw/cw_clone/lib/cw/key_input.rb', line 46

def push_to_quit_maybe
  if @chr == 'q'
    @quit_count += 1
  else
    @quit_count = 0
  end
end

- (Object) quit



58
59
60
61
62
# File '/Users/martyn/cw/cw_clone/lib/cw/key_input.rb', line 58

def quit
  Params.exit = true
  reset_stdin
  return true
end

- (Boolean) quit_input?

Returns:

  • (Boolean)


64
65
66
67
# File '/Users/martyn/cw/cw_clone/lib/cw/key_input.rb', line 64

def quit_input?
  push_to_quit_maybe
  return quit if is_quit?
end

- (Object) read



15
16
17
18
19
20
21
22
23
# File '/Users/martyn/cw/cw_clone/lib/cw/key_input.rb', line 15

def read
  @chr = nil
  begin
    system("stty raw -echo")
    @chr = STDIN.getc
  ensure
    system("stty raw -echo")
  end
end

- (Object) reset_stdin



41
42
43
# File '/Users/martyn/cw/cw_clone/lib/cw/key_input.rb', line 41

def reset_stdin
  system("stty -raw echo")
end