# encoding: UTF-8 module Earthquake module Output def output_filters @output_filters ||= [] end def output_filter(&block) output_filters << block end def outputs @outputs ||= [] end def output(&block) if block outputs << block else return if item_queue.empty? insert do while item = item_queue.shift item["_stream"] = true puts_items(item) end end end end def puts_items(items) [items].flatten.reverse_each do |item| next if output_filters.any? { |f| f.call(item) == false } outputs.each do |o| begin o.call(item) rescue => e error e end end end end def insert(*messages) clear_line puts messages unless messages.empty? yield if block_given? ensure Readline.refresh_line end def clear_line print "\e[0G" + "\e[K" end def color_of(screen_name) config[:colors][screen_name.to_i(36) % config[:colors].size] end end init do outputs.clear output_filters.clear config[:colors] ||= (31..36).to_a + (91..96).to_a config[:color] ||= {} config[:color].reverse_merge!( :info => 90, :notice => 31, :event => 42, :url => [4, 36] ) output do |item| next unless item["text"] info = [] if item["in_reply_to_status_id"] info << "(reply to #{id2var(item["in_reply_to_status_id"])})" elsif item["retweeted_status"] info << "(retweet of #{id2var(item["retweeted_status"]["id"])})" end info << Time.parse(item["created_at"]).strftime(config[:time_format]) if item["_detail"] && item["source"] info << (item["source"].u =~ />(.*) #{item["target"]["screen_name"]}" when "favorite", "unfavorite" puts "[#{item["event"]}]".c(:event) + " #{item["source"]["screen_name"]} => #{item["target"]["screen_name"]} : #{item["target_object"]["text"].u}" when "delete" puts "[deleted]".c(:event) + " #{item["delete"]["status"]["id"]}" else if config[:debug] ap item end end end end extend Output end