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, #quiet, #raw, #refresh, #render, #required, #to_s, #validate, #validation?, #value, #value?

Constructor Details

#initialize(prompt, **options) ⇒ MaskQuestion

Create masked question

Parameters:

  • options (Hash)

Options Hash (**options):

  • :mask (String)

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

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

Instance Method Details

#keyenter(event) ⇒ Object


37
38
39
# File 'lib/tty/prompt/mask_question.rb', line 37

def keyenter(event)
  @done_masked = true
end

#keypress(event) ⇒ Object


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

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


33
34
35
# File 'lib/tty/prompt/mask_question.rb', line 33

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)

28
29
30
31
# File 'lib/tty/prompt/mask_question.rb', line 28

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


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

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


67
68
69
70
# File 'lib/tty/prompt/mask_question.rb', line 67

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


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

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