public
Rubygem
Description: A tool to simplify working with remote branches
Homepage: http://grb.rubyforge.org/
Clone URL: git://github.com/webmat/git_remote_branch.git
Click here to lend your support to: git_remote_branch and make a donation at www.pledgie.com !
git_remote_branch / vendor / capture_fu.rb
100644 59 lines (46 sloc) 1.389 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module CaptureFu
  VERSION = '0.0.1'
 
  def capture_output(&block)
    real_out, real_err = $stdout, $stderr
    result = fake_out = fake_err = nil
    begin
      fake_out, fake_err = Helpers::PipeStealer.new, Helpers::PipeStealer.new
      $stdout, $stderr = fake_out, fake_err
      result = yield
    ensure
      $stdout, $stderr = real_out, real_err
    end
    return result, fake_out.captured, fake_err.captured
  end
 
  # This first implementation is only intended for batch executions.
  # You can't pipe stuff programmatically to the child process.
  def capture_process_output(command)
 
    #capture stderr in the same stream
    command << ' 2>&1' unless Helpers.stderr_already_redirected(command)
 
    out = `#{command}`
    return $?.exitstatus, out
  end
 
 
  private
 
  module Helpers
    
    def self.stderr_already_redirected(command)
      #Already redirected to stdout (valid for Windows)
      return true if command =~ /2>&1\s*\Z/
 
      #Redirected to /dev/null (this is clearly POSIX-dependent)
      return true if command =~ /2>\/dev\/null\s*\Z/
 
      return false
    end
 
    class PipeStealer < File
      attr_reader :captured
      def initialize
        @captured = ''
      end
      def write(s)
        @captured << s
      end
      def captured
        return nil if @captured.empty?
        @captured.dup
      end
    end
 
  end #Helper module
end