Sha256: 17ab3a2b62213daaf1080116efcaa2bb71f07011ca3af1a1a1848502bbed30f6

Contents?: true

Size: 1.81 KB

Versions: 178

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

require "appsignal/utils/rails_helper"

module Appsignal
  class CLI
    module Helpers
      private

      COLOR_CODES = {
        :red => 31,
        :green => 32,
        :yellow => 33,
        :blue => 34,
        :pink => 35,
        :default => 0
      }.freeze

      def ruby_2_6_or_up?
        Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.6.0")
      end

      def coloring=(value)
        @coloring = value
      end

      def coloring?
        return true unless defined?(@coloring)
        @coloring
      end

      def colorize(text, color)
        return text unless coloring?
        return text if Gem.win_platform?

        reset_color_code = COLOR_CODES.fetch(:default)
        color_code = COLOR_CODES.fetch(color, reset_color_code)

        "\e[#{color_code}m#{text}\e[#{reset_color_code}m"
      end

      def periods
        3.times do
          print "."
          sleep 0.5
        end
      end

      def press_any_key
        puts
        print "  Ready? Press any key:"
        stdin.getc
        puts
        puts
      end

      def ask_for_input
        value = stdin.gets
        value ? value.chomp : ""
      rescue Interrupt
        puts "\nExiting..."
        exit 1
      end

      def required_input(prompt)
        loop do
          print prompt
          value = ask_for_input
          return value unless value.empty?
        end
      end

      def yes_or_no(prompt, options = {})
        loop do
          print prompt
          input = ask_for_input.strip
          input = options[:default] if input.empty? && options[:default]
          case input
          when "y", "Y", "yes"
            return true
          when "n", "N", "no"
            return false
          end
        end
      end

      def stdin
        $stdin
      end
    end
  end
end

Version data entries

178 entries across 178 versions & 1 rubygems

Version Path
appsignal-3.3.11-java lib/appsignal/cli/helpers.rb
appsignal-3.3.11 lib/appsignal/cli/helpers.rb
appsignal-3.4.0-java lib/appsignal/cli/helpers.rb
appsignal-3.4.0 lib/appsignal/cli/helpers.rb
appsignal-3.3.10-java lib/appsignal/cli/helpers.rb
appsignal-3.3.10 lib/appsignal/cli/helpers.rb
appsignal-3.3.9-java lib/appsignal/cli/helpers.rb
appsignal-3.3.9 lib/appsignal/cli/helpers.rb
appsignal-3.3.8-java lib/appsignal/cli/helpers.rb
appsignal-3.3.8 lib/appsignal/cli/helpers.rb
appsignal-3.3.7-java lib/appsignal/cli/helpers.rb
appsignal-3.3.7 lib/appsignal/cli/helpers.rb
appsignal-3.3.6-java lib/appsignal/cli/helpers.rb
appsignal-3.3.6 lib/appsignal/cli/helpers.rb
appsignal-3.3.5-java lib/appsignal/cli/helpers.rb
appsignal-3.3.5 lib/appsignal/cli/helpers.rb
appsignal-3.3.4-java lib/appsignal/cli/helpers.rb
appsignal-3.3.4 lib/appsignal/cli/helpers.rb
appsignal-3.3.3-java lib/appsignal/cli/helpers.rb
appsignal-3.3.3 lib/appsignal/cli/helpers.rb