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



39
40
41
# File 'lib/plugins/stdout.rb', line 39

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

Instance Method Details

#call(statuses, event) ⇒ Object



43
44
45
# File 'lib/plugins/stdout.rb', line 43

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


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/plugins/stdout.rb', line 47

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 = ''
  output_text += "\e[1K\e[0G" unless win?
  Curses::init_screen;
  cols = Curses.cols;
  Curses::close_screen
  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)})"
    if s.in_reply_to_status_id
      status += " (reply to #{s.in_reply_to_status_id})"
    end

    id = s.id
    len =  id.to_s.length + 2
    if cols > 0 then
      status = status.truncate_column(cols-len-1).join( "\n" )
      status = status.gsub( /\n/, "\n".ljust(len) )
    end
    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
    output_text << TermColor.parse("<90>-----Fetched on " + Time.now.strftime(time_format) + "</90>\n")
    print output_text
  end
end