Class: UserInput

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

Overview

Provides functions to convert terminal input to desired data-types and does some verifications. Keyboard-input is always non-blocking, to allow syntax-checking on the fly and to accelerate the work-flow, wherever possible.

Class Method Summary collapse

Class Method Details

.a2i(ar) ⇒ Object

For cases when no data-type can be imposed for the input, but ultimately an integer is needed. [4,3,2] becomes 432. Beware, that the character-values must have been converted to int, already, by substracting 48 from any number-symbol, or by converting its character incarnation to int.



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/user_input.rb', line 197

def self::a2i(ar)
  if ar.respond_to?(:to_ary)
    ar.reverse!
    res = 0
    ar.each_with_index do |n, i|
      res += (n * 10**i)
    end
    return res
  else
    puts "Invalid input " << ar.to_s
    return 0
  end
end

.char_inputObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/user_input.rb', line 104

def self::char_input
  begin
    char = wait_for_user()
    if( 'q' == char.chr.downcase)
      print "\n\tBye "
      return :quit_game
    elsif ('a'..'z').member?(char.chr.downcase)
      return char
    end
  rescue Interrupt
    exit true
  end
end

.debout(str) ⇒ Object

Brutal log-function: See your desired message, then hit ‘q’ to exit program.



28
29
30
31
32
33
34
35
# File 'lib/user_input.rb', line 28

def self::debout(str)
  if $DEBUG == :DEBUG
    puts str
    if(wait_for_user().chr == 'q')
      exit true
    end
  end
end

.float_inputObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/user_input.rb', line 85

def self::float_input
  ar = []
  loop do
    begin
      char = wait_for_user()
      if char == 10 || char == 13
        return ar.join.to_f
      elsif( 'q' == char.chr.downcase)
        print "\n\tBye "
        return :quit_game
      else
        ar << char
      end
    rescue Interrupt
      exit true
    end
  end
end

.inputObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/user_input.rb', line 119

def self::input
  inp = []
  loop do
    begin
      char = wait_for_user()
      if char == 10 || char == 13
        return inp
      elsif( 'q' == char.chr.downcase)
        print "\n\tBye. "
        return :quit_game
      elsif ('a'..'z').member?(char.chr.downcase)
        return char.chr
      else
        inp << char - 48
      end
    rescue Interrupt
      exit true
    end

  end
end

.int_array_input(len, range = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/user_input.rb', line 141

def self::int_array_input(len, range = nil)
  ar = []
  until ar.length == len
    if(range)
      print bold(' (Available: ')
      disp = range.to_a - ar
      if disp.length > 2
        print disp.join(', ')
      elsif disp.length == 2
        print disp.first.to_s << ' or ' << disp.last.to_s
      else
        print disp.first.to_s
      end
      puts bold(')')
    end

    begin
      char = wait_for_user()
      if( 'q' == char.chr.downcase)
        print "\n\tBye. "
        return :quit_game
      elsif range && range.member?(char - 48)
        num = char - 48
        if(ar.member?(num)) 
          puts '%d cannot happen twice!' %num
          puts 'Try again or push "q" to quit.'
        else
          ar << num
        end
      else
        print "%s is not a valid input." %char.chr
        if(range)
          print ' (Available: '
          disp = range.to_a - ar
          if disp.length > 2
            puts disp.join(', ') << ')' 
          elsif disp.length == 2
            puts disp.first.to_s << ' or ' << disp.last.to_s << ')'
          else
            puts disp.first.to_s << ')'
          end
        end
        puts 'Try again or push "q" to quit.'
      end
    rescue Interrupt
      exit true
    end
  end
  return ar
end

.int_input(range = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/user_input.rb', line 55

def self::int_input(range = nil)
  ar = []
  loop do
    begin
      char = wait_for_user()
      if char == 10 || char == 13
        if(range && range.min > ar.join.to_i)
          puts "\n%d is too small. Choose a number from the list of events or 0 (zero)" %ar.join.to_i
          ar.clear
        else
          return ar.join.to_i
        end
      elsif( 'q' == char.chr.downcase)
        print "\n\tBye "
        return :quit_game
      else
        num = char - 48
        ar << num
        print num
        if(range && range.max < ar.join.to_i )
          puts "\n%d is becoming too big. Choose a number from the list of events or 0 (zero)" %ar.join.to_i
          ar.clear
        end
      end
    rescue Interrupt
      exit true
    end
  end
end

.string_inputObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/user_input.rb', line 37

def self::string_input
  inp = ""
  loop do
    begin
      char = wait_for_user()
      if char == 10 || char == 13
        return inp
      elsif( 'q' == char.chr.downcase)
        exit true
      else
        inp << char
      end
    rescue Interrupt
      exit true
    end
  end
end