Module: WithKnowledgeOfColor
- Included in:
- String
- Defined in:
- lib/quality_extensions/string/with_knowledge_of_color.rb
Constant Summary collapse
- Color_regexp =
/\e\[[^m]+m/
Instance Method Summary collapse
-
#length_without_color ⇒ Object
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. - #ljust_with_color(width, padstr = ' ') ⇒ Object
- #nonprinting_characters_used_for_color ⇒ Object
- #rjust_with_color(width, padstr = ' ') ⇒ Object
- #strip_color ⇒ Object
Instance Method Details
#length_without_color ⇒ Object
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.
19 20 21 |
# File 'lib/quality_extensions/string/with_knowledge_of_color.rb', line 19 def length_without_color strip_color.length end |
#ljust_with_color(width, padstr = ' ') ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/quality_extensions/string/with_knowledge_of_color.rb', line 27 def ljust_with_color(width, padstr=' ') #ljust(width + nonprinting_characters_used_for_color.length, padstr) # That didn't work when you wanted the padstr to have color (as in ' '.on_blue) padding_width = [(width - length_without_color), 0].max self + padstr*padding_width end |
#nonprinting_characters_used_for_color ⇒ Object
23 24 25 |
# File 'lib/quality_extensions/string/with_knowledge_of_color.rb', line 23 def nonprinting_characters_used_for_color self.scan(Color_regexp).join end |
#rjust_with_color(width, padstr = ' ') ⇒ Object
35 36 37 38 |
# File 'lib/quality_extensions/string/with_knowledge_of_color.rb', line 35 def rjust_with_color(width, padstr=' ') padding_width = [(width - length_without_color), 0].max padstr*padding_width + self end |
#strip_color ⇒ Object
13 14 15 |
# File 'lib/quality_extensions/string/with_knowledge_of_color.rb', line 13 def strip_color gsub(Color_regexp, '') end |