Class: Termtter::StdOut

Inherits:
Hook
  • Object
show all
Defined in:
lib/plugins/stdout.rb

Instance Attribute Summary

Attributes inherited from Hook

#exec_proc, #name, #points

Instance Method Summary collapse

Methods inherited from Hook

#match?

Constructor Details

#initializeStdOut

Returns a new instance of StdOut.



19
20
21
# File 'lib/plugins/stdout.rb', line 19

def initialize
  super(:name => :stdout, :points => [:output])
end

Instance Method Details

#call(statuses, event) ⇒ Object



23
24
25
# File 'lib/plugins/stdout.rb', line 23

def call(statuses, event)
  print_statuses(statuses)
end


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/plugins/stdout.rb', line 27

def print_statuses(statuses, sort = true, time_format = nil)
  return unless statuses and statuses.first
  unless time_format
    t0 = Time.now
    t1 = Time.parse(statuses.first[:created_at])
    t2 = Time.parse(statuses.last[:created_at])
    time_format = 
      if [t0.year, t0.month, t0.day] == [t1.year, t1.month, t1.day] \
        and [t1.year, t1.month, t1.day] == [t2.year, t2.month, t2.day]
        '%H:%M:%S'
      else
        '%y/%m/%d %H:%M'
      end
  end

  output_text = ''
  statuses.each do |s|
    text = TermColor.escape(s.text)
    color = config.plugins.stdout.colors[s.user.id.to_i % config.plugins.stdout.colors.size]
    reply_to = s.in_reply_to_status_id ? "(reply to #{s.in_reply_to_status_id})" : nil
    time = "(#{Time.parse(s.created_at).strftime(time_format)})"
    source =
      case s.source
      when />(.*?)</ then $1
      when 'web' then 'web'
      end

    erbed_text = ERB.new(config.plugins.stdout.timeline_format).result(binding)
    output_text << TermColor.parse(erbed_text) + "\n"
  end

  if config.plugins.stdout.enable_pager && ENV['LINES'] && statuses.size > ENV['LINES'].to_i
    file = Tempfile.new('termtter')
    file.print output_text
    file.close
    system "#{config.plugins.stdout.pager} #{file.path}"
    file.close(true)
  else
    print output_text
  end
end