From: Rob Biedenharn Date: 2008-12-06T04:23:18+09:00 Subject: Re: [QUIZ] AnsiString (#185) Since the Facets ANSICode was mentioned, I just thought that I'd also make everyone aware of the term-ansicolor gem by Florian Frank flori@ping.de which does this type of thing for colors. http://term-ansicolor.rubyforge.org/ gem install term-ansicolor -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com On Dec 5, 2008, at 1:03 PM, Matthew Moss wrote: > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- > > The three rules of Ruby Quiz 2: > > 1. Please do not post any solutions or spoiler discussion for this > quiz until 48 hours have passed from the time on this message. > > 2. Support Ruby Quiz 2 by submitting ideas as often as you can! > Visit . > > 3. Enjoy! > > Suggestion: A [QUIZ] in the subject of emails about the problem > helps everyone on Ruby Talk follow the discussion. Please reply to > the original quiz message, if you can. > > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- > > ## AnsiString (#185) > > > _Quiz description provided by Transfire_ > > Make a subclass of String (or delegate) that tracks "embedded" ANSI > codes along with the text. The class should add methods for wrapping > the text in ANSI codes. Implement as much of the core String API as > possible. So for example: > > s1 = AnsiString.new("Hi") > s2 = AnsiString.new("there!) > > s1.red # wrap text in red/escape ANSI codes > s1.blue # wrap text in blue/escape ANSI codes > > s3 = s1 + ' ' + s2 #=> New AnsiString > s3.to_str #=> "\e[31mHi\e[0m \e[34mthere!\e[0m" > > I have an [ANSICode][1] module (it's in [Facets][2]) that you are > welcome to provide for the ANSI backend, if desired. It is easy > enough to use; the literal equivalent of the above would be: > > ANSICode.red('Hi') + ' ' + ANSICode.blue('there!') > > Bonus points for being able to use ANSIStrings in a gsub block: > > ansi_string.gsub(pattern){ |s| s.red } > > > [1]: http://facets.rubyforge.org/doc/api/more/classes/ANSICode.html > [2]: http://facets.rubyforge.org/