Module: WithKnowledgeOfColor

Included in:
String
Defined in:
lib/qualitysmith_extensions/string/with_knowledge_of_color.rb

Overview

Author

Tyler Rick

Copyright

Copyright © 2007 QualitySmith, Inc.

License

Ruby License

Submit to Facets?

Maybe

++

Constant Summary collapse

Color_regexp =
/\e\[[^m]+m/

Instance Method Summary collapse

Instance Method Details

#length_without_colorObject

This version of length takes into account the fact that the ANSI color codes themselves don’t take up any space to display on the screen. So this returns the number of characters wide the string is when it is actually printed to the screen.



17
18
19
# File 'lib/qualitysmith_extensions/string/with_knowledge_of_color.rb', line 17

def length_without_color
  strip_color.length
end

#ljust_without_color(width) ⇒ Object



25
26
27
# File 'lib/qualitysmith_extensions/string/with_knowledge_of_color.rb', line 25

def ljust_without_color(width)
  ljust(width + nonprinting_characters_used_for_color.length)
end

#nonprinting_characters_used_for_colorObject



21
22
23
# File 'lib/qualitysmith_extensions/string/with_knowledge_of_color.rb', line 21

def nonprinting_characters_used_for_color
  self.scan(Color_regexp).join
end

#strip_colorObject



11
12
13
# File 'lib/qualitysmith_extensions/string/with_knowledge_of_color.rb', line 11

def strip_color
  gsub(Color_regexp, '')
end