Class: Senrigan::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/senrigan/formatter.rb

Instance Method Summary collapse

Constructor Details

#initializeFormatter

Returns a new instance of Formatter.



7
8
# File 'lib/senrigan/formatter.rb', line 7

def initialize
end

Instance Method Details

#format(entity) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/senrigan/formatter.rb', line 10

def format(entity)
  case entity
  when Entities::Message
    format_message(entity)
  when Entities::MessageChangeEvent
    format_message_change_event(entity)
  else
    entity.inspect
  end
end

#format_attachment(attachment) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/senrigan/formatter.rb', line 66

def format_attachment(attachment)
  lines = []
  lines.push attachment.title.to_s.colorize(:green) unless attachment.title.to_s.empty?
  lines.push attachment.text.to_s.colorize(:green) unless attachment.text.to_s.empty?
  lines.push attachment.title_link.to_s.colorize(:green) unless attachment.title_link.to_s.empty?
  lines.join("\n")
end

#format_header(entity) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/senrigan/formatter.rb', line 46

def format_header(entity)
  [
    "@#{entity.user.name}#{entity.user.is_bot ? ' [App]' : ''}".to_s.colorize(:light_blue),
    'in',
    "#{entity.channel.is_private ? '🔒 ' : '#'}#{entity.channel.name}".colorize(:light_green),
    Time.at(entity.ts.to_i).strftime('%H:%M').colorize(:light_black)
  ].join(' ')
end

#format_message(entity) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/senrigan/formatter.rb', line 26

def format_message(entity)
  lines = []
  lines.push format_header(entity)
  lines.push entity.content unless entity.content.empty?
  entity.attachments.each do |attachment|
    lines.push format_attachment(attachment)
  end
  lines.push format_parent(entity.parent) if entity.parent
  lines.join("\n")
end

#format_message_change_event(entity) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/senrigan/formatter.rb', line 37

def format_message_change_event(entity)
  if entity.edited_message.content == entity.previous_message.content &&
     !entity.edited_message.attachments.empty? && entity.previous_message.attachments.empty?
    entity.edited_message.attachments.map { |a| format_attachment(a) }.join("\n")
  else
    format_message(entity.edited_message)
  end
end

#format_parent(entity) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/senrigan/formatter.rb', line 55

def format_parent(entity)
  lines = []
  lines.push [
    '>',
    "@#{entity.user.name}#{entity.user.is_bot ? ' [App]' : ''}".to_s,
    Time.at(entity.ts.to_i).strftime('%H:%M')
  ].join(' ').colorize(:light_black)
  lines.push "> #{entity.content.split(/\r?\n/).join("\n> ")}".colorize(:light_black)
  lines.join("\n")
end

#format_print(entity) ⇒ Object



21
22
23
24
# File 'lib/senrigan/formatter.rb', line 21

def format_print(entity)
  str = format(entity)
  STDOUT.puts str unless str.empty?
end