Method: TTY::Reader#read_multiline

Defined in:
lib/tty/reader.rb

#read_multiline(prompt = "", value: "", echo: true, raw: true, nonblock: false) {|String| ... } ⇒ Array[String] Also known as: read_lines

Read multiple lines and return them in an array. Skip empty lines in the returned lines array. The input gathering is terminated by Ctrl+d or Ctrl+z.

Parameters:

  • prompt (String) (defaults to: "")

    the prompt displayed before the input

  • value (String) (defaults to: "")

    the value to pre-populate line with

  • echo (Boolean) (defaults to: true)

    whether to echo chars back or not, defaults to false

  • [Boolean] (Hash)

    a customizable set of options

Yields:

  • (String)

    line

Returns:

  • (Array[String])

393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/tty/reader.rb', line 393

def read_multiline(prompt = "", value: "", echo: true, raw: true,
                   nonblock: false)
  @stop = false
  lines = []
  empty_str = ""

  loop do
    line = read_line(prompt, value: value, echo: echo, raw: raw,
                             nonblock: nonblock)
    value = empty_str unless value.empty? # reset
    break if !line || line == empty_str
    next  if line !~ /\S/ && !@stop

    if block_given?
      yield(line) unless line.to_s.empty?
    else
      lines << line unless line.to_s.empty?
    end
    break if @stop
  end

  lines
end