Module: SublInit::CLI::IO
- Defined in:
- lib/sublinit/cli/io.rb
Constant Summary collapse
- POSITIVE_ANWSER_REGEXP =
/^(Y|y|yes)$/i.freeze
- COLOR_CODE_MAPPING =
{ white: 0, red: 31, green: 32, yellow: 33, purple: 34, pink: 35, blue: 36 }.freeze
Class Method Summary collapse
- .ask(question, options = {}) ⇒ Object
- .ask_boolean(message, options = {}) ⇒ Object
- .say(message, color: :white, newline: true) ⇒ Object
Class Method Details
.ask(question, options = {}) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/sublinit/cli/io.rb', line 24 def ask(question, = {}) say(question, **) say('Answer: ', newline: false) answer = $stdin.gets.strip block_given? ? yield(answer) : answer end |
.ask_boolean(message, options = {}) ⇒ Object
18 19 20 21 22 |
# File 'lib/sublinit/cli/io.rb', line 18 def ask_boolean(, = {}) ask("#{message} (Y/n)", **) do |answer| answer.match?(POSITIVE_ANWSER_REGEXP) end end |
.say(message, color: :white, newline: true) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/sublinit/cli/io.rb', line 33 def say(, color: :white, newline: true) color_code = COLOR_CODE_MAPPING.fetch(color, 0) = "\e[#{color_code}m#{message}\e[0m" newline ? puts() : print() end |