Module: MotionProvisioning::Utils

Defined in:
lib/motion-provisioning/utils.rb

Class Method Summary collapse

Class Method Details

.ask(what, question) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/motion-provisioning/utils.rb', line 13

def ask(what, question)
  what = "\e[1m" + what.rjust(10) + "\e[0m" # bold
  $stderr.print(what(what) + ' ' + question + ' ')
  $stderr.flush

  result = $stdin.gets
  result.chomp! if result
  result
end

.ask_password(what, question) ⇒ Object



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
# File 'lib/motion-provisioning/utils.rb', line 23

def ask_password(what, question)
  require 'io/console' # needed for noecho

  # Save current buffering mode
  buffering = $stderr.sync

  # Turn off buffering
  $stderr.sync = true
  `stty -icanon`

  begin
    $stderr.print(what(what) + ' ' + question + ' ')
    $stderr.flush
    pw = ""

    $stderr.noecho do
      while ( char = $stdin.getc ) != "\n" # break after [Enter]
        putc "*"
        pw << char
      end
    end
  ensure
    print "\n"
  end

  # Restore original buffering mode
  $stderr.sync = buffering

  `stty -icanon`
  pw
end

.log(what, msg) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/motion-provisioning/utils.rb', line 4

def log(what, msg)
  require 'thread'
  @print_mutex ||= Mutex.new
  # Because this method can be called concurrently, we don't want to mess any output.
  @print_mutex.synchronize do
    $stderr.puts(what(what) + ' ' + msg)
  end
end

.what(what) ⇒ Object



55
56
57
# File 'lib/motion-provisioning/utils.rb', line 55

def what(what)
  "\e[1m" + what.rjust(10) + "\e[0m" # bold
end