Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorize the results #27

Open
darrencauthon opened this issue Feb 18, 2015 · 14 comments
Open

Colorize the results #27

darrencauthon opened this issue Feb 18, 2015 · 14 comments

Comments

@darrencauthon
Copy link
Owner

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 rossfuhrman/starting_blocks-rspec#4

@darrencauthon
Copy link
Owner Author

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
Copy link
Owner Author

Wait a second... starting_blocks IS respecting colors?

1 sb watch ruby

@rossfuhrman
Copy link
Contributor

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
Copy link
Owner Author

@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
Copy link
Contributor

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
Copy link
Owner Author

@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
Copy link
Owner Author

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

@rossfuhrman
Copy link
Contributor

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
Copy link
Owner Author

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
Copy link
Contributor

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
Copy link
Owner Author

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

@darrencauthon
Copy link
Owner Author

@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
Copy link
Contributor

@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
Copy link
Owner Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants