Err the Blog Atom Feed Icon
Err the Blog
Rubyisms and Railities
  • “A Ruby Rainbow”
    – Chris on October 20, 2006

    Ruby could use a bit more color. We’re all clamoring for it. Coloring our tests. Tagging our log files. Here’s some quick (and/or dirty) help.

    $ sudo gem install colored

    Launch yourself an irb and try ‘er out:

    colored irb terminal session

    Fun stuff (pretend it says colored, not color for the require). Not just for the console, though—it also works in Rails log files.

    logger.debug "API: ".red.bold + "Got topics from Chowhound.".red
    

    becomes

    colored irb terminal session

    And hey, Windows users?

    $ gem install win32console

    It works, thanks to lucas for testing and Dr Nic for the gemified version of Win32Console.

    Here’s a short screencast (it’s so fast that it appears static):

    colored irb terminal session

    Have fun.

  • binary42, about 10 hours later:

    This reminds me of some code I worked on in Heretix. We did the same thing for Ruby strings.. though it had a few quirks.

    Now we have a nice packaged gem! Thanks.

  • Geoff, about 10 hours later:

    I had made something very similar myself for personal use. Very cool stuff. Any chance we can get this working with zentest?

  • Geoff, about 10 hours later:

    PS: Just noticed the example as i’m watching battlestar galactica… hrm. ponderous, indeed. :)

  • mj, about 11 hours later:

    Anyone have this problem?

    Local gem file not found: color*.gem Attempting remote installation of ‘color’ Updating Gem source index for: require.errtheblog.com ERROR: While executing gem … (TypeError) can’t convert Hash into String

  • Dr Nic, about 15 hours later:

    “screencast” – you are so web2.0. :)

  • Chris, 2 days later:

    Of course, there’s already the Text::Highlight gem. But that’s no fun.

  • Justin, 3 days later:

    Win32Console is already gemified – just install win32console. No need to go to another gem repository.

    It’s a different project than Gonzales, but uses his code:

    http://rubyforge.org/projects/winconsole/

  • topfunky, 4 days later:

    autotest can use the redgreen gem to do this…

    1. in ~/.autotest require ‘autotest/redgreen’
  • topfunky, 4 days later:

    Textile mangled my code…

    Anyway, there is already a color gem. Maybe a different name could be used?

    http://rubyforge.org/projects/color/

  • nkryptic, 8 days later:

    Hope this will be of some use.

    I wrote up some code to colorize your test suite that is run by rake. I wanted to run all my test and have the output be colorized without much work, so rather than hacking the ruby test libs I just re-opened them a little.

    Here is the url: http://www.nkryptic.com/2006/10/26/rails-colorized-rake-testing

  • leslie, 3 months later:

    For irb-specific coloring, check out http://pablotron.org/software/wirble/

    FYI, in the States, “colored” can be a charged term, so beware.

  • Deep Blue, 3 months later:

    Yes, beware, I as a computer know of such prejudices of color. Deep Blue? I’m actually quite cheery and my aluminum and silicon parts are neither deep, nor blue.

  • alex, 5 months later:

    In the screenshot it still says gem install color and require colo@r. To get it working change both to @colored

  • Edvard Majakari, 9 months later:

    I failed to get it working with Windows too, but after 20 minutes of inspecting how redgreen works, I could get it to work. My solution doesn’t require any external gems or plugins, but it does pollute your test_helper even more. Maybe I should convert it to plugin some day and take use of redgreen.

    1. Hack for colorifying rake tests – green tests are now really green!
    2. (most stuff shamelessly stolen from redgreen.rb)

    if ENV[‘COLORIZE’] || ENV[‘TERM’] == ‘linux’ require ‘test/unit’ require ‘test/unit/ui/console/testrunner’ end

    module Color
      COLORS = { :clear => 0, :red => 31, :green => 32, :yellow => 33 }
      def self.method_missing(color_name, *args)
        colname = color_name.to_s
        ansi_color = (colname =~ /^light/ ?
                      light_color(colname.gsub(/light/, '')) :
                      color(colname))
        ansi_color + args.first + color(:clear)
      end
      def self.color(color)
        "\e[#{COLORS[color.to_sym]}m"
      end
    end
    def self.light_color(color)
      "\e[1;#{COLORS[color.to_sym]}m"
    end
    class Test::Unit::UI::Console::TestRunner
      def adjust_io_fd
        if !@io_set && PLATFORM =~ /win32/
          @io_set = true
          require 'Win32/Console/ANSI'
          @io = Win32::Console::ANSI::IO.new
        end
      end
    end
    def output_single(something, level=NORMAL)
      return unless (output?(level))
      adjust_io_fd
      something = case something
                  when '.' then Color.lightgreen('.')
                  when 'F' then Color.lightred("F")
                  when 'E' then Color.lightyellow("E")
                  else something
                  end
      @io.write(something)
      @io.flush
    end
    class Test::Unit::TestResult
      alias :old_to_s :to_s
      def to_s
        if old_to_s =~ /\d+ tests, \d+ assertions, (\d+) failures, (\d+) errors/
          Color.send($1.to_i != 0 || $2.to_i != 0 ? :lightred : :lightgreen, $&)
        end
      end
    end
    class Test::Unit::Failure
      alias :old_long_display :long_display
      def long_display
        old_long_display.sub('Failure', Color.lightred('Failure'))
      end
    end
    class Test::Unit::Error
      alias :old_long_display :long_display
      def long_display
        old_long_display.sub('Error', Color.lightyellow('Error'))
      end
    end
  • Fourteen people have commented.
    Chime in.
    Sorry, no more comments :(
This is Err, the weblog of PJ Hyett and Chris Wanstrath.
All original content copyright ©2006-2008 the aforementioned.