Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
125 lines (116 sloc) 4.28 KB
diff --git a/lib/plugins/defaults/stdout.rb b/lib/plugins/defaults/stdout.rb
index 26e1f6f..54ef357 100755
--- a/lib/plugins/defaults/stdout.rb
+++ b/lib/plugins/defaults/stdout.rb
@@ -7,7 +7,7 @@ require 'tempfile'
config.plugins.stdout.set_default(:colors, (31..36).to_a + (91..96).to_a)
config.plugins.stdout.set_default(
:timeline_format,
- '<90>(<%=time%>) [<%=status_id%>]</90> <<%=color%>><%=s.user.screen_name%>: <%=text%></<%=color%>> ' +
+ '<90>(<%=time%>) [<%=status_id%>]</90> ||><<%=color%>><%=s.user.screen_name%></<%=color%>>: ||<<%=text%> ' +
'<90><%=reply_to_status_id ? " (reply_to [#{reply_to_status_id}]) " : ""%><%=source%><%=s.user.protected ? "[P]" : ""%></90>'
)
config.plugins.stdout.set_default(:time_format_today, '%H:%M:%S')
@@ -83,28 +83,70 @@ module Termtter
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
- output_text = ''
+ outputs = []
+ not_replies = []
statuses.each do |s|
- output_text << status_line(s, time_format, event)
+ 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)
+
if config.plugins.stdout.enable_pager && ENV['LINES'] && statuses.size > ENV['LINES'].to_i
file = Tempfile.new('termtter')
- file.print output_text
+ file.puts outputs
file.close
system "#{config.plugins.stdout.pager} #{file.path}"
file.close(true)
else
- print output_text
+ puts outputs
+ end
+ end
+
+ def uncolored(str)
+ str.gsub(/\e\[([0-9]+m)/, '')
+ 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
+
+ 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
+ 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)
@@ -129,17 +171,27 @@ module Termtter
hook.call(result, event)
}
- erbed_text = ERB.new(config.plugins.stdout.timeline_format).result(binding)
- indent_text = indent > 0 ? "#{' ' * (indent - 1)} ┗ " : ''
+ formats = config.plugins.stdout.timeline_format.split(/\|\|[><]/)
- erbed_text = Client.get_hooks(:pre_coloring).inject(erbed_text){|result, hook| hook.call(result, event)}
+ erbed_texts = formats.map do |format|
+ t = ERB.new(format).result(binding)
+ t = Client.get_hooks(:pre_coloring).inject(t){|result, hook| hook.call(result, event)}
+ t = TermColor.parse(t)
+ TermColor.unescape(t)
+ end
+
+ if indent > 0
+ indent_text = "#{' ' * (indent - 1)} ┗ "
+ erbed_texts = [indent_text, erbed_texts.join.split(/[\r\n]/).join]
+ end
+
+ texts = [erbed_texts]
- text = TermColor.parse(indent_text + erbed_text) + "\n"
- text = TermColor.unescape(text)
if config.plugins.stdout.show_as_thread && s.in_reply_to_status_id
- text << status_line(Status[s.in_reply_to_status_id], time_format, event, indent + 1)
+ texts.concat status_line(Status[s.in_reply_to_status_id], time_format, event, indent + 1)
end
- text
+
+ texts
end
end