summaryrefslogtreecommitdiff
path: root/lib/leap_cli/commands/common.rb
blob: d49490ebd06f840ac602ff6d4794093f0251a3da (plain)
1
2
3
4
5
6
7
8
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
38
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
64
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
91
92
93
94
95
96
97
98
99
100
101
102
103
require 'readline'

module LeapCli; module Commands

  extend LeapCli::LogCommand
  extend LeapCli::Util

  def path(name)
    Path.named_path(name)
  end

  #
  # keeps prompting the user for a numbered choice, until they pick a good one or bail out.
  #
  # block is yielded and is responsible for rendering the choices.
  #
  def numbered_choice_menu(msg, items, &block)
    while true
      say("\n" + msg + ':')
      items.each_with_index(&block)
      say("q. quit")
      index = ask("number 1-#{items.length}> ")
      if index.empty?
        next
      elsif index =~ /q/
        bail!
      else
        i = index.to_i - 1
        if i < 0 || i >= items.length
          bail!
        else
          return i
        end
      end
    end
  end

  def parse_node_list(nodes)
    if nodes.is_a? Config::Object
      Config::ObjectList.new(nodes)
    elsif nodes.is_a? Config::ObjectList
      nodes
    elsif nodes.is_a? String
      manager.filter!(nodes)
    else
      bail! "argument error"
    end
  end

  def say(statement)
    if ends_in_whitespace?(statement)
      $stdout.print(statement)
      $stdout.flush
    else
      $stdout.puts(statement)
    end
  end

  def ask(question, options={})
    default = options[:default]
    if default
      if ends_in_whitespace?(question)
        question = question + "|" + default + "| "
      else
        question = question + "|" + default + "|"
      end
    end
    response = Readline.readline(question, true) # set to false if ever reading passwords.
    if response
      response = response.strip
      if response.empty?
        return default
      else
        return response
      end
    else
      return default
    end
  end

  def agree(question, options={})
    while true
      response = ask(question, options)
      if response.nil?
        say('Please enter "yes" or "no".')
      elsif ["y","yes", "ye"].include?(response.downcase)
        return true
      elsif ["n", "no"].include?(response.downcase)
        return false
      else
        say('Please enter "yes" or "no".')
      end
    end
  end

  private

  # true if str ends in whitespace before a color escape code.
  def ends_in_whitespace?(str)
    /[ \t](\e\[\d+(;\d+)*m)?\Z/ =~ str
  end

end; end