Skip to content

Commit

Permalink
Adding an option to display tweets on CLI in reverse order (oldest fi…
Browse files Browse the repository at this point in the history
…rst). Added specs as well, including a spec helper to capture STDOUT for testing it a bit more thoroughly

Signed-off-by: John Nunemaker <nunemaker@gmail.com>
  • Loading branch information
cdb authored and jnunemaker committed Jan 5, 2009
1 parent 47d0506 commit 40d2f1a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
Empty file modified bin/twitter
100644 → 100755
Empty file.
6 changes: 5 additions & 1 deletion lib/twitter/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,19 @@ def run
option('force', 'f') {
description "Ignore since_id and show first page of results even if there aren't new ones"
}
option('reverse', 'r') {
description 'Reverse the output so the oldest tweets are at the top'
}

def run
do_work do
timeline = params['timeline'].value == 'me' ? 'user' : params['timeline'].value
options, since_id = {}, Configuration["#{timeline}_since_id"]
options[:since_id] = since_id if !since_id.nil? && !params['force'].given?
reverse = params['reverse'].given? ? true : false
cache = [:friends, :user].include?(timeline)
collection = base.timeline(timeline.to_sym, options)
output_tweets(collection, {:cache => cache, :since_prefix => timeline})
output_tweets(collection, {:cache => cache, :since_prefix => timeline, :reverse => reverse})
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/twitter/cli/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ def output_tweets(collection, options={})
options = {
:cache => false,
:since_prefix => '',
:empty_msg => 'Nothing new since your last check.'
:empty_msg => 'Nothing new since your last check.',
:reverse => false
}.merge(options)

if collection.size > 0
justify = collection.collect { |s| s.user.screen_name }.max { |a,b| a.length <=> b.length }.length rescue 0
indention = ' ' * (justify + 3)
say("\n#{indention}#{collection.size} new tweet(s) found.\n\n")
collection.reverse! if options[:reverse]
collection.each do |s|
Tweet.create_from_tweet(current_account, s) if options[:cache]

Expand All @@ -33,7 +35,7 @@ def output_tweets(collection, options={})
say "#{CGI::unescapeHTML(formatted_name)}: #{CGI::unescapeHTML(formatted_msg)}\n#{indention}#{formatted_time}\n\n"
end

Configuration["#{options[:since_prefix]}_since_id"] = collection.first.id
Configuration["#{options[:since_prefix]}_since_id"] = options[:reverse] ? collection.last.id : collection.first.id
else
say(options[:empty_msg])
end
Expand Down
10 changes: 9 additions & 1 deletion spec/cli/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ class Tweet < OpenStruct
end

specify "should properly format" do
output_tweets(@collection)
stdout_for {
output_tweets(@collection)
}.should match /with a few words[\w\W]*with a\./
end

specify 'should format in reverse' do
stdout_for {
output_tweets(@collection, :reverse => true)
}.should match /with a\.[\w\W]*with a few words/
end
end
end
13 changes: 12 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,15 @@
dir = File.dirname(__FILE__)

$:.unshift(File.join(dir, '/../lib/'))
require dir + '/../lib/twitter'
require dir + '/../lib/twitter'


def stdout_for(&block)
# Inspired by http://www.ruby-forum.com/topic/58647
old_stdout = $stdout
$stdout = StringIO.new
yield
output = $stdout.string
$stdout = old_stdout
output
end

0 comments on commit 40d2f1a

Please sign in to comment.