1
2
3
4
5
6
7
8
9
10
11
12
13
            stdin, stdout, stderr = Open3.popen3(cmd)
            leftover = { }
            TextMate::IO.exhaust(:output => stdout, :error => stderr) do |str, type|
              # we only want to call ‘block’ with full lines so we cut any trailing bytes that are not newline terminated and save for next time we call block
              if str =~ /\A(.*\n|)([^\n]*)\z/m
                lines = leftover[type].to_s + $1
                leftover[type] = $2
                lines.each { |line| block.call(type, line) }
              else
                raise "Allan’s regexp did not match ‘#{str}"
              end
            end
            leftover.each_pair { |type, str| block.call(type, str) }