Class: Col
- Inherits:
-
Object
- Object
- Col
- Defined in:
- lib/col.rb,
lib/col/version.rb
Overview
————————————————————————— #
Defined Under Namespace
Classes: DB, Error, Formatter, Utils
Constant Summary collapse
- COLORED_REGEXP =
/\e\[ # opening character sequence (?: [34][0-7] | [0-9] ) ? # optional code ( ; (?: [34][0-7] | [0-9] ))* # more optional codes m # closing character /x
- VERSION =
"1.0.2"
Class Method Summary collapse
- .[](*args) ⇒ Object
- .inline(*args) ⇒ Object
-
.plain(string) ⇒ Object
Convenience method to remove color codes from a string.
-
.uncolored(string) ⇒ Object
Convenience method to remove color codes from a string.
Instance Method Summary collapse
-
#fmt(*spec) ⇒ Object
e.g.
-
#initialize(*args) ⇒ Col
constructor
args: array of strings (to_s is called on each).
- #method_missing(message, *args, &block) ⇒ Object
- #to_s ⇒ Object
-
#to_str ⇒ Object
Works nicely with puts.
Constructor Details
#initialize(*args) ⇒ Col
args: array of strings (to_s is called on each)
9 10 11 |
# File 'lib/col.rb', line 9 def initialize(*args) @strings = args.map { |a| a.to_s } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(message, *args, &block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/col.rb', line 65 def method_missing(, *args, &block) unless args.empty? super # We're not interested in a message with arguments; NoMethodError end if Col::DB.method?() Col.new( self.fmt() ) # Col["..."].yellow -> Col # to allow Col["..."].yellow.bold else self.fmt() # Col["..."].gbow -> String end end |
Class Method Details
.inline(*args) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/col.rb', line 35 def Col.inline(*args) unless args.size % 2 == 0 # even? breaks 1.8.6 raise Col::Error, "Col.inline requires an even number of arguments" end result = String.new Col::Utils.each_pair(args) do |string, format| result << Col(string.to_s).fmt(format) end result end |
.plain(string) ⇒ Object
Convenience method to remove color codes from a string. Also available as Col.uncolored.
31 32 33 |
# File 'lib/col.rb', line 31 def Col.plain(string) string.gsub(COLORED_REGEXP, '') end |
.uncolored(string) ⇒ Object
Convenience method to remove color codes from a string. Also available as Col.plain.
25 26 27 |
# File 'lib/col.rb', line 25 def Col.uncolored(string) string.gsub(COLORED_REGEXP, '') end |
Instance Method Details
#fmt(*spec) ⇒ Object
52 53 54 |
# File 'lib/col.rb', line 52 def fmt(*spec) ::Col::Formatter.new(@strings, *spec).result end |
#to_s ⇒ Object
56 57 58 |
# File 'lib/col.rb', line 56 def to_s @strings.join end |
#to_str ⇒ Object
Works nicely with puts. E.g. puts Col(“…”).red.bold
61 62 63 |
# File 'lib/col.rb', line 61 def to_str to_s end |