Skip to content

Commit

Permalink
Add long formatting to streaming commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed May 8, 2012
1 parent 04ad89d commit aa9d1f5
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions lib/t/stream.rb
Expand Up @@ -9,20 +9,38 @@ module T
class Stream < Thor
include T::Printable

STATUS_HEADINGS_FORMATTING = [
"%-18s", # Add padding to maximum length of a Tweet ID
"%-12s", # Add padding to length of a timestamp formatted with ls_formatted_time
"%-20s", # Add padding to maximum length of a Twitter screen name
"%s", # Last element does not need special formatting
]

def initialize(*)
super
@rcfile = RCFile.instance
end

desc "all", "Stream a random sample of all Tweets (Control-C to stop)"
method_option "csv", :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
method_option "long", :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
def all
if options['csv']
say STATUS_HEADINGS.to_csv
client.on_inited do
if options['csv']
say STATUS_HEADINGS.to_csv
elsif options['long'] && STDOUT.tty?
say STATUS_HEADINGS.size.times.map do |index|
STATUS_HEADINGS_FORMATTING[index] % STATUS_HEADINGS[index]
end
end
end
client.on_timeline_status do |status|
if options['csv']
print_csv_status(status)
elsif options['long']
print_table build_long_status(status).each_with_index.map do |element, index|
STATUS_HEADINGS_FORMATTING[index] % element
end
else
print_status(status)
end
Expand All @@ -42,17 +60,23 @@ def matrix

desc "search KEYWORD [KEYWORD...]", "Stream Tweets that contain specified keywords, joined with logical ORs (Control-C to stop)"
method_option "csv", :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
method_option "long", :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
def search(keyword, *keywords)
keywords.unshift(keyword)
client.on_inited do
search = T::Search.new
search.options = search.options.merge(options)
search.options = search.options.merge(:reverse => true)
search.options = search.options.merge(:format => STATUS_HEADINGS_FORMATTING)
search.all(keywords.join(' OR '))
end
client.on_timeline_status do |status|
if options['csv']
print_csv_status(status)
elsif options['long']
print_table build_long_status(status).each_with_index.map do |element, index|
STATUS_HEADINGS_FORMATTING[index] % element
end
else
print_status(status)
end
Expand All @@ -63,16 +87,22 @@ def search(keyword, *keywords)

desc "timeline", "Stream your timeline (Control-C to stop)"
method_option "csv", :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
method_option "long", :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
def timeline
client.on_inited do
cli = T::CLI.new
cli.options = cli.options.merge(options)
cli.options = cli.options.merge(:reverse => true)
cli.options = cli.options.merge(:format => STATUS_HEADINGS_FORMATTING)
cli.timeline
end
client.on_timeline_status do |status|
if options['csv']
print_csv_status(status)
elsif options['long']
print_table build_long_status(status).each_with_index.map do |element, index|
STATUS_HEADINGS_FORMATTING[index] % element
end
else
print_status(status)
end
Expand All @@ -83,14 +113,23 @@ def timeline

desc "users SCREEN_NAME [SCREEN_NAME...]", "Stream Tweets either from or in reply to specified users (Control-C to stop)"
method_option "csv", :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
method_option "long", :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
def users(screen_name, *screen_names)
if options['csv']
say STATUS_HEADINGS.to_csv
end
screen_names.unshift(screen_name)
client.on_inited do
if options['csv']
say STATUS_HEADINGS.to_csv
elsif options['long'] && STDOUT.tty?
say STATUS_HEADINGS.size.times.map do |index|
STATUS_HEADINGS_FORMATTING[index] % STATUS_HEADINGS[index]
end
end
end
client.on_timeline_status do |status|
if options['csv']
print_csv_status(status)
elsif options['long']
print_table(build_long_status(status))
else
print_status(status)
end
Expand Down

0 comments on commit aa9d1f5

Please sign in to comment.