darrencauthon / starting_blocks

Run your minitest tests & specs immediately, with no setup or project edits.
MIT License
4 stars 2 forks source link

Colorize the results #27

Open darrencauthon opened 9 years ago

darrencauthon commented 9 years ago

Today, output from the script is printed out with a simple puts command.

If that output includes text with colors, the colors will not be shown... the color codes will be shown instead.

Is there a way to print out the colors?

This issue was brought up with the starting_blocks-rspec issue here https://github.com/rossfuhrman/starting_blocks-rspec/issues/4

darrencauthon commented 9 years ago

By googling "ruby puts with colors" I found this link. It might be a good starting point:

http://stackoverflow.com/questions/2070010/how-to-output-my-ruby-commandline-text-in-different-colours

darrencauthon commented 9 years ago

Wait a second... starting_blocks IS respecting colors?

1 sb watch ruby

rossfuhrman commented 9 years ago

This was brought up by the rspec plugin, but you can see the same lack of color by doing: sb execute "git status" Assuming you have colors set up for git.

This line runs the supplied system program: https://github.com/darrencauthon/starting_blocks/blob/master/lib/starting_blocks/bash.rb#L5

There are a surprising number of ways to start these system programs in ruby. Unfortunately, I can't find one that returns the output and prints the results in color for commands like rspec and git status.

darrencauthon commented 9 years ago

@rossfuhrman Can you try running the following script?

# the_test.rb
require 'minitest/autorun'
require 'minitest/spec'
require 'minitest/pride'

(1..1000).each { |_| describe("x") { it("x") { 1.must_equal 1 } } }

I see colors when I do it: banners_and_alerts_and_1__sb_watch__ruby__and_the_test_rb____desktop_ugh__-_vim

rossfuhrman commented 9 years ago

Right. If I use the minitest pride plugin, I get colors. If I use the nyan cat rspec formatter, I get color in my output when using starting blocks for rspec tests.

It looks like the pride plugin re-writes the output so that it is encoded with the colors. A "." becomes "\e[39m.\e[0m" I'm guessing the nyan cat rspec formatter does something similar.

But the colors are lost when running system commands through the starting blocks execute command line option as well.

If I run bundle install I get: screenshot 3 30 15 9 47 pm

If I run sb execute "bundle instal" I get: screenshot 3 30 15 9 47 pm

darrencauthon commented 9 years ago

@rossfuhrman I know nothing about how those terminal colors work.

Have you looked through these options?

http://stackoverflow.com/questions/2232/calling-shell-commands-from-ruby

darrencauthon commented 9 years ago

I'm wondering... maybe there's a gem that would give us both?

rossfuhrman commented 9 years ago

I looked at that link as well as some of the links inside of it. There are a bunch of ways to run system commands from within ruby, but I couldn't get any of them to do all of these at the same time: A) return the output to the caller so it could be processed B) display the output in color (for calls to rspec or git status, etc.) C) return from the call to the system process

I think I will try to create a simplified version of the program that just demonstrates the problem and some of the solutions I've tried so we can try and figure something out.

darrencauthon commented 9 years ago

If we can figure it out, we might also consider creating a gem to solve the problem for others.

I say this because I haven't been able to find a gem to do what we need yet.

rossfuhrman commented 9 years ago

I created a gist to demonstrate different ways to run system commands through ruby. However none of them meet the criteria to fix this bug. None of the options print the output in color AND return the output.

https://gist.github.com/rossfuhrman/d6e92e337bf9cd038035

darrencauthon commented 9 years ago

@rossfuhrman Hmm... perhaps we could run the tests twice? :tongue:

darrencauthon commented 9 years ago

@rossfuhrman Thinking aloud.... perhaps starting blocks could expose the method to be changed per plugin.

So let's say you're using rspec. If the rspec plugin parsed the text results and not the response code, then perhaps that plugin could switch the system to show the proper colors.

But if another plugin needs the response, then it can switch to that mode too.

rossfuhrman commented 9 years ago

@darrencauthon Yeah, like passing a color option to the call to sb? If we did that, we could get colorized output at the expense of loss of data that is being sent to growl. If it was an option, then the user could decide if color or growl info was more important I suppose.

darrencauthon commented 9 years ago

@rossfuhrman Yeah, you are right... it's hard to tell how these plugins will play together with this option getting flipped.

We should bring this up in tonight's KC Ruby meetup.