Module: Dialog

Defined in:
lib/nera/nera_dialog.rb

Overview

module for CUI interactive interface

Constant Summary collapse

PROMPT =
"> "

Class Method Summary collapse

Class Method Details

.ask(message = "Input y or n.", default = true) ⇒ Object




205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/nera/nera_dialog.rb', line 205

def ask( message = "Input y or n.", default = true)
  while true
    m = "(y/N)"
    m = "(Y/n)" if default
    $stdout.puts( "#{message} : #{m}")

    buf = Readline.readline(PROMPT).chomp.strip.downcase
    if buf == ''
      if default
        return true
      else
        return false
      end
    end
    
    if buf.match(/^y$/) or buf.match(/^yes$/)
      return true
    elsif buf.match(/^n$/) or buf.match(/^no$/)
      return false
    else
      $stdout.puts "The input is not valid. Try again"
      redo
    end
  end
end

.get_integer(message = "Input a integer", default = 0) ⇒ Object




181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/nera/nera_dialog.rb', line 181

def get_integer( message = "Input a integer", default = 0)
  unless default.is_a?(Integer)
    raise ArgumentError, "Arguments are not valid."
  end

  while true
    $stdout.puts( "#{message} : [#{default}]")

    buf = Readline.readline(PROMPT).chomp.strip
    buf = default.to_s if buf==''
    unless buf.match(/^[0-9]+$/)
      $stdout.puts "The input #{buf} is not valid. Try again"
      redo
    end
    num = Integer(buf)
    if yield num
      return num
    else
      $stdout.puts "The input #{buf} does not match the condition. Try again"
    end
  end
end

.message(str) ⇒ Object




115
116
117
# File 'lib/nera/nera_dialog.rb', line 115

def message( str)
  $stdout.puts str
end

.select_many(arr, message = "Select multiple numbers (ex. 1,3,5-8)", default = 0) ⇒ Object




66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/nera/nera_dialog.rb', line 66

def select_many( arr, message = "Select multiple numbers (ex. 1,3,5-8)", default = 0)
  unless arr.is_a?(Array) and arr.size >= 1
    raise ArgumentError, "Arguments are not valid."
  end

  result = []
  valid_input = nil
  until valid_input
    $stdout.puts( "#{message} : [#{default}]")
    arr.size.times do |i|
      puts( "[#{i}]:\t#{arr[i]}")
    end

    buf = Readline.readline(PROMPT).chomp
    buf = default.to_s if buf==''
    
    valid_input = true
    buf = buf.split(',').map do |e| e.strip end
    buf.each do |e|
      if e.match(/^[0-9]+$/)
        num = Integer(e)
        if num < 0 or num >= arr.size
          valid_input = nil
          break
        else
          result << num
        end
      elsif e.match(/^[0-9]+-[0-9]+$/)
        f = e.split('-')
        from = Integer(f[0])
        to = Integer(f[1])
        if from >= to
          valid_input = nil
          break
        end
        for i in from..to
          result << i if i > 0 and i <= arr.size
        end
      else
        valid_input = nil
        break
      end
    end
    $stdout.puts "The input #{buf} is not valid. Try again." unless valid_input
  end
  return result
end

.select_one(arr, message = "Input the number", default = 0) ⇒ Object


Select one from the list specified by the array.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/nera/nera_dialog.rb', line 12

def select_one( arr, message = "Input the number", default = 0)
  unless arr.is_a?(Array) and arr.size >= 1 and default.to_i >= 0 and default.to_i < arr.size
    raise ArgumentError, "Arguments are not valid."
  end

  while true
    $stdout.puts( "#{message} : [#{default.to_s}]")
    arr.size.times do |i|
      puts( "[#{i}]:\t#{arr[i]}")
    end
    buf = Readline.readline(PROMPT).chomp.strip
    buf = default.to_s if buf==''

    unless buf.match(/^[0-9]+$/)
      $stdout.puts "The input #{buf} is not valid. Try again"
      redo
    end
    num = Integer(buf)
    if num >= 0 and num < arr.size
      valid_input = true
      return num
    end
    $stdout.puts "Your input is not valid. Try again."
  end
end

.select_one_or_return_string(arr, message = "Input the number", default = 0) ⇒ Object




39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/nera/nera_dialog.rb', line 39

def select_one_or_return_string( arr, message = "Input the number", default = 0)
  unless arr.is_a?(Array) and arr.size >= 1
    raise ArgumentError, "Arguments are not valid."
  end

  while true
    $stdout.puts( "#{message} : [#{default.to_s}]")
    arr.size.times do |i|
      puts( "[#{i}]:\t#{arr[i]}")
    end
    buf = Readline.readline(PROMPT).chomp.strip
    buf = default.to_s if buf==''

    if buf.match(/^[0-9]+$/)
      num = Integer(buf)
      if num >= 0 and num < arr.size
        valid_input = true
        return num
      end
    else
      return buf
    end
    $stdout.puts "Your input is not valid. Try again."
  end
end

.set_multiple_values(arr, message = "Input the values") ⇒ Object




120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
# File 'lib/nera/nera_dialog.rb', line 120

def set_multiple_values( arr, message = "Input the values")
  unless arr.is_a?(Array)
    raise ArgumentError, "Arguments are not valid."
  end
  
  result = Hash.new
  arr.each do |a|
    unless a.is_a?(Array) and a.size == 3
      raise ArgumentError, "Arguments are not valid."
    end
    key = a[0]
    type = a[1]
    val = a[2]
    unless ( type == Integer or type == Float or type == String ) and val.is_a?(type)
      raise ArgumentError, "Arguments are not valid."
    end
    result[key] = val
  end
  unless arr.size == result.size
    raise ArgumentError, "There is a nonunique key."
  end
  
  ok = nil
  until ok
    # show the list
    arr.each_with_index do |elem,i|
      $stdout.puts "[#{i}] :\t#{elem[0]}, #{elem[1]}, #{result[elem[0]]}"
    end
    $stdout.puts "[#{arr.size}] : \tOK"
    # input
    buf = Readline.readline(PROMPT).chomp.strip
    unless buf.match(/^[0-9]+$/)
      redo
    end
    input = buf.to_i
    if input >= 0 and input < arr.size
      $stdout.puts "Input #{arr[input][0]}  (#{arr[input][1]})"
      buf = Readline.readline(PROMPT).chomp.strip
      if arr[input][1] == Integer
        begin
          result[ arr[input][0] ] = Integer(buf)
        rescue
          $stdout.puts "The specified value is not convertible to Integer"
        end
      elsif arr[input][1] == Float
        begin
          result[ arr[input][0] ] = Float(buf)
        rescue
          $stdout.puts "The specified value is not convertible to Float"
        end
      elsif arr[input][1] == String
        result[ arr[input][0] ] = buf.to_s
      end
    elsif input == arr.size
      ok = true
    end
  end
  return result
end