Skip to content

Commit

Permalink
Merge pull request #784 from rtib/fix_prompt
Browse files Browse the repository at this point in the history
tty-prompt usage
  • Loading branch information
rodjek authored Oct 22, 2019
2 parents 25ad912 + 43f2155 commit a60f378
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lib/pdk/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
require 'pdk'
require 'pdk/cli/errors'

module TTY
autoload :Prompt, 'tty/prompt'
autoload :TestPrompt, 'tty/test_prompt'
end

class Cri::Command::CriExitException
def initialize(is_error:)
@is_error = is_error
Expand Down
2 changes: 1 addition & 1 deletion lib/pdk/cli/module/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module PDK::CLI

run do |opts, args, _cmd|
require 'pdk/generate/module'
require 'tty-prompt'
require 'tty/prompt'

module_name = args[0]

Expand Down
6 changes: 5 additions & 1 deletion lib/pdk/cli/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Util
autoload :CommandRedirector, 'pdk/cli/util/command_redirector'
autoload :OptionNormalizer, 'pdk/cli/util/option_normalizer'
autoload :OptionValidator, 'pdk/cli/util/option_validator'
autoload :Interview, 'pdk/cli/util/interview'
autoload :Spinner, 'pdk/cli/util/spinner'

# Ensures the calling code is being run from inside a module directory.
#
Expand Down Expand Up @@ -37,6 +39,8 @@ def spinner_opts_for_platform
module_function :spinner_opts_for_platform

def prompt_for_yes(question_text, opts = {})
require 'tty/prompt'

prompt = opts[:prompt] || TTY::Prompt.new(help_color: :cyan)
validator = proc { |value| [true, false].include?(value) || value =~ %r{\A(?:yes|y|no|n)\Z}i }
response = nil
Expand All @@ -46,7 +50,7 @@ def prompt_for_yes(question_text, opts = {})
q.default opts[:default] unless opts[:default].nil?
q.validate(validator, _('Answer "Y" to continue or "n" to cancel.'))
end
rescue TTY::Prompt::Reader::InputInterrupt
rescue PDK::CLI::Util::Interview::READER::InputInterrupt
PDK.logger.info opts[:cancel_message] if opts[:cancel_message]
end

Expand Down
4 changes: 2 additions & 2 deletions lib/pdk/cli/util/command_redirector.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'pdk'
require 'tty-prompt'
require 'tty/prompt'

module PDK
module CLI
Expand All @@ -18,7 +18,7 @@ def target_command(cmd)
def run
@prompt.puts _('Did you mean \'%{command}\'?') % { command: pastel.bold(@command) }
@prompt.yes?('-->')
rescue TTY::Prompt::Reader::InputInterrupt
rescue PDK::CLI::Util::Interview::READER::InputInterrupt
nil
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pdk/cli/util/interview.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'tty-prompt'
require 'tty/prompt'
require 'pdk'

module PDK
Expand Down

0 comments on commit a60f378

Please sign in to comment.