Module: CliTool::StdinOut::ClassMethods

Defined in:
lib/cli_tool/stdin_out.rb

Instance Method Summary collapse

Instance Method Details

#colorize(text, *color) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cli_tool/stdin_out.rb', line 134

def colorize(text, *color)

  # Determine what to colors we should use
  color = [:reset] if color.empty?
  color = color.flatten

  # Prepare the colorizing prefix for the text
  prefix = color.inject('') do |o, c|
    ANSI_COLORS[c] ? o << "\e[#{ANSI_COLORS[c]}m" : o
  end

  suffix = "\e[0m"

  # Allow color nesting
  text = text.gsub(suffix, "#{suffix}#{prefix}")
  "#{prefix}#{text}#{suffix}" # Add color
end

#confirm(message, color = :reset, default = :n, timer = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cli_tool/stdin_out.rb', line 109

def confirm(message, color = :reset, default = :n, timer = nil)

  # Handle the default value
  default = "#{default}".downcase[0..0].to_sym

  # Get the prompt
  prompt = if default == :y
     'Y/n'
  else
    'y/N'
  end

  begin
    # Prompt for answer
    result = input("#{message} [#{prompt}]", color, timer).strip
    result = result.empty? ? default : result.downcase[0..0].to_sym
    raise MissingInput unless [:y, :n].include?(result)
  rescue MissingInput
    puts "Sorry that input was not accepted", :red
    retry
  end

  result
end

#input(message = '', color = :reset, timer = nil, default = nil) ⇒ Object



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
# File 'lib/cli_tool/stdin_out.rb', line 74

def input(message = '', color = :reset, timer = nil, default = nil)

  # Prompt for input
  print("#{message} ", color)

  # Get the input from the CLI
  if block_given? && yield == :noecho
    gets = Proc.new do
      STDIN.noecho(&:gets).strip
      print "\n"
    end
  else
    gets = Proc.new do
      STDIN.gets.strip
    end
  end

  # Handle timing out
  result = begin
    if timer
      Timeout::timeout(timer, &gets)
    else
      gets.call
    end
  rescue Timeout::Error # Verify that this is correct?
    default
  end

  result
end

#password(*a) ⇒ Object



105
106
107
# File 'lib/cli_tool/stdin_out.rb', line 105

def password(*a)
  input(*a) { :noecho }
end


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cli_tool/stdin_out.rb', line 62

def print(text, color = :reset, timer = nil)

  # Process information for ANSI color codes
  super(colorize(text, color))

  # Sleep after displaying the message
  if timer
    puts(colorize("Sleeping for #{timer} seconds...", color))
    sleep(timer)
  end
end

#puts(text, color = :reset, timer = nil) ⇒ Object

Handle putsing of information (supports color and sleeps)



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cli_tool/stdin_out.rb', line 50

def puts(text, color = :reset, timer = nil)

  # Process information for ANSI color codes
  super(colorize(text, color))

  # Sleep after displaying the message
  if timer
    puts(colorize("Sleeping for #{timer} seconds...", color))
    sleep(timer)
  end
end