Module: MotionProvisioning::Utils
- Defined in:
- lib/motion-provisioning/utils.rb
Defined Under Namespace
Classes: Answer
Class Method Summary collapse
- .ask(what, question) ⇒ Object
- .ask_password(what, question) ⇒ Object
- .log(what, msg) ⇒ Object
- .what(what) ⇒ Object
Class Method Details
.ask(what, question) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/motion-provisioning/utils.rb', line 27 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
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 |
# File 'lib/motion-provisioning/utils.rb', line 37 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
18 19 20 21 22 23 24 25 |
# File 'lib/motion-provisioning/utils.rb', line 18 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
69 70 71 |
# File 'lib/motion-provisioning/utils.rb', line 69 def what(what) "\e[1m" + what.rjust(10) + "\e[0m" # bold end |