Forum: Ruby Help needed on string processing

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Shin guey W. (Guest)
on 2007-04-03 12:30
    I am trying to modify wirble gems to support color on windows
platform. Wirble is using some ANSI color code embeded in the string but
this doesn't work in windows platform. So, I write a method to change
the color on the windows command prompt, but I am still new to ruby and
I am not sure any effective way to do the string processing in ruby.

  Here is what I want to do:
  In wirble, it will return the processed string with the ANSI color
code inside it and I want to process it and call the block with yield
statement to change the command prompt color.

  So, here is the example string:
"\0330;30mThis is black string\0330;0m\0330;34Blue string\0330;0m"

  I want to write a function to run it with the block associated:
  string_process(input_string) { |color| do_color(color) }

  So, for the previous string it will run like this:
  yield :black -> call do_color(:black)
  print "This is black string"
  yield :nothing -> call do_color(:nothing)
  yield :blue -> call do_color(:blue)
  print "Blue string"
  yield :nothing -> call do_color(:nothing)

So, is it possible to do that? How do I achieve that?

Below are some wirble function and my do_color function:

    # Do color for win32 platform
    def do_color(key)
        hout ="kernel32", "GetStdHandle", 'L', 'L')
        set_console_txt_attrb ="kernel32","SetConsoleTextAttribute", ['L', 'N'], 'I')
        stdout =
        case key
        when :nothing
   stdout, 7)
        when :black
   stdout, 0)
        when :red
   stdout, 4)
        when :green
   stdout, 2)
        when :brown
   stdout, 6)
        when :blue
   stdout, 1)
        when :cyan
   stdout, 3)
        when :purple
   stdout, 5)
        when :light_gray
   stdout, 7)
        when :dark_gray
   stdout, 8)
        when :light_red
   stdout, 12)
        when :light_green
   stdout, 10)
        when :yellow
   stdout, 14)
        when :light_blue
   stdout, 9)
        when :light_cyan
   stdout, 11)
        when :light_purple
   stdout, 13)
        when :white
   stdout, 15)
            puts 'color didn\'t found for ' + key.to_s

    module Color
      COLORS = {
        :nothing      => '0;0',
        :black        => '0;30',
        :red          => '0;31',
        :green        => '0;32',
        :brown        => '0;33',
        :blue         => '0;34',
        :cyan         => '0;36',
        :purple       => '0;35',
        :light_gray   => '0;37',
        :dark_gray    => '1;30',
        :light_red    => '1;31',
        :light_green  => '1;32',
        :yellow       => '1;33',
        :light_blue   => '1;34',
        :light_cyan   => '1;36',
        :light_purple => '1;35',
        :white        => '1;37',

  #Wirble will escape the string with this statement:
   open_color = "\033[#{COLORS[key]}m"
   close_color = "\033[#{COLORS[:nohting]}m"
   return open_color + output_string + close_color
This topic is locked and can not be replied to.