Module: Origen::Utility::InputCapture

Included in:
Application::Release, RegressionManager
Defined in:
lib/origen/utility/input_capture.rb

Instance Method Summary collapse

Instance Method Details

#find_space(line, max_position) ⇒ Object

Find the space closest to but less than max_position, returns max_position if none can be found



120
121
122
123
124
# File 'lib/origen/utility/input_capture.rb', line 120

def find_space(line, max_position)
  x = max_position
  x -= 1 until line[x] == ' ' || x == 0
  x == 0 ? max_position : x
end

#get_text(options = {}) ⇒ Object

Gets text input from the user Supply an optional default value in the event that the user enters nothing



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
64
65
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
# File 'lib/origen/utility/input_capture.rb', line 14

def get_text(options = {})
  options = {
    default:        false,
    single:         false, # Set if only a single line entry is expected
    confirm:        false,
    accept:         false, # Supply and array of entries you are willing to accept
    case_sensitive: false, # If accept values are supplied they will be treated as case
    # in-sensitive by default
    wrap:           true   # Automatically split long lines
  }.merge(options)
  if options[:confirm]
    puts "Type 'yes' or 'no' to confirm or 'quit' to abort."
  elsif options[:accept]
    puts "You can enter: #{options[:accept].map { |v| "'#{v}'" }.join(', ')} or 'quit' to abort."
  # "
  else
    puts options[:single] ? "Enter 'quit' to abort." : "Enter a single '.' to finish, or 'quit' to abort."
  end
  puts '------------------------------------------------------------------------------------------'
  text = ''
  line = ''
  if options[:confirm]
    print "(#{options[:default]}): " if options[:default]
  else
    print "Hit return to accept the default (#{options[:default]}): " if options[:default]
  end

  while line != '.'
    orig_line = Readline.readline('', false).chomp.rstrip
    line = orig_line.strip
    if (line.empty? || line == '.') && text.empty? && options[:default]
      text = options[:default].to_s
      line = '.'
    elsif line.downcase == 'quit'
      exit 0
    elsif line == '.'
    # Do nothing
    else
      if options[:wrap]
        split_long_line(orig_line) do |short_line|
          text << "#{short_line}\n"
        end
      else
        text << orig_line
      end
    end
    confirm = text.strip.downcase if options[:confirm]
    text = text.strip if options[:single]
    line = '.' if options[:single] || options[:confirm]
  end
  puts ''

  if options[:confirm]
    if confirm == 'no' || confirm == 'n'
      if options[:confirm] == :return_boolean
        return false
      else
        exit 0
      end
    end
    if confirm == 'yes' || confirm == 'y'
      if options[:confirm] == :return_boolean
        true
      end
    else
      get_text(options)
    end

  elsif options[:accept]
    accept = options[:accept].map do |v|
      v = v.to_s
      v = v.downcase unless options[:case_sensitive]
      v
    end
    text = text.downcase unless options[:case_sensitive]
    text = text.strip
    if accept.include?(text)
      text
    else
      get_text(options)
    end

  else
    text
  end
end

#split_long_line(line) ⇒ Object

Splits a long line into short ones, split by the nearest space



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/origen/utility/input_capture.rb', line 102

def split_long_line(line)
  if line.length <= 90
    yield line
  else
    until line.empty?
      if line.length <= 90
        yield line
        line = ''
      else
        yield line.slice(0, find_space(line, 90))
        line = line.slice(find_space(line, 90), line.length).strip
      end
    end
  end
end