Module: Geordi::Interaction
- Defined in:
- lib/geordi/interaction.rb
Class Method Summary collapse
-
.announce(text) ⇒ Object
Start your command by
announce-ing what you're about to do. - .confirm_or_cancel(message = 'Continue?', cancel_message = 'Cancelled.', default: 'y') ⇒ Object
-
.fail(text) ⇒ Object
Exit execution with status code 1 and give a short note what happened, e.g.
-
.note(text) ⇒ Object
Any meta information, i.e.
-
.note_cmd(text) ⇒ Object
Like
note, but pink. -
.prompt(text, default = nil, agreement_regex = nil) ⇒ Object
Returns the user's input.
-
.success(text) ⇒ Object
When you're done, inform the user with a
successand a short message. -
.warn(text) ⇒ Object
Like
note, but yellow.
Class Method Details
.announce(text) ⇒ Object
Start your command by announce-ing what you're about to do
9 10 11 12 |
# File 'lib/geordi/interaction.rb', line 9 def announce(text) = "\n# #{text}" puts "\e[4;34m#{}\e[0m" # blue underline end |
.confirm_or_cancel(message = 'Continue?', cancel_message = 'Cancelled.', default: 'y') ⇒ Object
64 65 66 |
# File 'lib/geordi/interaction.rb', line 64 def confirm_or_cancel( = 'Continue?', = 'Cancelled.', default: 'y') prompt(, default, /y|yes/) or fail() end |
.fail(text) ⇒ Object
Exit execution with status code 1 and give a short note what happened, e.g. "Failed" or "Cancelled"
36 37 38 39 40 |
# File 'lib/geordi/interaction.rb', line 36 def fail(text) = "\nx #{text}" puts "\e[31m#{}\e[0m" # red exit(1) end |
.note(text) ⇒ Object
Any meta information, i.e. hints, comments, infos or explanations should
be printed with note.
Please do not use it for command output (data, file contents, lists etc).
17 18 19 |
# File 'lib/geordi/interaction.rb', line 17 def note(text) puts '> ' + text end |
.note_cmd(text) ⇒ Object
Like note, but pink. Use to print (bash) commands.
Also see Util.run!
29 30 31 32 |
# File 'lib/geordi/interaction.rb', line 29 def note_cmd(text) = "> #{text}" puts "\e[36m#{}\e[0m" # cyan end |
.prompt(text, default = nil, agreement_regex = nil) ⇒ Object
Returns the user's input. If agreement_regex is given, returns whether the input matches the regex.
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/geordi/interaction.rb', line 51 def prompt(text, default = nil, agreement_regex = nil) = "#{text} " << "[#{default}] " if default print "\e[35m#{}\e[0m" # pink input = $stdin.gets.strip input = default if input.empty? && default agreement_regex ? !!(input =~ agreement_regex) : input rescue Interrupt fail 'Cancelled.' end |
.success(text) ⇒ Object
When you're done, inform the user with a success and a short message. It
should be a sentence (i.e. ending with [.!?]).
44 45 46 47 |
# File 'lib/geordi/interaction.rb', line 44 def success(text) = "\n> #{text}" puts "\e[32m#{}\e[0m" # green end |
.warn(text) ⇒ Object
Like note, but yellow. Use to warn the user.
22 23 24 25 |
# File 'lib/geordi/interaction.rb', line 22 def warn(text) = "> #{text}" puts "\e[33m#{}\e[0m" # yellow end |