Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

colored output using watchr in rails app

I'm using watchr in my rails3 app since I can't get autotest to work right. Watchr has been great but the only problem is I can't get colored output. I'm using this as my watchr file

If I run my tests manually via "rspec spec/" then I get colored output. However, letting watchr run the tests (via modifying one of my files) the output is no longer in color.

One thing I notice is that this part seems to prevent colored output somehow

def run(cmd)
  puts(cmd)
  `#{cmd}`
end

Normally, watchr runs the tests using this (in my watchr file)

result = run "rake spec"

But if I instead do the following I get colored output.

result = system "rake spec"

Only problem is that the above line no longer captures the output to "result", which in turn mans growl notifications no longer work.

Any ideas on how I can have the best of both worlds? Growl notifications AND my test output in color?

like image 511
Lan Avatar asked Dec 06 '25 15:12

Lan


1 Answers

I just ran into this issue as well. The solution is to provide the --tty option, in addition to --color, to the rspec command line invocation, e.g.:

result = run("bundle exec rspec --color --tty #{spec}")

According to this thread on the rspec google group:

The --tty option is there to tell RSpec that the output is being streamed to a terminal as opposed to a file, in which case we don't want to print the color codes even if --color is invoked.

If you then "puts" the result back to the terminal, you'll see the original coloring. If you want to parse that output for Growl, you'll want to strip out the ansi color codes using the strip_ansi method Bill Turner provided.

Hope that helps!

like image 180
odonnellt Avatar answered Dec 08 '25 08:12

odonnellt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!