Module: Geordi::Interaction

Instance Method Summary collapse

Instance Method Details

#announce(text) ⇒ Object

Start your command by ‘announce`-ing what you’re about to do



7
8
9
10
# File 'lib/geordi/interaction.rb', line 7

def announce(text)
  message = "\n# #{text}"
  puts "\e[4;34m#{message}\e[0m" # blue underline
end

#fail(text) ⇒ Object

Exit execution with status code 1 and give a short note what happened, e.g. “Failed” or “Cancelled”



33
34
35
36
37
# File 'lib/geordi/interaction.rb', line 33

def fail(text)
  message = "\nx #{text}"
  puts "\e[31m#{message}\e[0m" # red
  exit(1)
end

#note(text) ⇒ Object

Any hints, comments, infos or explanations should be ‘note`d. Please do not print any output (data, file contents, lists) with `note`.



14
15
16
# File 'lib/geordi/interaction.rb', line 14

def note(text)
  puts '> ' + text
end

#note_cmd(text) ⇒ Object

Like ‘note`, but pink. Use to print (bash) commands. Also see Util.system!



26
27
28
29
# File 'lib/geordi/interaction.rb', line 26

def note_cmd(text)
  message = "> #{text}"
  puts "\e[35m#{message}\e[0m" # pink
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.



53
54
55
56
57
58
59
60
61
62
# File 'lib/geordi/interaction.rb', line 53

def prompt(text, default = nil, agreement_regex = nil)
  message = "#{text} "
  message << "[#{default}] " if default

  print "\e[36m#{message}\e[0m" # cyan
  input = $stdin.gets.strip
  input = default if input.empty? && default

  agreement_regex ? !!(input =~ agreement_regex) : input
end

#strip_heredoc(string) ⇒ Object



45
46
47
48
49
# File 'lib/geordi/interaction.rb', line 45

def strip_heredoc(string)
  leading_whitespace = (string.match(/\A( +)[^ ]+/) || [])[1]
  string.gsub! /^#{leading_whitespace}/, '' if leading_whitespace
  string
end

#success(text) ⇒ Object

When you’re done, inform the user with a ‘success` and a short message



40
41
42
43
# File 'lib/geordi/interaction.rb', line 40

def success(text)
  message = "\n> #{text}"
  puts "\e[32m#{message}\e[0m" # green
end

#warn(text) ⇒ Object

Like ‘note`, but yellow. Use to warn the user.



19
20
21
22
# File 'lib/geordi/interaction.rb', line 19

def warn(text)
  message = "> #{text}"
  puts "\e[33m#{message}\e[0m" # yellow
end