Module: Confirm

Extended by:
Confirm
Included in:
Confirm
Defined in:
lib/inquirer/prompts/confirm.rb

Instance Method Summary collapse

Instance Method Details

#prompt(opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/inquirer/prompts/confirm.rb', line 9

def prompt opts = {}
  @question = opts[:message]
  @default  = opts[:default]
  @value    = nil

  render_prompt

  IOHelper.read_char do |char|

    key = IOChar.char_to_key(char)

    if key.casecmp( Inquirer::Style::Confirm.option_true[0] ) == 0
      @value = true
      false
    elsif key.casecmp( Inquirer::Style::Confirm.option_false[0] ) == 0
      @value = false
      false
    elsif key == 'return' && !@default.nil?
      @value = @default
      false
    else
      true
    end
  end

  render_result

  @value
end

#render_promptObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/inquirer/prompts/confirm.rb', line 39

def render_prompt

  options = [
    Inquirer::Style::Confirm.option_true[0].downcase,
    Inquirer::Style::Confirm.option_false[0].downcase
  ]
  if !@default.nil?
    if @default
      options[0].capitalize!
    else
      options[1].capitalize!
    end
  end

  # start with the question prefix
  prompt = Inquirer::Style.question_prefix

  prompt += Inquirer::Style::Confirm.question % @question

  prompt += Inquirer::Style::Confirm.options % options

  prompt += ' '

  IOHelper.render( prompt )
end

#render_resultObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/inquirer/prompts/confirm.rb', line 65

def render_result

  # start with the question prefix
  result = Inquirer::Style.question_prefix

  result += Inquirer::Style::Confirm.question % @question

  response = nil
  if !@value.nil?
    if @value
      response = Inquirer::Style::Confirm.option_true
    else
      response = Inquirer::Style::Confirm.option_false
    end

    result += Inquirer::Style::Confirm.response % response
  end

  result += IOChar.newline

  # flush previous data
  IOHelper.clear

  # rerender result
  IOHelper.render( result )
end