lib/t/cli/search.rb in t-0.3.0 vs lib/t/cli/search.rb in t-0.3.1
- old
+ new
@@ -1,16 +1,18 @@
require 'action_view'
+require 'pager'
require 'retryable'
require 't/core_ext/enumerable'
require 't/rcfile'
require 'thor'
require 'twitter'
module T
class CLI
class Search < Thor
include ActionView::Helpers::DateHelper
+ include Pager
DEFAULT_HOST = 'api.twitter.com'
DEFAULT_PROTOCOL = 'https'
DEFAULT_NUM_RESULTS = 20
MAX_PAGES = 16
@@ -30,11 +32,11 @@
def all(query)
defaults = {:include_entities => false}
defaults.merge!(:rpp => options['number']) if options['number']
timeline = client.search(query, defaults)
timeline.reverse! if options['reverse']
- run_pager
+ page unless T.env.test?
timeline.each do |status|
say "#{status.from_user.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
end
end
@@ -45,11 +47,11 @@
client.home_timeline(:page => page, :count => MAX_NUM_RESULTS).map do |status|
status if /#{query}/i.match(status.text)
end
end
end
- run_pager
+ page unless T.env.test?
timeline.flatten.compact.each do |status|
say "#{status.user.screen_name.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
end
end
map %w(tl) => :timeline
@@ -62,11 +64,11 @@
client.user_timeline(screen_name, :page => page, :count => MAX_NUM_RESULTS).map do |status|
status if /#{query}/i.match(status.text)
end
end
end
- run_pager
+ page unless T.env.test?
timeline.flatten.compact.each do |status|
say "#{status.user.screen_name.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
end
end
@@ -92,36 +94,9 @@
parent_options['host'] || DEFAULT_HOST
end
def protocol
parent_options['no_ssl'] ? 'http' : DEFAULT_PROTOCOL
- end
-
- def run_pager
- return if RUBY_PLATFORM =~ /win32/
- return if ENV["T_ENV"] == "test"
- return unless STDOUT.tty?
-
- read, write = IO.pipe
-
- unless Kernel.fork # Child process
- STDOUT.reopen(write)
- STDERR.reopen(write) if STDERR.tty?
- read.close
- write.close
- return
- end
-
- # Parent process, become pager
- STDIN.reopen(read)
- read.close
- write.close
-
- ENV['LESS'] = 'FSRX' # Don't page if the input is short enough
-
- Kernel.select [STDIN] # Wait until we have input before we start the pager
- pager = ENV['PAGER'] || 'less'
- exec pager rescue exec "/bin/sh", "-c", pager
end
end
end
end