class Dispel::StyleMap

Attributes

background[RW]
foreground[RW]
lines[RW]

Public Class Methods

new(lines) click to toggle source
# File lib/dispel/style_map.rb, line 5
def initialize(lines)
  @lines = Array.new(lines)
end
single_line_reversed(columns) click to toggle source
# File lib/dispel/style_map.rb, line 103
def self.single_line_reversed(columns)
  map = StyleMap.new(1)
  map.add(:reverse, 0, 0...columns)
  map
end
styled(content, styles) click to toggle source
# File lib/dispel/style_map.rb, line 81
def self.styled(content, styles)
  styles ||= []
  content = content.dup

  build = []
  build << [:normal]

  buffered = ''
  styles.each do |style|
    if style
      build[-1] << buffered
      buffered = ''

      # set new style
      build << [style]
    end
    buffered << (content.slice!(0,1) || '')
  end
  build[-1] << buffered + content
  build
end

Public Instance Methods

+(other) click to toggle source
# File lib/dispel/style_map.rb, line 59
def +(other)
  lines = self.lines + other.lines
  new = StyleMap.new(0)
  new.lines = lines
  new
end
add(style, line, columns) click to toggle source
# File lib/dispel/style_map.rb, line 9
def add(style, line, columns)
  @lines[line] ||= []
  @lines[line] << [style, columns]
end
flatten() click to toggle source
# File lib/dispel/style_map.rb, line 19
def flatten
  @lines.map do |styles|
    next unless styles

    # change to style at start and recalculate one after the end
    points_of_change = styles.map{|s,c| c.min ? [c.min, c.max + 1] : nil }.flatten.compact.uniq
    next if points_of_change.empty?

    flat = []

    points_of_change.each do |point|
      flat[point] = :normal # set default
      styles.each do |style, columns|
        next unless columns.include?(point)
        flat[point] = style
      end
    end

    flat
  end
end
invert!() click to toggle source
# File lib/dispel/style_map.rb, line 50
def invert!
  map = {:reverse => :normal, :normal => :reverse}
  @lines.compact.each do |styles|
    styles.map! do |style, columns|
      [map[style] || style, columns]
    end
  end
end
left_pad!(offset) click to toggle source
# File lib/dispel/style_map.rb, line 41
def left_pad!(offset)
  @lines.compact.each do |styles|
    next unless styles
    styles.map! do |style, columns|
      [style, (columns.first + offset)..(columns.last + offset)]
    end
  end
end
pop() click to toggle source
# File lib/dispel/style_map.rb, line 77
def pop
  slice!(-1, 1)
end
prepend(style, line, columns) click to toggle source
# File lib/dispel/style_map.rb, line 14
def prepend(style, line, columns)
  @lines[line] ||= []
  @lines[line].unshift [style, columns]
end
shift() click to toggle source
# File lib/dispel/style_map.rb, line 73
def shift
  slice!(0, 1)
end
slice!(*args) click to toggle source
# File lib/dispel/style_map.rb, line 66
def slice!(*args)
  sliced = lines.slice!(*args)
  new = StyleMap.new(0)
  new.lines = sliced
  new
end