in reply to Coloring IRC logs nicely

For anyone who's interested, here's the Jaad/davido pool-solution in Perl:

package Chat::Pool; # 16 should suffice. use constant POOL_SIZE => 16; sub new { bless [ # $self->pool: Arrayref of ["#fg","#bg"]-pairs. # Fixed size. [ $_[0]->precalc_colors(POOL_SIZE) ], # $self->col->[$id] contains the ["#fg","#bg"]-pair of a user. [], ] => shift } sub tick { my ($self, $id, $time) = @_; # If we haven't allocated a color for $id... unless(defined $self->col->[$id]) { # Take one from the pool (pop), assign in to $id, and unshift it. # All in one line :) unshift @{ $self->pool }, $self->col->[$id] = pop @{ $self->pool } +; } } # Return the allocated color for $_[1]. sub color {@{ $_[0]->col->[$_[1]] }} # Precalculate the pool. sub precalc_colors { my ($self, $num) = @_; local $_; return map {[ $self->calc_color($_, $num) ]} 0..$num-1; } # calc_color as in OP sub pool : lvalue { $_[0]->[0] } sub col : lvalue { $_[0]->[1] }

This solution is really easy :)

Update: If you don't want HTML-colors, but ANSI (e.g. for STDOUT), you can use:

package Chat::Pool::ANSI; use base "Chat::Pool"; sub precalc_colors {( ["\033[31;1m"], ["\033[32;1m"], ["\033[33;1m"], ["\033[34;1m"], ["\033[35;1m"], ["\033[36;1m"], ["\033[37;1m"], ["\033[31m"], ["\033[32m"], ["\033[33m"], ["\033[34m"], ["\033[35m"], ["\033[36m"], )} # later... printf "%s%s%s\n", ($chat->color($id))[0], $text, "\033[0m";