Class: Termtter::StdOut
- Inherits:
-
Hook
- Object
- Hook
- Termtter::StdOut
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
#initialize ⇒ StdOut
Returns a new instance of StdOut.
96
97
98
|
# File 'lib/plugins/defaults/stdout.rb', line 96
def initialize
super(:name => :stdout, :points => [:output])
end
|
Instance Method Details
#call(statuses, event) ⇒ Object
100
101
102
|
# File 'lib/plugins/defaults/stdout.rb', line 100
def call(statuses, event)
print_statuses(statuses, event)
end
|
#color_of_screen_name(screen_name) ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/plugins/defaults/stdout.rb', line 194
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_cache ⇒ Object
211
212
213
|
# File 'lib/plugins/defaults/stdout.rb', line 211
def color_of_screen_name_cache
@color_of_screen_name_cache ||= {}
end
|
#color_of_user(user) ⇒ Object
190
191
192
|
# File 'lib/plugins/defaults/stdout.rb', line 190
def color_of_user(user)
color_of_screen_name(user.screen_name)
end
|
#colorize_users(text) ⇒ Object
183
184
185
186
187
188
|
# File 'lib/plugins/defaults/stdout.rb', line 183
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
215
216
217
|
# File 'lib/plugins/defaults/stdout.rb', line 215
def escape(data)
data.gsub(/[:cntrl:]/) {|c| c == "\n" ? c : c.dump[1...-1]}.untaint
end
|
#inspect ⇒ Object
104
105
106
|
# File 'lib/plugins/defaults/stdout.rb', line 104
def inspect
"#<Termtter::StdOut @name=#{@name}, @points=#{@points.inspect}, @exec_proc=#{@exec_proc.inspect}>"
end
|
#print_statuses(statuses, event, sort = true, time_format = nil) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/plugins/defaults/stdout.rb', line 108
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. &&
ENV['LINES'] &&
statuses.size > ENV['LINES'].to_i
file = Tempfile.new('termtter')
file.print output_text
file.close
system "#{config.plugins.stdout.} #{file.path}"
file.close(true)
else
Termtter::Client.clear_line
print output_text
end
end
|
#screen_name_to_hash(screen_name) ⇒ Object
206
207
208
209
|
# File 'lib/plugins/defaults/stdout.rb', line 206
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
131
132
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
|
# File 'lib/plugins/defaults/stdout.rb', line 131
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
=
if s.
Termtter::Client.data_to_typable_id(s..id)
else
nil
end
time = "(#{Time.parse(s.created_at).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|
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|
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..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
|