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(style) ⇒ Object



108
109
110
111
# File 'lib/ruco/style_map.rb', line 108

def self.curses_style(style)
  return 0 unless style
  STYLES[style] or raise("Unknown style #{style.inspect}")
end

.single_line_reversed(columns) ⇒ Object



113
114
115
116
117
# File 'lib/ruco/style_map.rb', line 113

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

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

#flattenObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruco/style_map.rb', line 19

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

    flat = []

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

    max = styles.map{|_,columns| columns.last }.max
    flat[max+1] = :normal
    flat
  end
end

#invert!Object



50
51
52
53
54
55
56
57
# File 'lib/ruco/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/ruco/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

#popObject



77
78
79
# File 'lib/ruco/style_map.rb', line 77

def pop
  slice!(-1, 1)
end

#prepend(style, line, columns) ⇒ Object



14
15
16
17
# File 'lib/ruco/style_map.rb', line 14

def prepend(style, line, columns)
  @lines[line] ||= []
  @lines[line].unshift [style, columns]
end

#shiftObject



73
74
75
# File 'lib/ruco/style_map.rb', line 73

def shift
  slice!(0, 1)
end

#slice!(*args) ⇒ Object



66
67
68
69
70
71
# File 'lib/ruco/style_map.rb', line 66

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