Class: TTY::Prompt::MaskQuestion

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

Constant Summary collapse

DELETE_KEYS =

Names for delete keys

i[backspace delete].freeze

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):

  • :mask (String)

API:

  • public



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

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

Instance Method Details

#keyenter(_event) ⇒ Object



41
42
43
# File 'lib/tty/prompt/mask_question.rb', line 41

def keyenter(_event)
  @done_masked = true
end

#keypress(event) ⇒ Object



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

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



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

def keyreturn(_event)
  @done_masked = true
end

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

Set character for masking the STDIN input

Parameters:

  • (defaults to: (not_set = true))

Returns:

API:

  • public



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

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tty/prompt/mask_question.rb', line 79

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



71
72
73
74
# File 'lib/tty/prompt/mask_question.rb', line 71

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

API:

  • private



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

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