class TTY::Prompt::MaskQuestion
Constants
- DELETE_KEYS
Names for delete keys
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 16 def initialize(prompt, **options) super @mask = options.fetch(:mask) { @prompt.symbols[:dot] } @done_masked = false @failure = false end
Public Instance Methods
keyenter(_event)
click to toggle source
# File lib/tty/prompt/mask_question.rb, line 40 def keyenter(_event) @done_masked = true end
keypress(event)
click to toggle source
# File lib/tty/prompt/mask_question.rb, line 44 def keypress(event) if DELETE_KEYS.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 36 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 30 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 78 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 70 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 55 def render_question header = ["#{@prefix}#{message} "] if echo? masked = @mask.to_s * @input.to_s.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.join end