Class: CW::KeyInput
- Inherits:
-
Object
- Object
- CW::KeyInput
- Defined in:
- lib/cw/key_input.rb
Instance Method Summary collapse
- #char ⇒ Object
-
#initialize ⇒ KeyInput
constructor
A new instance of KeyInput.
- #is_letter?(char = nil) ⇒ Boolean
- #is_number?(char = nil) ⇒ Boolean
- #is_punctuation?(char = nil) ⇒ Boolean
- #is_quit? ⇒ Boolean
- #is_relevant_char?(char = nil) ⇒ Boolean
- #push_to_quit_maybe ⇒ Object
- #quit ⇒ Object
- #quit_input? ⇒ Boolean
- #read ⇒ Object
- #reset_stdin ⇒ Object
Constructor Details
#initialize ⇒ KeyInput
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
#char ⇒ Object
10 11 12 |
# File 'lib/cw/key_input.rb', line 10 def char @chr end |
#is_letter?(char = nil) ⇒ 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
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
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
60 61 62 |
# File 'lib/cw/key_input.rb', line 60 def is_quit? @quit_count >= 4 end |
#is_relevant_char?(char = nil) ⇒ 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_maybe ⇒ Object
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 |
#quit ⇒ Object
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
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 |
#read ⇒ Object
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_stdin ⇒ Object
47 48 49 |
# File 'lib/cw/key_input.rb', line 47 def reset_stdin system("stty -raw echo") end |