Giter VIP home page Giter VIP logo

capture_output_streams's Introduction

CaptureOutputStreams

Control output streams of third party gems or libraries.

There are so many cool ruby gems and libraries out there. What if you want to use one of them in your cli application without having a way to capture or modify the output streams.

While writing my own cli tool, i wanted to use the great git-up gem to update my local git branches.

I also wanted to control and format the console output of my own cli tools, but the author of the git-up gem uses 'puts' and 'system' statement in his code and I did not want to alter his code.

Here an example with the git-up gem.

git-up is a command line utility, but I wanted to use it in my code. So, to sync a repo, one would do:

[1] pry(main)> require 'git-up'
=> true

[2] pry(main)> GitUp.new.run([])
Fetching origin
master up to date
=> nil

My repo is synced now, but the method is only returning nil and the outputs are directly pushed to the console.

So if I want to hide the output from the console, I could do something like:

[3] pry(main)> require 'capture_output_streams'
=> true

[4] pry(main)> cos = capture_output_streams {GitUp.new.run([])}
=> #<struct  stdout="Fetching origin\n\e[1mmaster \e[0m\e[32mup to date\e[0m\n", stderr="">

[5] pry(main)> cos.stdout
=> "Fetching origin\n\e[1mmaster \e[0m\e[32mup to date\e[0m\n"

[6] pry(main)> puts cos.stdout
Fetching origin
master up to date
=> nil

Installation

Add this line to your application's Gemfile:

gem 'capture_output_streams'

And then execute:

$ bundle

Or install it yourself as:

$ gem install capture_output_streams

Usage

Pass your code within a block to capture_output_streams, and you'll get a struct back with the output streams.

a simple puts statment

[1] pry(main)> require 'capture_output_streams'
=> true

[2] pry(main)> capture_output_streams { puts "hello world" }
=> #<struct  stdout="hello world\n", stderr="">

a more complete example:

[3] pry(main)> cos = capture_output_streams do
[3] pry(main)*   system('date')
[3] pry(main)*   puts "--------------------"
[3] pry(main)*   $stderr.puts "im a failure"
[3] pry(main)* end
=> #<struct  stdout="Fri Nov 14 11:37:23 CET 2014\n--------------------\n", stderr="im a failure\n">

[4] pry(main)> puts cos.stdout
Fri Nov 14 11:37:23 CET 2014
--------------------
=> nil

[5] pry(main)> puts cos.stderr
im a failure
=> nil

Note

The kernel system and backticks (``) methods are also dynamically mocked during the block code execution as they don't use $stdout.

Open4 is used for these mockings.

Contributing

  1. Fork it ( http://github.com/msutter/capture_output_streams/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

capture_output_streams's People

Contributors

sealrealize avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.