Open darrencauthon opened 9 years ago
By googling "ruby puts with colors" I found this link. It might be a good starting point:
Wait a second... starting_blocks IS respecting colors?
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.
@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:
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:
If I run
sb execute "bundle instal"
I get:
@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
I'm wondering... maybe there's a gem that would give us both?
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.
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.
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.
@rossfuhrman Hmm... perhaps we could run the tests twice? :tongue:
@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.
@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.
@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.
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