Class: TTY::Prompt::MaskQuestion

Inherits:
Question
  • Object
show all
Defined in:
lib/tty/prompt/mask_question.rb

Constant Summary

Constants inherited from Question

Question::UndefinedSetting

Instance Attribute Summary

Attributes inherited from Question

#message, #messages, #modifier, #validation

Instance Method Summary collapse

Methods inherited from Question

#call, #convert, #convert?, #convert_result, #default, #default?, #echo, #in, #in?, #inspect, #message_for, #modify, #process_input, #raw, #refresh, #render, #required, #to_s, #validate, #validation?

Constructor Details

#initialize(prompt, options = {}) ⇒ MaskQuestion

Create masked question

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :mask (String)


15
16
17
18
19
20
21
# File 'lib/tty/prompt/mask_question.rb', line 15

def initialize(prompt, options = {})
  super
  @mask        = options.fetch(:mask) { Symbols.symbols[:dot] }
  @done_masked = false
  @failure     = false
  @prompt.subscribe(self)
end

Instance Method Details

#keyenter(event) ⇒ Object



39
40
41
# File 'lib/tty/prompt/mask_question.rb', line 39

def keyenter(event)
  @done_masked = true
end

#keypress(event) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/tty/prompt/mask_question.rb', line 43

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) ⇒ Object



35
36
37
# File 'lib/tty/prompt/mask_question.rb', line 35

def keyreturn(event)
  @done_masked = true
end

#mask(char = (not_set = true)) ⇒ self

Set character for masking the STDIN input

Parameters:

  • char (String) (defaults to: (not_set = true))

Returns:

  • (self)


30
31
32
33
# 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) ⇒ Object

Read input from user masked by character



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tty/prompt/mask_question.rb', line 77

def read_input(question)
  @done_masked = false
  @failure = false
  @input = ''
  until @done_masked
    @prompt.read_keypress
    @prompt.print(@prompt.clear_line)
    @prompt.print(render_question)
  end
  @prompt.puts
  @input
end

#render_error(errors) ⇒ Object



69
70
71
72
# File 'lib/tty/prompt/mask_question.rb', line 69

def render_error(errors)
  @failure = !errors.empty?
  super
end

#render_questionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Render question and input replaced with masked character



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tty/prompt/mask_question.rb', line 54

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