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.



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

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



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

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.



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

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

.float_inputObject



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

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



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

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



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
191
# File 'lib/user_input.rb', line 142

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



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
84
# File 'lib/user_input.rb', line 56

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



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

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

.wait_for_userObject

The marvellous and mystic, heard-about so-long… unblocking read from STDIN!!



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/user_input.rb', line 215

def self::wait_for_user()
  char = nil
  # There is a difference which makes me prefer the below code from STDIN.raw(args)
  # You can try
  # char = STDIN.raw(&:getc)	
  # .., which does *not* work the same way and it won't for me.

  STDIN.raw do 
    STDIN.noecho do
      # STDIN.ready?
      # Modification for ruby 4.0.1 compatibility. Unclear what the 
      # requirements are. This works.
      until IO.select([STDIN], nil, nil, 0)
        sleep(0.1)
      end
      char = (STDIN.read_nonblock(1).ord rescue nil)
    end
  end
  return char
end