Module: MotionProvisioning::Utils

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

Defined Under Namespace

Classes: Answer

Class Method Summary collapse

Class Method Details

.ask(what, question) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/motion-provisioning/utils.rb', line 29

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
  Answer.new(result)
end

.ask_password(what, question) ⇒ Object



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

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



20
21
22
23
24
25
26
27
# File 'lib/motion-provisioning/utils.rb', line 20

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



71
72
73
# File 'lib/motion-provisioning/utils.rb', line 71

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