Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
139 lines (138 sloc) 4.03 KB
diff --git a/lib/plugins/another_stdout.rb b/lib/plugins/another_stdout.rb
new file mode 100755
index 0000000..c4f2f8a
--- /dev/null
+++ b/lib/plugins/another_stdout.rb
@@ -0,0 +1,132 @@
+# -*- coding: utf-8 -*-
+module Termtter
+
+ class AnotherStdOut < Hook
+ def initialize
+ # NOTE: overwrite :stdout hook
+ super(:name => :stdout, :points => [:output])
+ end
+
+ def call(statuses, event)
+ print_statuses(statuses, event)
+ end
+
+ private
+ def print_statuses(statuses, event, sort = true, time_format = nil)
+ return unless statuses and statuses.first
+ time_format ||= Termtter::Client.time_format_for statuses
+
+ outputs = []
+ not_replies = []
+ statuses.each do |s|
+ not_replies << outputs.size
+ outputs.concat status_line(s, time_format, event)
+ end
+
+ justs = config.plugins.stdout.timeline_format.scan(/\|\|([><])/).map{|v|v == '<' ? :ljust : :rjust}
+ justs.unshift :ljust
+
+ format_column(outputs, not_replies, justs)
+
+ outputs.map! do |t|
+ t = Client.get_hooks(:pre_coloring).inject(t){|result, hook| hook.call(result, event)}
+ t = TermColor.parse(t)
+ TermColor.unescape(t)
+ end
+
+ if config.plugins.stdout.enable_pager && ENV['LINES'] && statuses.size > ENV['LINES'].to_i
+ file = Tempfile.new('termtter')
+ file.puts outputs
+ file.close
+ system "#{config.plugins.stdout.pager} #{file.path}"
+ file.close(true)
+ else
+ puts outputs
+ end
+ end
+
+ def uncolored(str)
+ str.gsub(/<\/?[^>]+>/, '')
+ end
+
+ def format_column(lines, not_replies, justs)
+ array = not_replies.map{|i|lines[i]}
+
+ cols = array.first.size
+ ws = [0] * cols
+
+ ws = array.inject(ws){|r,v|[r, v].transpose.map{|max, col|
+ [max, uncolored(col).size].max}}
+
+ ws[ws.length-1] = 0
+ @ws = @ws.nil? ? ws : @ws.zip(ws).map(&:max)
+
+ not_replies.each do |i|
+ v = lines[i]
+ left = 0
+ lines[i] = [@ws, v, justs].transpose.map do |w, s, just|
+ first = true
+ left += w
+ s.map do |line|
+ adjusted = line.__send__(just, w + line.size - uncolored(line).size)
+ margin = first ? '' : ' ' * (left - w)
+ first = false
+ margin + adjusted
+ end
+ end
+ end
+
+ lines.map!{|v|v.join}
+ end
+
+ # [status, in_reply_to...]
+ def status_line(s, time_format, event, indent = 0)
+ return [] unless s
+ text = TermColor.escape(s.text)
+ color = config.plugins.stdout.colors[s.user.id.to_i % config.plugins.stdout.colors.size]
+ status_id = Termtter::Client.data_to_typable_id(s.id)
+ reply_to_status_id =
+ if s.in_reply_to_status_id.nil?
+ nil
+ else
+ Termtter::Client.data_to_typable_id(s.in_reply_to_status_id)
+ end
+
+ time = "#{Time.parse(s.created_at).strftime(time_format)}"
+ time = Client.get_hooks(:prepare_time).inject(time){|result, hook| hook.call(result, event)}
+
+ source =
+ case s.source
+ when />(.*?)</ then $1
+ when 'web' then 'web'
+ end
+
+ source = Client.get_hooks(:prepare_source).inject(source){|result, hook| hook.call(result, event)}
+ screen_name = Client.get_hooks(:prepare_screenname).inject(s.user.screen_name){|result, hook|
+ hook.call(result, event)
+ }
+
+ formats = config.plugins.stdout.timeline_format.split(/\|\|[><]/)
+
+ erbed_texts = formats.map do |format|
+ ERB.new(format).result(binding)
+ end
+
+ if indent > 0
+ indent_text = "#{' ' * (indent - 1)} ┗ "
+ erbed_texts = [indent_text, erbed_texts.join.split(/[\r\n]/).join]
+ end
+
+ texts = [erbed_texts]
+
+ if config.plugins.stdout.show_as_thread && s.in_reply_to_status_id
+ texts.concat status_line(Status[s.in_reply_to_status_id], time_format, event, indent + 1)
+ end
+
+ texts
+ end
+ end
+
+ Client.register_hook(AnotherStdOut.new)
+
+end
Something went wrong with that request. Please try again.