Skip to content

Commit

Permalink
Merge pull request #4560 from CocoaPods/yavuz/imp/UIWithPager
Browse files Browse the repository at this point in the history
[User Interface] add method to pipe outputs to a pager
  • Loading branch information
manuyavuz committed Nov 19, 2015
2 parents 2128c44 + abce6b7 commit 6c64eeb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/cocoapods/user_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,21 @@ def warn(message, actions = [], verbose_only = false)
warnings << { :message => message, :actions => actions, :verbose_only => verbose_only }
end

# Pipes all output inside given block to a pager.
#
# @yield Code block in which inputs to {#puts} and {#print} methods will be printed to the piper.
#
def with_pager
prev_handler = Signal.trap('INT', 'IGNORE')
IO.popen((ENV['PAGER'] || 'less -R'), 'w') do |io|
UI.output_io = io
yield
end
ensure
Signal.trap('INT', prev_handler)
UI.output_io = nil
end

private

# @!group Helpers
Expand Down
12 changes: 12 additions & 0 deletions spec/unit/user_interface_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ module Pod
# TODO
end

describe '#with_pager' do
it 'sets output_io' do
fd = IO.sysopen('/dev/null', 'w')
io = IO.new(fd)
IO.stubs(:popen).yields(io)
UI.with_pager do
UI.output_io.should == io
end
UI.output_io.should.be.nil
end
end

describe '#labeled' do
it 'prints nothing if value is nil' do
UI.labeled('label', nil)
Expand Down

0 comments on commit 6c64eeb

Please sign in to comment.