Skip to content

Instantly share code, notes, and snippets.

@hnagato
Created February 21, 2012 06:31
Show Gist options
  • Save hnagato/1874290 to your computer and use it in GitHub Desktop.
Save hnagato/1874290 to your computer and use it in GitHub Desktop.
location 表示 + screen_name をもうちょいカラフルに
# -*- coding: utf-8 -*-
module Earthquake
module Output
require 'uri'
def puts_items(items)
mark_color = [ config[:colors].sample, 1 ].flatten # random & bold
[items].flatten.reverse_each do |item|
next if output_filters.any? { |f| f.call(item) == false }
if item["text"] && !item["_stream"]
item['_mark'] = ' '.c(mark_color) + item['_mark'].to_s
end
outputs.each do |o|
begin
o[:block].call(item)
rescue => e
error e
end
end
end
end
def color_of_hashtag(hashtag)
if hashtag.delete("^0-9a-zA-Z").empty?
config[:colors][URI.escape(hashtag).delete("^0-9a-zA-Z").to_i(16) % config[:colors].size]
else
color_of(hashtag)
end
end
def indent(length, maxlength = 12)
' ' * (length > maxlength ? 1 : maxlength - length)
end
end
end
Earthquake.init do
output :tweet 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
if !config[:hide_time] && item["created_at"]
info << Time.parse(item["created_at"]).strftime(config[:time_format])
end
if !config[:hide_app_name] && item["source"]
info << (item["source"].u =~ />(.*)</ ? $1 : 'web')
end
id = id2var(item["id"])
text = (item["retweeted_status"] && item["truncated"] ? "RT @#{item["retweeted_status"]["user"]["screen_name"]}: #{item["retweeted_status"]["text"]}" : item["text"]).u
text.gsub!(/\s+/, ' ') unless config[:raw_text]
text.prepend("\n") if config[:raw_text]
text = text.coloring(/@[0-9A-Za-z_]+/) { |i| color_of(i) }
text = text.coloring(/(^#[^\s ]+)|([\s ]+#[^\s ]+)/) { |i| color_of_hashtag(i) }
if config[:expand_url]
entities = (item["retweeted_status"] && item["truncated"]) ? item["retweeted_status"]["entities"] : item["entities"]
if entities
entities.values_at("urls", "media").flatten.compact.each do |entity|
url, expanded_url = entity.values_at("url", "expanded_url")
if url && expanded_url
text = text.sub(url, expanded_url)
end
end
end
end
text = text.coloring(URI.regexp(["http", "https"]), :url)
if item["_highlights"]
item["_highlights"].each do |h|
color = config[:color][:highlight].nil? ? [ color_of(h), 1 ].flatten : :highlight
text = text.coloring(/#{h}/i, color)
end
end
mark = item["_mark"] || ""
screen_name = item["user"]["screen_name"]
location = item["user"]["location"] || "unkonwn"
status = [
"#{mark}" + "#{id}".c(:info),
"#{screen_name.c(color_of(screen_name))}#{indent(screen_name.size)}",
"#{text}",
(item["user"]["protected"] ? "[P]".c(:notice) : nil),
info.join(' - ').c(:info),
location.c(:location)
].compact.join(" ")
puts status
end
end
@hnagato
Copy link
Author

hnagato commented Feb 21, 2012

~/.earthquake/config

# Colors
## Light
if ENV['COLORFGBG'] == '11;15'
  Earthquake.config[:colors] = [
      1..6, 8..14, 16..38, 52..74, 88..105, 124..140, 160..176, 196..215
  ].map(&:to_a).flatten.map {|c| [38, 5, c] }

  Earthquake.config[:color] = {
    :info      => [38, 5, 10],
    :notice    => [38, 5, 31],
    :event     => [38, 5, 42],
    :url       => [38, 5, 4, 4],
    :location  => [38, 5, 100]
#    :highlight => [38, 5, 9, 1]
  }
## Dark
else
  Earthquake.config[:colors] = [
    1..6, 9..14, 104..123, 130..159, 165..195, 200..230, 245..245
  ].map(&:to_a).flatten.map {|c| [38, 5, c] }
#    [ 31..38, 91..96 ].map(&:to_a).flatten
  Earthquake.config[:color] = {
    :info      => [38, 5, 11],
    :notice    => [38, 5 ,5],
    :event     => [38, 5, 9],
    :url       => [38, 5, 4, 4],
    :location  => [38, 5, 250]
#    :highlight => [38, 5, 9, 1]
  }
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment