lib/ansiterm/attr.rb in ansiterm-0.3.2 vs lib/ansiterm/attr.rb in ansiterm-0.3.3

- old
+ new

@@ -10,11 +10,11 @@ # # This allows you to decide on a case by case basis # whether to e.g. encode a string as spans with one Attr, # or characters with one Attr per character. # - # Use Attr#transition(other_attr) to retrieve an ANSI + # Use `Attr#transition(other_attr)` to retrieve an ANSI # sequence that represents the changes from self to # other_attr. # class Attr NORMAL = 0 @@ -30,11 +30,19 @@ @bgcol = bgcol @flags = flags || 0 freeze end + def ==(other) + return false if !other.kind_of?(self.class) + return fgcol == other.fgcol && + bgcol == other.bgcol && + flags == other.flags + end + def merge(attrs) + return self if self == attrs if attrs.kind_of?(self.class) old = attrs attrs = {} attrs[:bgcol] = old.bgcol if old.bgcol attrs[:fgcol] = old.fgcol if old.fgcol @@ -62,12 +70,12 @@ @bgcol.nil? end def transition_to(other) t = [] - t << [other.fgcol] if other.fgcol && other.fgcol != self.fgcol - t << [other.bgcol] if other.bgcol && other.bgcol != self.bgcol + t << [other.fgcol] if other.fgcol != self.fgcol && other.fgcol + t << [other.bgcol] if other.bgcol != self.fgcol && other.bgcol if other.bold? != self.bold? t << [other.bold? ? 1 : 22] end @@ -87,6 +95,9 @@ "\e[#{t.join(";")}m" end end end + def self.attr(*args) + Attr.new(*args) + end end