1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
require 'ansi/code'
module ANSI
# ANSI::Chain was inspired by Kazuyoshi Tlacaelel's Isna library.
#
class Chain
#
def initialize(string)
@string = string.to_s
@codes = []
end
#
attr :string
#
attr :codes
#
def method_missing(s, *a, &b)
if ANSI::CHART.key?(s)
@codes << s
self
else
super(s, *a, &b)
end
end
#
def to_s
if codes.empty?
result = @string
else
result = Code.ansi(@string, *codes)
codes.clear
end
result
end
#
def to_str
to_s
end
end
end
|