Sha256: 63888e001b383e1494be246ea4d03781735c04b0b9a0a496b42103273a5a4e4e

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require 'io/console'

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

      def self.prompt_and_save(text, file:, obscure: false)
        if file.is_a? Symbol
          file = ".#{file}"
        end

        current = File.exist?(file) ? File.read(file).strip : ''

        response = prompt(text, input: current, obscure: obscure)

        if response.empty?
          File.delete(file) if File.exist?(file)
        else
          File.write(file, response)
        end

        return response
      end

      def self.prompt(text = '', input: '', obscure: false, value_color: :light_yellow)
        text = text.bold

        print "\r#{text}#{input.color(value_color)}"

        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 ? '*'.color(value_color) * input.size : input.color(value_color)}"

          when 27
            # ESC
            raise PressedEscape if input.empty?

            print "\r#{text}#{' ' * input.size} "
            print "\r#{text}"

            input = ''
            char = ''

          when 13
            # ENTER

          else
            input += char
            if obscure
              print '*'.color(value_color)
            else
              print char.color(value_color)
            end
          end
        end

        print "\r#{text}#{' ' * input.size} "
        print "\r#{text}"

        input
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
make_menu-2.1.2 lib/make_menu/console/prompter.rb