Class: Thor::Shell::Basic

Inherits:
Object
  • Object
show all
Defined in:
lib/cinch/bot_template/main/thor.rb

Instance Method Summary collapse

Instance Method Details

#prepare_message(message, *color, paint: true) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cinch/bot_template/main/thor.rb', line 51

def prepare_message(message, *color, paint: true)
  case paint
  when true
    spaces = "  " * padding
    spaces + Paint[message.to_s, *color]
  when false
    spaces = "  " * padding
    spaces + set_color(message.to_s, *color)
  else
    spaces = "  " * padding
    spaces + Paint[message.to_s, *color]
  end

end
Note:

Override Thor::Shell::Basic.print_wrapped

  1. Replace 6 - with 6 spaces

  2. Replace 4 - with 4 spaces

  3. Replace 2 - with 2 spaces

  4. Replace #$ at the beginning of lines

with '\n'


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cinch/bot_template/main/thor.rb', line 88

def print_wrapped(message, options = {})
  message.lstrip!
  message.gsub!(/\n\s+/, "\n")
  message = message.split("\n")
  message.each do |line|
    line.gsub!(/^------/, ' ' * 6) if line[0..5] == '-' * 6
    line.gsub!(/^----/, ' ' * 4) if line[0..3] == '-' * 4
    line.gsub!(/^--/, ' ' * 2) if line[0..1] == '-' * 2
    line.gsub!(/^#\$/, "\n")
    stdout.puts line
  end
end

#say(message = "", color = nil, force_new_line = (message.to_s !~ /( |\t)\Z/), paint: true) ⇒ Object

Say (print) something to the user. If the sentence ends with a whitespace or tab character, a new line is not appended (print + flush). Otherwise are passed straight to puts (behavior got from Highline).

Example

say(“I know you knew that.”)



73
74
75
76
77
78
79
# File 'lib/cinch/bot_template/main/thor.rb', line 73

def say(message = "", color = nil, force_new_line = (message.to_s !~ /( |\t)\Z/), paint: true)
  buffer = prepare_message(message, *color, paint: paint)
  buffer << "\n" if force_new_line && !message.to_s.end_with?("\n")

  stdout.print(buffer)
  stdout.flush
end