Class: Dispel::StyleMap
- Inherits:
-
Object
- Object
- Dispel::StyleMap
- Defined in:
- lib/dispel/style_map.rb
Instance Attribute Summary collapse
-
#background ⇒ Object
Returns the value of attribute background.
-
#foreground ⇒ Object
Returns the value of attribute foreground.
-
#lines ⇒ Object
Returns the value of attribute lines.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #add(style, line, columns) ⇒ Object
- #flatten ⇒ Object
-
#initialize(lines) ⇒ StyleMap
constructor
A new instance of StyleMap.
- #invert! ⇒ Object
- #left_pad!(offset) ⇒ Object
- #pop ⇒ Object
- #prepend(style, line, columns) ⇒ Object
- #shift ⇒ Object
- #slice!(*args) ⇒ Object
Constructor Details
#initialize(lines) ⇒ StyleMap
Returns a new instance of StyleMap.
5 6 7 |
# File 'lib/dispel/style_map.rb', line 5 def initialize(lines) @lines = Array.new(lines) end |
Instance Attribute Details
#background ⇒ Object
Returns the value of attribute background.
3 4 5 |
# File 'lib/dispel/style_map.rb', line 3 def background @background end |
#foreground ⇒ Object
Returns the value of attribute foreground.
3 4 5 |
# File 'lib/dispel/style_map.rb', line 3 def foreground @foreground end |
#lines ⇒ Object
Returns the value of attribute lines.
3 4 5 |
# File 'lib/dispel/style_map.rb', line 3 def lines @lines end |
Class Method Details
.single_line_reversed(columns) ⇒ Object
103 104 105 106 107 |
# 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) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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 |
Instance Method Details
#+(other) ⇒ Object
59 60 61 62 63 64 |
# 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) ⇒ Object
9 10 11 12 |
# File 'lib/dispel/style_map.rb', line 9 def add(style, line, columns) @lines[line] ||= [] @lines[line] << [style, columns] end |
#flatten ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# 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! ⇒ Object
50 51 52 53 54 55 56 57 |
# 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) ⇒ Object
41 42 43 44 45 46 47 48 |
# 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 ⇒ Object
77 78 79 |
# File 'lib/dispel/style_map.rb', line 77 def pop slice!(-1, 1) end |
#prepend(style, line, columns) ⇒ Object
14 15 16 17 |
# File 'lib/dispel/style_map.rb', line 14 def prepend(style, line, columns) @lines[line] ||= [] @lines[line].unshift [style, columns] end |
#shift ⇒ Object
73 74 75 |
# File 'lib/dispel/style_map.rb', line 73 def shift slice!(0, 1) end |