# Help needed on string processing

**URL:** <https://www.ruby-forum.com/t/help-needed-on-string-processing/90954>\
**Category:** Ruby\
**Created:** [April 3, 2007, 10:30am UTC](https://www.ruby-forum.com/t/help-needed-on-string-processing/90954 "2007-04-03T10:30:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![shinwoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@shinwoo](https://www.ruby-forum.com/u/shinwoo)\
**Post date:** [April 3, 2007, 10:30am UTC](https://www.ruby-forum.com/t/help-needed-on-string-processing/90954/1 "2007-04-03T10:30:38Z")

</div>

Hi,  
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 = Win32API.new("kernel32", "GetStdHandle", 'L', 'L')
    set_console_txt_attrb =

```

Win32API.new(“kernel32”,“SetConsoleTextAttribute”, [‘L’, ‘N’], ‘I’)  
stdout = hout.call(-11)  
case key  
when :nothing  
set\_console\_txt\_attrb.call( stdout, 7)  
when :black  
set\_console\_txt\_attrb.call( stdout, 0)  
when :red  
set\_console\_txt\_attrb.call( stdout, 4)  
when :green  
set\_console\_txt\_attrb.call( stdout, 2)  
when :brown  
set\_console\_txt\_attrb.call( stdout, 6)  
when :blue  
set\_console\_txt\_attrb.call( stdout, 1)  
when :cyan  
set\_console\_txt\_attrb.call( stdout, 3)  
when :purple  
set\_console\_txt\_attrb.call( stdout, 5)  
when :light\_gray  
set\_console\_txt\_attrb.call( stdout, 7)  
when :dark\_gray  
set\_console\_txt\_attrb.call( stdout, 8)  
when :light\_red  
set\_console\_txt\_attrb.call( stdout, 12)  
when :light\_green  
set\_console\_txt\_attrb.call( stdout, 10)  
when :yellow  
set\_console\_txt\_attrb.call( stdout, 14)  
when :light\_blue  
set\_console\_txt\_attrb.call( stdout, 9)  
when :light\_cyan  
set\_console\_txt\_attrb.call( stdout, 11)  
when :light\_purple  
set\_console\_txt\_attrb.call( stdout, 13)  
when :white  
set\_console\_txt\_attrb.call( stdout, 15)  
else  
puts 'color didn’t found for ’ + key.to\_s  
end  
end

```
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
