Sha256: 457fd18a5faf0b33798bc1cc4a93f762f97cd242306573c5c247f9ea9402506a

Contents?: true

Size: 1.68 KB

Versions: 41

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Doing
  # Request Yes/No answers on command line
  module PromptYN
    ##
    ## Ask a yes or no question in the terminal
    ##
    ## @param      question          [String] The question
    ##                               to ask
    ## @param      default_response  [Boolean]   default
    ##                               response if no input
    ##
    ## @return     [Boolean] yes or no
    ##
    def yn(question, default_response: false)
      return @force_answer == :yes ? true : false unless @force_answer.nil?

      $stdin.reopen('/dev/tty')

      default = if default_response.is_a?(String)
                  default_response =~ /y/i ? true : false
                else
                  default_response
                end

      # if global --default is set, answer default
      return default if @default_answer

      # if this isn't an interactive shell, answer default
      return default unless $stdout.isatty

      # clear the buffer
      if ARGV&.length
        ARGV.length.times do
          ARGV.shift
        end
      end
      system 'stty cbreak'

      cw = white
      cbw = boldwhite
      cbg = boldgreen
      cd = Color.default

      options = unless default.nil?
                  "#{cw}[#{default ? "#{cbg}Y#{cw}/#{cbw}n" : "#{cbw}y#{cw}/#{cbg}N"}#{cw}]#{cd}"
                else
                  "#{cw}[#{cbw}y#{cw}/#{cbw}n#{cw}]#{cd}"
                end
      $stdout.syswrite "#{cbw}#{question.sub(/\?$/, '')} #{options}#{cbw}?#{cd} "
      res = $stdin.sysread 1
      puts
      system 'stty cooked'

      res.chomp!
      res.downcase!

      return default if res.empty?

      res =~ /y/i ? true : false
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
doing-2.1.88 lib/doing/prompt/yn.rb
doing-2.1.87 lib/doing/prompt/yn.rb
doing-2.1.86 lib/doing/prompt/yn.rb
doing-2.1.85 lib/doing/prompt/yn.rb
doing-2.1.84 lib/doing/prompt/yn.rb
doing-2.1.83 lib/doing/prompt/yn.rb
doing-2.1.82 lib/doing/prompt/yn.rb
doing-2.1.81 lib/doing/prompt/yn.rb
doing-2.1.80 lib/doing/prompt/yn.rb
doing-2.1.79 lib/doing/prompt/yn.rb
doing-2.1.78 lib/doing/prompt/yn.rb
doing-2.1.77 lib/doing/prompt/yn.rb
doing-2.1.76 lib/doing/prompt/yn.rb
doing-2.1.75 lib/doing/prompt/yn.rb
doing-2.1.74 lib/doing/prompt/yn.rb
doing-2.1.73 lib/doing/prompt/yn.rb
doing-2.1.72 lib/doing/prompt/yn.rb
doing-2.1.69 lib/doing/prompt/yn.rb
doing-2.1.68 lib/doing/prompt/yn.rb
doing-2.1.66 lib/doing/prompt/yn.rb