Help needed on string processing


#1

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