class AnsiTerm::Attr

# 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.

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 sequence that represents the changes from self to other_attr.

Constants

BOLD
CROSSED_OUT
ITALICS
NORMAL
UNDERLINE

Attributes

bgcol[R]
fgcol[R]
flags[R]

Public Class Methods

new(fgcol: nil, bgcol: nil, flags: 0) click to toggle source
# File lib/ansiterm/attr.rb, line 28
def initialize(fgcol: nil, bgcol: nil, flags: 0)
  @fgcol = fgcol
  @bgcol = bgcol
  @flags = flags || 0
  freeze
end

Public Instance Methods

add_flag(flags) click to toggle source
# File lib/ansiterm/attr.rb, line 46
def add_flag(flags); merge({flags: @flags | flags}); end
bold() click to toggle source
# File lib/ansiterm/attr.rb, line 51
def bold;         add_flag(BOLD); end
bold?() click to toggle source
# File lib/ansiterm/attr.rb, line 55
def bold?;        (@flags & BOLD) != 0; end
clear_flag(flags) click to toggle source
# File lib/ansiterm/attr.rb, line 47
def clear_flag(flags); merge({flags: @flags & ~flags}); end
crossed_out() click to toggle source
# File lib/ansiterm/attr.rb, line 53
def crossed_out;  add_flag(CROSSED_OUT); end
crossed_out?() click to toggle source
# File lib/ansiterm/attr.rb, line 57
def crossed_out?; (@flags & CROSSED_OUT) != 0; end
merge(attrs) click to toggle source
# File lib/ansiterm/attr.rb, line 35
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
normal() click to toggle source
# File lib/ansiterm/attr.rb, line 50
def normal;       clear_flag(BOLD); end
normal?() click to toggle source
# File lib/ansiterm/attr.rb, line 59
def normal?
  @flags == NORMAL &&
    @fgcol.nil? &&
    @bgcol.nil?
end
reset() click to toggle source
# File lib/ansiterm/attr.rb, line 49
def reset;        self.class.new; end
transition_to(other) click to toggle source
# File lib/ansiterm/attr.rb, line 65
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

  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
end
underline() click to toggle source
# File lib/ansiterm/attr.rb, line 52
def underline;    add_flag(UNDERLINE); end
underline?() click to toggle source
# File lib/ansiterm/attr.rb, line 56
def underline?;   (@flags & UNDERLINE) != 0; end