class TTY::Prompt::MaskQuestion
Public Class Methods
new(prompt, options = {})
click to toggle source
Create masked question
@param [Hash] options @option options [String] :mask
@api public
Calls superclass method
# File lib/tty/prompt/mask_question.rb, line 14 def initialize(prompt, options = {}) super @mask = options.fetch(:mask) { Symbols.symbols[:dot] } @done_masked = false @failure = false @prompt.subscribe(self) end
Public Instance Methods
keyenter(event)
click to toggle source
# File lib/tty/prompt/mask_question.rb, line 38 def keyenter(event) @done_masked = true end
keypress(event)
click to toggle source
# File lib/tty/prompt/mask_question.rb, line 42 def keypress(event) if [:backspace, :delete].include?(event.key.name) @input.chop! unless @input.empty? elsif event.value =~ /^[^\e\n\r]/ @input += event.value end end
keyreturn(event)
click to toggle source
# File lib/tty/prompt/mask_question.rb, line 34 def keyreturn(event) @done_masked = true end
mask(char = (not_set = true))
click to toggle source
Set character for masking the STDIN input
@param [String] char
@return [self]
@api public
# File lib/tty/prompt/mask_question.rb, line 29 def mask(char = (not_set = true)) return @mask if not_set @mask = char end
read_input(question)
click to toggle source
Read input from user masked by character
@private
# File lib/tty/prompt/mask_question.rb, line 76 def read_input(question) @done_masked = false @failure = false @input = '' @prompt.print(question) until @done_masked @prompt.read_keypress question = render_question total_lines = @prompt.count_screen_lines(question) @prompt.print(@prompt.clear_lines(total_lines)) @prompt.print(render_question) end @prompt.puts @input end
render_error(errors)
click to toggle source
Calls superclass method
# File lib/tty/prompt/mask_question.rb, line 68 def render_error(errors) @failure = !errors.empty? super end
render_question()
click to toggle source
Render question and input replaced with masked character
@api private
# File lib/tty/prompt/mask_question.rb, line 53 def render_question header = "#{@prefix}#{message} " if echo? masked = "#{@mask * "#{@input}".length}" if @done_masked && !@failure masked = @prompt.decorate(masked, @active_color) elsif @done_masked && @failure masked = @prompt.decorate(masked, @error_color) end header += masked end header << "\n" if @done header end