Class: Ruco::StyleMap

Inherits:
Object show all
Defined in:
lib/ruco/style_map.rb

Constant Summary collapse

STYLES =
{
  :normal => 0,
  :reverse => Curses::A_REVERSE
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ StyleMap

Returns a new instance of StyleMap.



5
6
7
# File 'lib/ruco/style_map.rb', line 5

def initialize(lines)
  @lines = Array.new(lines)
end

Instance Attribute Details

#linesObject

Returns the value of attribute lines.



3
4
5
# File 'lib/ruco/style_map.rb', line 3

def lines
  @lines
end

Class Method Details

.curses_style(styles) ⇒ Object



86
87
88
# File 'lib/ruco/style_map.rb', line 86

def self.curses_style(styles)
  styles.sum{|style| STYLES[style] or raise("Unknown style #{style}") }
end

.styled(content, styles) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruco/style_map.rb', line 64

def self.styled(content, styles)
  styles ||= []
  content = content.dup

  build = []
  build << [[]]

  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



37
38
39
40
41
42
# File 'lib/ruco/style_map.rb', line 37

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/ruco/style_map.rb', line 9

def add(style, line, columns)
  @lines[line] ||= []
  @lines[line] << [style, columns]
end

#flattenObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruco/style_map.rb', line 14

def flatten
  @lines.map do |styles|
    next unless styles

    # start and one after end of every column-range changes styles
    points_of_change = styles.map{|s,c| [c.first, c.last+1] }.flatten

    flat = []

    styles.each do |style, columns|
      points_of_change.each do |point|
        next unless columns.include?(point)
        flat[point] ||= []
        flat[point].unshift style
      end
    end

    max = styles.map{|s,c|c.last}.max
    flat[max+1] = []
    flat
  end
end

#popObject



55
56
57
# File 'lib/ruco/style_map.rb', line 55

def pop
  slice!(-1, 1)
end

#shiftObject



51
52
53
# File 'lib/ruco/style_map.rb', line 51

def shift
  slice!(0, 1)
end

#slice!(*args) ⇒ Object



44
45
46
47
48
49
# File 'lib/ruco/style_map.rb', line 44

def slice!(*args)
  sliced = lines.slice!(*args)
  new = StyleMap.new(0)
  new.lines = sliced
  new
end