lib/tty/reader.rb
# Get a single line from STDIN. Each key pressed is echoed
# back to the shell. The input terminates when enter or
# return key is pressed.
#
# @param [String] prompt
#   the prompt to display before input
# @param [String] value
#   the value to pre-populate line with
# @param [Boolean] echo
#   whether to echo chars back or not, defaults to false
# @param [Array<Symbol>] exit_keys
#   the custom keys to exit line editing
# @option [Boolean] raw
#   whenther raw mode is enabled, defaults to true
# @option [Boolean] nonblock
#   whether to wait for input or not, defaults to false
#
# @return [String]
#
# @api public
Get a single line from STDIN. Each key pressed is echoed
back to the shell. The input terminates when enter or
return key is pressed.
Get a single line from STDIN . Each key pressed is echoed back to the shell . The input terminates when enter or return key is pressed .
def read_line(prompt = "", value: "", echo: true, raw: true,
                  nonblock: false, exit_keys: nil)
      line = Line.new(value, prompt: prompt)
      screen_width = TTY::Screen.width
      history_in_use = false
      previous_key_name = ""
      buffer = ""
      output.print(line)
      while (codes = get_codes(echo: echo, raw: raw, nonblock: nonblock)) &&
            (code = codes[0])
        char = codes.pack("U*")
        key_name = console.keys[char]
        if exit_keys && exit_keys.include?(key_name)
          trigger_key_event(char, line: line.to_s)
          break
        end
        if raw && echo
          clear_display(line, screen_width)
        end
        if (key_name == :tab || code == TAB || key_name == :shift_tab) &&
           completion_handler
          initial = previous_key_name != :tab && previous_key_name != :shift_tab
          direction = key_name == :shift_tab ? :previous : :next
          if completion = @completer.complete(line, initial: initial,
                                                    direction: direction)
            trigger_completion_event(completion, line.to_s)
          end
        elsif key_name == :escape && completion_handler &&
              (previous_key_name == :tab || previous_key_name == :shift_tab)
          @completer.cancel(line)
        elsif key_name == :backspace || code == BACKSPACE
          if !line.start?
            line.left
            line.delete
          end
        elsif key_name == :delete || code == DELETE
          line.delete
        elsif key_name.to_s =~ /ctrl_/
        elsif key_name == :up
          @history.replace(line.text) if history_in_use
          if history_previous?
            line.replace(history_previous(skip: !history_in_use))
            history_in_use = true
          end
        elsif key_name == :down
          @history.replace(line.text) if history_in_use
          if history_next?
            line.replace(history_next)
          elsif history_in_use
            line.replace(buffer)
            history_in_use = false
          end
        elsif key_name == :left
          line.left
        elsif key_name == :right
          line.right
        elsif key_name == :home
          line.move_to_start
        elsif key_name == :end
          line.move_to_end
        else
          if raw && [CARRIAGE_RETURN, NEWLINE].include?(code)
            char = "\n"
            line.move_to_end
          end
          line.insert(char)
          buffer = line.text unless history_in_use
        end
        if (key_name == :backspace || code == BACKSPACE) && echo
          if raw
            output.print("\e[1X") unless line.start?
          else
            output.print(?\s + (line.start? ? "" : ?\b))
          end
        end
        previous_key_name = key_name
        trigger_key_event(char, line: line.to_s)
        if raw && echo
          output.print(line.to_s)
          if char == "\n"
            line.move_to_start
          elsif !line.end? 
            output.print(cursor.backward(line.text_size - line.cursor))
          end
        end
        if [CARRIAGE_RETURN, NEWLINE].include?(code)
          buffer = ""
          output.puts unless echo
          break
        end
      end
      if track_history? && echo
        add_to_history(line.text.rstrip)
      end
      line.text
    end
def read_line ( prompt = "" , value : "" , echo : true , raw : true , nonblock : false , exit_keys : nil ) line = Line . new ( value , prompt : prompt ) screen_width = TTY :: Screen . width history_in_use = false previous_key_name = "" buffer = "" output . print ( line ) while ( codes = get_codes ( echo : echo , raw : raw , nonblock : nonblock ) ) && ( code = codes [ 0 ] ) char = codes . pack ( "U*" ) key_name = console . keys [ char ] if exit_keys && exit_keys . include? ( key_name ) trigger_key_event ( char , line : line . to_s ) break end if raw && echo clear_display ( line , screen_width ) end if ( key_name == :tab || code == TAB || key_name == :shift_tab ) && completion_handler initial = previous_key_name != :tab && previous_key_name != :shift_tab direction = key_name == :shift_tab ? :previous : :next if completion = @completer . complete ( line , initial : initial , direction : direction ) trigger_completion_event ( completion , line . to_s ) end elsif key_name == :escape && completion_handler && ( previous_key_name == :tab || previous_key_name == :shift_tab ) @completer . cancel ( line ) elsif key_name == :backspace || code == BACKSPACE if ! line . start? line . left line . delete end elsif key_name == :delete || code == DELETE line . delete elsif key_name . to_s =~ / ctrl_ / elsif key_name == :up @history . replace ( line . text ) if history_in_use if history_previous? line . replace ( history_previous ( skip : ! history_in_use ) ) history_in_use = true end elsif key_name == :down @history . replace ( line . text ) if history_in_use if history_next? line . replace ( history_next ) elsif history_in_use line . replace ( buffer ) history_in_use = false end elsif key_name == :left line . left elsif key_name == :right line . right elsif key_name == :home line . move_to_start elsif key_name == :end line . move_to_end else if raw && [ CARRIAGE_RETURN , NEWLINE ] . include? ( code ) char = "\n" line . move_to_end end line . insert ( char ) buffer = line . text unless history_in_use end if ( key_name == :backspace || code == BACKSPACE ) && echo if raw output . print ( "\e[1X" ) unless line . start? else output . print ( ?\s + ( line . start? ? "" : ?\b ) ) end end previous_key_name = key_name trigger_key_event ( char , line : line . to_s ) if raw && echo output . print ( line . to_s ) if char == "\n" line . move_to_start elsif ! line . end? output . print ( cursor . backward ( line . text_size - line . cursor ) ) end end if [ CARRIAGE_RETURN , NEWLINE ] . include? ( code ) buffer = "" output . puts unless echo break end end if track_history? && echo add_to_history ( line . text . rstrip ) end line . text end
Get a single line from STDIN.
Get a single line from STDIN .
# skip # trigger before line is printed to allow for line changes # readjust cursor position
None
String
prompt
value
echo
raw
nonblock
exit_keys
nonblock whether to wait for input or not, defaults to false
nonblock whether to wait for input or not defaults to false
false
api
None