Sha256: 77b33a4613dc6080155886b523286c139c44655d54ac2262c92e99d38a6decd4

Contents?: true

Size: 859 Bytes

Versions: 1

Compression:

Stored size: 859 Bytes

Contents

# frozen_string_literal: true

module SlackMessaging
  class HighlineCli
    def ask(prompt)
      highline_client.ask(prompt) do |conf|
        conf.readline = true
      end.to_s
    end

    def ask_yes_no(prompt)
      answer = highline_client.ask(prompt) do |conf|
        conf.readline = true
      end.to_s

      answer.empty? ? true : !!(answer =~ /^y/i)
    end

    def ask_options(prompt, choices)
      choices_as_string_options = ''.dup
      choices.each { |choice| choices_as_string_options << "#{choices.index(choice) + 1}. #{choice}\n" }
      compiled_prompt = "#{prompt}\n#{choices_as_string_options.strip}"

      index = highline_client.ask(compiled_prompt) do |conf|
        conf.readline = true
      end.to_i - 1

      choices[index]
    end

    private def highline_client
      @highline_client ||= HighLine.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slack_messaging-3.0.0 lib/slack_messaging/highline_cli.rb