Class: Termtter::StdOut

Inherits:
Hook
  • Object
show all
Defined in:
lib/plugins/defaults/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.



98
99
100
# File 'lib/plugins/defaults/stdout.rb', line 98

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

Instance Method Details

#call(statuses, event) ⇒ Object



102
103
104
# File 'lib/plugins/defaults/stdout.rb', line 102

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

#color_of_screen_name(screen_name) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/plugins/defaults/stdout.rb', line 196

def color_of_screen_name(screen_name)
  return color_of_screen_name_cache[screen_name] if
    color_of_screen_name_cache.key?(screen_name)
  num = screen_name_to_hash(screen_name)
  color = config.plugins.stdout.instance_eval {
    sweets.include?(screen_name) ?
      sweet_color : colors[num % colors.size]
  }
  color_of_screen_name_cache[screen_name] = color
  color_of_screen_name_cache[screen_name]
end

#color_of_screen_name_cacheObject



213
214
215
# File 'lib/plugins/defaults/stdout.rb', line 213

def color_of_screen_name_cache
  @color_of_screen_name_cache ||= {}
end

#color_of_user(user) ⇒ Object



192
193
194
# File 'lib/plugins/defaults/stdout.rb', line 192

def color_of_user(user)
  color_of_screen_name(user.screen_name)
end

#colorize_users(text) ⇒ Object



185
186
187
188
189
190
# File 'lib/plugins/defaults/stdout.rb', line 185

def colorize_users(text)
  text.gsub(/@([0-9A-Za-z_]+)/) do |i|
    color = color_of_screen_name($1)
    "<#{color}>#{i}</#{color}>"
  end
end

#escape(data) ⇒ Object



217
218
219
# File 'lib/plugins/defaults/stdout.rb', line 217

def escape(data)
  data.gsub(/[:cntrl:]/) {|c| c == "\n" ? c : c.dump[1...-1]}.untaint
end

#inspectObject



106
107
108
# File 'lib/plugins/defaults/stdout.rb', line 106

def inspect
  "#<Termtter::StdOut @name=#{@name}, @points=#{@points.inspect}, @exec_proc=#{@exec_proc.inspect}>"
end


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/plugins/defaults/stdout.rb', line 110

def print_statuses(statuses, event, sort = true, time_format = nil)
  return unless statuses and statuses.first
  time_format ||= Termtter::Client.time_format_for statuses

  output_text = ''
  statuses.each do |s|
    output_text << status_line(s, time_format, event)
  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
    Termtter::Client.clear_line
    print output_text
  end
end

#screen_name_to_hash(screen_name) ⇒ Object



208
209
210
211
# File 'lib/plugins/defaults/stdout.rb', line 208

def screen_name_to_hash(screen_name)
  config.plugins.stdout.screen_name_to_hash_proc.
    call(screen_name)
end

#status_line(s, time_format, event, indent = 0) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/plugins/defaults/stdout.rb', line 133

def status_line(s, time_format, event, indent = 0)
  return '' unless s
  text = escape(TermColor.escape(s.text))
  color = color_of_user(s.user)
  status_id = Termtter::Client.data_to_typable_id(s.id)
  reply_to_status_id =
    if s.in_reply_to_status_id
      Termtter::Client.data_to_typable_id(s.in_reply_to_status_id)
    else
      nil
    end

  retweeted_status_id =
    if s.retweeted_status
      Termtter::Client.data_to_typable_id(s.retweeted_status.id)
    else
      nil
    end

  time = "(#{Time.parse(s.created_at).localtime.strftime(time_format)})"
  source =
    case s.source
    when />(.*?)</ then $1
    when 'web' then 'web'
    end

  text = colorize_users(text)
  text = Client.get_hooks(:pre_coloring).inject(text) {|result, hook|
    #Termtter::Client.logger.debug "stdout status_line: call hook :pre_coloring #{hook.inspect}"
    hook.call(result, event)
  }
  indent_text = indent > 0 ? eval(config.plugins.stdout.indent_format) : ''
  erbed_text = ERB.new(config.plugins.stdout.timeline_format).result(binding)
  erbed_text = Client.get_hooks(:pre_output).inject(erbed_text) {|result, hook|
    #Termtter::Client.logger.debug "stdout status_line: call hook :pre_output #{hook.inspect}"
    hook.call(result, event)
  }
  text = TermColor.unescape(TermColor.parse(erbed_text) + "\n")
  if config.plugins.stdout.show_reply_chain && s.in_reply_to_status_id
    indent += 1
    unless indent > config.plugins.stdout.max_indent_level
      begin
        if status = Termtter::API.twitter.cached_status(s.in_reply_to_status_id)
          text << status_line(status, time_format, event, indent)
        end
      rescue Rubytter::APIError
      end
    end
  end
  text
end