Class: CW::KeyInput

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

Instance Method Summary collapse

Constructor Details

#initializeKeyInput

Returns a new instance of KeyInput.



6
7
8
# File 'lib/cw/key_input.rb', line 6

def initialize
  @quit_count = 0
end

Instance Method Details

#charObject



10
11
12
# File 'lib/cw/key_input.rb', line 10

def char
  @chr
end

#is_letter?(char = nil) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/cw/key_input.rb', line 27

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

#is_number?(char = nil) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/cw/key_input.rb', line 32

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

#is_punctuation?(char = nil) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/cw/key_input.rb', line 37

def is_punctuation? char = nil
  char = char ? char : @chr
  [' ', ',', '.', '=', '?','/','+'].detect{|letr| letr == char}
end

#is_quit?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/cw/key_input.rb', line 60

def is_quit?
  @quit_count >= 4
end

#is_relevant_char?(char = nil) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/cw/key_input.rb', line 42

def is_relevant_char? char = nil
  char = char ? char : @chr
  is_letter?(char) || is_number?(char) || is_punctuation?(char) ? true : false
end

#push_to_quit_maybeObject



52
53
54
55
56
57
58
# File 'lib/cw/key_input.rb', line 52

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

#quitObject



64
65
66
67
68
# File 'lib/cw/key_input.rb', line 64

def quit
  Cfg.config.params["exit"] = true
  reset_stdin
  return true
end

#quit_input?Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/cw/key_input.rb', line 70

def quit_input?
  push_to_quit_maybe
  return quit if is_quit?
end

#readObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cw/key_input.rb', line 14

def read
  @chr = nil
  begin
    system("stty raw -echo")
    @chr = STDIN.getc
#        @chr = @chr.downcase unless(@chr.include? 'Q')
    #        puts "@chr = #{@chr}"
    @chr
  ensure
    system("stty raw -echo")
  end
end

#reset_stdinObject



47
48
49
# File 'lib/cw/key_input.rb', line 47

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