Sha256: 9b5b484628edad617ab202050baaf52573b1862d5fc78337d498b9316a8d4e67

Contents?: true

Size: 878 Bytes

Versions: 3

Compression:

Stored size: 878 Bytes

Contents

# frozen_string_literal: true

require 'io/console'

module MakeMenu
  module Console
    module Prompter
      PressedEscape = Class.new(StandardError)

      def self.prompt(text = '', obscure: false)
        print text

        input = ''
        char = ''

        until !char.empty? && char.ord == 13
          char = $stdin.getch

          case char.ord
          when 127
            # BACKSPACE
            input = input[0..-2]
            print "\r#{text}#{' ' * input.size} "
            print "\r#{text}#{obscure ? '*' * input.size : input}"

          when 27
            # ESC
            raise PressedEscape

          when 13
            # ENTER

          else
            input += char
            if obscure
              print '*'
            else
              print char
            end
          end
        end

        input
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
make_menu-2.0.0 lib/make_menu/console/prompter.rb
make_menu-1.1.0 lib/make_menu/console/prompter.rb
make_menu-1.0.0 lib/make_menu/console/prompter.rb