=begin sexystring.rb Adds formatting methods: hexify, sexify, printable, and awesomise to the String class. Author: Matthew Kerwin Copyright (C) 2009 Matthew Kerwin. All Rights Reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed by Matthew Kerwin. 4. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =end class String # # Returns an Array of Strings, which are the hexadecimal representation of # this String object's bytes, in columns of 32-bytes. # # When +ansi+ is either: # * not given, # * given and false/nil, or # * given and equal to :mono # this method returns simple Strings. Any other value for +ansi+ # returns Strings including ANSI commands to alternate the background # of every four columns. # def hexify ansi=false ary=[] if ansi && (ansi != :mono) g = false i = 0 str=[] scan(/.{1,4}/m).each do |c| if (g = !g) as = "\e[100m" ae = "\e[0m" else as = ae = '' end str << (as + c.unpack("C*").map{|c|'%02x'%c}.join(' ') + ae) if (i+=1)%8 == 0 ary << str.join(' ') str = [] end end ary << str.join(' ') if str.length > 0 else scan(/.{1,32}/m).each{|c|ary << c.unpack("C*").map{|c|'%02x'%c}.join(' ')} end ary end # # Returns an Array of Strings, which are this String object split into 32-byte # columns. # # When +ansi+ is either: # * not given, # * given and false/nil, or # * given and equal to :mono # this method returns simple Strings. Any other value for +ansi+ # returns Strings including ANSI commands to alternate the background # of every four columns. # def sexify ansi=false ary=[] if ansi && (ansi != :mono) g = false i = 0 str='' scan(/.{1,4}/m).each do |c| if (g = !g) as = "\e[100m" ae = "\e[0m" else as = ae = '' end str << (as + c.printable + ae) if (i+=1)%8 == 0 ary << str str = '' end end ary << str if str.length > 0 ary.map{|s|"%-#{32 + s.scan(/\e\[(?:[0-9]+(?:;[0-9]+)*)m/).join.length}s"%s} else scan(/.{1,32}/m).each{|c|ary << ("%-32s" % c.printable)} ary end end # # Returns a copy of this String object, with all non-printable ASCII characters # replaced with +rep+ (default: ".") # def printable(rep=".") tr( ([*(0..31)] + [*(126..255)]).pack("C*"), rep[0..0] ) end # # Returns an Array of Strings, which are a combination of #hexify and #sexify. # # When +ansi+ is either: # * not given, # * given and false/nil, or # * given and equal to :mono # this method returns simple Strings. Any other value for +ansi+ # returns Strings including ANSI commands to alternate the background # of every four columns in both hexy and sexy sections. # def awesomise(ansi=false) sexify(ansi).zip(hexify(ansi).map{|h|": #{h}"}).map{|e|e.join} end alias :awesomize :awesomise end if $0 == __FILE__ #require 'sexystring' str = "lkjasdlkjasd\nalsdhlkjhaskdjh\n\nlkjhasdkjhaksjhdas\e[1mANSI MAGIC\e[0mklhkjhlysdufkhjknasdfkhjl7yk\nuhdfiuhdsf\t\tlkjhdsf" puts "\nThe String:", str.inspect, '-'*10, str, '-'*10 puts "\nPrintable:", str.printable puts "\nHexy:", str.hexify, '', str.hexify(:mono), '', str.hexify(:color) puts "\nSexy:", str.sexify, '', str.sexify(:mono), '', str.sexify(:color) puts "\nAwesome:", str.awesomise, '', str.awesomise(:mono), '', str.awesomise(:color) end