I have a very specific error that I cannot seem to resolve. I have tried both the colorize plugin and the extended string class from
Now what happens is when I want color some text it will work for one character. However, if that character is written over again or sometimes just the character next to it is written with a color, the 2d array becomes corrupted with the colorizing string text.
Note that this problem occurs with the colorize gem in the above stack overflow link AND when I don't use it and just extend the string class myself.
Provided is some sample code so that you can replicate it yourself. Note that you can NEVER use Ruby's pretty print to print the 2d array after coloring text as it will always look corrupted. If you do not wish to install the gem to test it yourself you can just extend the string class like one of the responses of the above link shows. It doesn't matter which version you choose, it doesn't effect the outcome in any way. I will also link that code at the bottom.
EDIT: One color per horizontal line is all that is allowed. Anything more than that causes corruption.
This version works just fine.
private def color(screen_object, i, j, k)
#Creates 2d
screen2 = Array.new(25) { Array.new(25, 0) }
for i in 0...25 do
if i == 0 || i == 24
screen2[i] = '$-----------------------$'
else
screen2[i] = '| |'
end
end
screen2[0][0] = '~'.colorize(:red)
for i in 0..25
puts "#{screen2[i]}"
end
end
This version causes corruption when overwriting the same location with the same char and same color.
private def color(screen_object, i, j, k)
#Creates 2d
screen2 = Array.new(25) { Array.new(25, 0) }
for i in 0...25 do
if i == 0 || i == 24
screen2[i] = '$-----------------------$'
else
screen2[i] = '| |'
end
end
screen2[0][0] = '~'.colorize(:red)
screen2[0][0] = '~'.colorize(:red)
for i in 0..25
puts "#{screen2[i]}"
end
end
This version doesn't overwrite any chars, but having chars next to each other that are colorized causes corruption.
private def color(screen_object, i, j, k)
#Creates 2d
screen2 = Array.new(25) { Array.new(25, 0) }
for i in 0...25 do
if i == 0 || i == 24
screen2[i] = '$-----------------------$'
else
screen2[i] = '| |'
end
end
screen2[0][0] = '~'.colorize(:red)
screen2[0][1] = '~'.colorize(:red)
for i in 0..25
puts "#{screen2[i]}"
end
end
But sometimes writing chars right next to each other is fine. Chars that are next to each other vertically don't seem to be affected as much.
private def color(screen_object, i, j, k)
#Creates 2d
screen2 = Array.new(25) { Array.new(25, 0) }
for i in 0...25 do
if i == 0 || i == 24
screen2[i] = '$-----------------------$'
else
screen2[i] = '| |'
end
end
screen2[0][0] = '~'.colorize(:red)
screen2[1][0] = '~'.colorize(:red)
for i in 0..25
puts "#{screen2[i]}"
end
end
Finally, many chars can have a color as long as they aren't net to each other.
private def color(screen_object, i, j, k)
#Creates 2d
screen2 = Array.new(25) { Array.new(25, 0) }
for i in 0...25 do
if i == 0 || i == 24
screen2[i] = '$-----------------------$'
else
screen2[i] = '| |'
end
end
screen2[0][0] = '~'.colorize(:red)
screen2[14][22] = '~'.colorize(:red)
screen2[7][22] = '~'.colorize(:red)
screen2[2][14] = '~'.colorize(:red)
screen2[24][24] = '~'.colorize(:red)
screen2[9][11] = '~'.colorize(:red)
for i in 0..25
puts "#{screen2[i]}"
end
end
Here is the string extension if you don't wish to install gem. Credit goes to user Ivan Black from the other post.
class String
def black; "\e[30m#{self}\e[0m" end
def red; "\e[31m#{self}\e[0m" end
def green; "\e[32m#{self}\e[0m" end
def brown; "\e[33m#{self}\e[0m" end
def blue; "\e[34m#{self}\e[0m" end
def magenta; "\e[35m#{self}\e[0m" end
def cyan; "\e[36m#{self}\e[0m" end
def gray; "\e[37m#{self}\e[0m" end
def bg_black; "\e[40m#{self}\e[0m" end
def bg_red; "\e[41m#{self}\e[0m" end
def bg_green; "\e[42m#{self}\e[0m" end
def bg_brown; "\e[43m#{self}\e[0m" end
def bg_blue; "\e[44m#{self}\e[0m" end
def bg_magenta; "\e[45m#{self}\e[0m" end
def bg_cyan; "\e[46m#{self}\e[0m" end
def bg_gray; "\e[47m#{self}\e[0m" end
def bold; "\e[1m#{self}\e[22m" end
def italic; "\e[3m#{self}\e[23m" end
def underline; "\e[4m#{self}\e[24m" end
def blink; "\e[5m#{self}\e[25m" end
def reverse_color; "\e[7m#{self}\e[27m" end
end