On 7/13/14, 5:11 PM, yamadapc wrote:
Hello all :)

I've made a simple port of ruby's colorize library for D.
I'd greatly appreciate any feedback. Windows isn't supported,


It's nice, but it could be more efficient.

You usually use colors as a one-time shot to then output something to the terminal. For example in Ruby it would be:

puts "hello".colorize.red.on_blue

In Ruby it's implemented using regular expressions, very ugly and not very performant. In D you implemented it as returning another string that contains the format, which allocates a new string that is short lived.

In Crystal we make colorize return a struct that wraps the original value but contains the color information. Then when that struct is converted to a string it appends the color codes to the output. In Crystal there's to_s (similar to toString()) but also to_s(io), which subclasses must override to append something to the given IO. That way memory allocations are reduced drastically without needing to create intermediate strings.

Here's the source code and some specs if you feel like copying this idea:


Reply via email to