lib/ansiterm/attr.rb in ansiterm-0.3.1 vs lib/ansiterm/attr.rb in ansiterm-0.3.2
- old
+ new
@@ -1,8 +1,10 @@
module AnsiTerm
+ # # Attr #
+ #
# Attr represents the attributes of a given character.
# It is intended to follow the "Flyweight" GOF pattern
# in that any object representing a given combination
# of flags can be reused as the object itself is immutable.
#
@@ -18,26 +20,33 @@
NORMAL = 0
BOLD = 1
ITALICS = 2
UNDERLINE = 4
CROSSED_OUT = 8
-
+
attr_reader :fgcol, :bgcol, :flags
def initialize(fgcol: nil, bgcol: nil, flags: 0)
@fgcol = fgcol
@bgcol = bgcol
@flags = flags || 0
freeze
end
def merge(attrs)
+ if attrs.kind_of?(self.class)
+ old = attrs
+ attrs = {}
+ attrs[:bgcol] = old.bgcol if old.bgcol
+ attrs[:fgcol] = old.fgcol if old.fgcol
+ attrs[:flags] = old.flags if old.flags
+ end
self.class.new({bgcol: @bgcol, fgcol: @fgcol, flags: @flags}.merge(attrs))
end
-
+
def add_flag(flags); merge({flags: @flags | flags}); end
- def clear_flag(flags); merge({flags: @flags & ~BOLD}); end
+ def clear_flag(flags); merge({flags: @flags & ~flags}); end
def reset; self.class.new; end
def normal; clear_flag(BOLD); end
def bold; add_flag(BOLD); end
def underline; add_flag(UNDERLINE); end
@@ -53,26 +62,26 @@
@bgcol.nil?
end
def transition_to(other)
t = []
- t << [other.fgcol] if other.fgcol != self.fgcol
- t << [other.bgcol] if other.bgcol != self.bgcol
-
+ t << [other.fgcol] if other.fgcol && other.fgcol != self.fgcol
+ t << [other.bgcol] if other.bgcol && other.bgcol != self.bgcol
+
if other.bold? != self.bold?
t << [other.bold? ? 1 : 22]
end
-
+
if other.underline? != self.underline?
t << [other.underline? ? 4 : 24]
end
-
+
if other.crossed_out? != self.crossed_out?
t << [other.crossed_out? ? 9 : 29]
end
return "\e[0m" if other.normal? && !self.normal? && t.length != 1
-
+
if t.empty?
""
else
"\e[#{t.join(";")}m"
end