lib/tty/prompt/question.rb in tty-prompt-0.11.0 vs lib/tty/prompt/question.rb in tty-prompt-0.12.0
- old
+ new
@@ -14,11 +14,16 @@
#
# @api public
class Question
include Checks
- UndefinedSetting = Module.new
+ UndefinedSetting = Class.new do
+ def to_s
+ "undefined"
+ end
+ alias inspect to_s
+ end
# Store question message
# @api public
attr_reader :message
@@ -36,11 +41,10 @@
@required = options.fetch(:required) { false }
@echo = options.fetch(:echo) { true }
@in = options.fetch(:in) { UndefinedSetting }
@modifier = options.fetch(:modifier) { [] }
@validation = options.fetch(:validation) { UndefinedSetting }
- @read = options.fetch(:read) { UndefinedSetting }
@convert = options.fetch(:convert) { UndefinedSetting }
@active_color = options.fetch(:active_color) { @prompt.active_color }
@help_color = options.fetch(:help_color) { @prompt.help_color }
@error_color = options.fetch(:error_color) { :red }
@messages = Utils.deep_copy(options.fetch(:messages) { { } })
@@ -101,93 +105,94 @@
#
# @api private
def render
@errors = []
until @done
- lines = render_question
- result = process_input
+ question = render_question
+ @prompt.print(question)
+ result = process_input(question)
if result.failure?
@errors = result.errors
- render_error(result.errors)
+ @prompt.print(render_error(result.errors))
else
@done = true
end
- refresh(lines)
+ @prompt.print(refresh(question.lines.count))
end
- render_question
+ @prompt.print(render_question)
convert_result(result.value)
end
# Render question
#
+ # @return [String]
+ #
# @api private
def render_question
header = "#{@prefix}#{message} "
if !echo?
header
elsif @done
header += @prompt.decorate("#{@input}", @active_color)
elsif default? && !Utils.blank?(@default)
header += @prompt.decorate("(#{default})", @help_color) + ' '
end
- @prompt.print(header)
- @prompt.puts if @done
-
- header.lines.count + (@done ? 1 : 0)
+ header << "\n" if @done
+ header
end
# Decide how to handle input from user
#
# @api private
- def process_input
- @input = read_input
+ def process_input(question)
+ @input = read_input(question)
if Utils.blank?(@input)
@input = default? ? default : nil
end
@evaluator.(@input)
end
# Process input
#
# @api private
- def read_input
- case @read
- when :keypress
- @prompt.read_keypress
- when :multiline
- @prompt.read_multiline.each(&:chomp!)
- else
- @prompt.read_line(echo: echo).chomp
- end
+ def read_input(question)
+ @prompt.read_line(question, echo: echo).chomp
end
# Handle error condition
#
+ # @return [String]
+ #
# @api private
def render_error(errors)
- errors.each do |err|
+ errors.reduce('') do |acc, err|
newline = (@echo ? '' : "\n")
- @prompt.print(newline + @prompt.decorate('>>', :red) + ' ' + err)
+ acc << newline + @prompt.decorate('>>', :red) + ' ' + err
+ acc
end
end
# Determine area of the screen to clear
#
- # @param [Array[String]] errors
+ # @param [Integer] lines
+ # number of lines to clear
#
+ # @return [String]
+ #
# @api private
def refresh(lines)
+ output = ''
if @done
if @errors.count.zero? && @echo
- @prompt.print(@prompt.cursor.up(lines))
+ output << @prompt.cursor.up(lines)
else
lines += @errors.count
end
else
- @prompt.print(@prompt.cursor.up(lines))
+ output << @prompt.cursor.up(lines)
end
- @prompt.print(@prompt.clear_lines(lines))
+ output + @prompt.clear_lines(lines)
end
# Convert value to expected type
#
# @param [Object] value
@@ -197,16 +202,9 @@
if convert? & !Utils.blank?(value)
Converters.convert(@convert, value)
else
value
end
- end
-
- # Set reader type
- #
- # @api public
- def read(value)
- @read = value
end
# Specify answer conversion
#
# @api public