Class: DefaultFileRenderer

Inherits:
AbstractTextPadRenderer
  • Object
show all
Defined in:
lib/canis/core/include/defaultfilerenderer.rb

Overview

a simple file renderer that allows setting of colors per line based on regexps passed to insert_mapping. See tasks.rb for example usage.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = nil) ⇒ DefaultFileRenderer

Returns a new instance of DefaultFileRenderer.



19
20
21
22
# File 'lib/canis/core/include/defaultfilerenderer.rb', line 19

def initialize source=nil
  @default_colors = [:white, :black, NORMAL]
  @pair = get_color($datacolor, @default_colors.first, @default_colors[1])
end

Instance Attribute Details

#default_colorsObject

Returns the value of attribute default_colors.



16
17
18
# File 'lib/canis/core/include/defaultfilerenderer.rb', line 16

def default_colors
  @default_colors
end

#hashObject (readonly)

Returns the value of attribute hash.



17
18
19
# File 'lib/canis/core/include/defaultfilerenderer.rb', line 17

def hash
  @hash
end

Instance Method Details

#color_mappings(hash) ⇒ Object



24
25
26
# File 'lib/canis/core/include/defaultfilerenderer.rb', line 24

def color_mappings hash
  @hash = hash
end

#insert_mapping(regex, dim) ⇒ Object

takes a regexp, and an array of color, bgcolor and attr



28
29
30
31
# File 'lib/canis/core/include/defaultfilerenderer.rb', line 28

def insert_mapping regex, dim
  @hash ||= {}
  @hash[regex] = dim
end

#match_line(line) ⇒ Object

matches given line with each regexp to determine color use Internally used by render.



34
35
36
37
38
39
40
41
# File 'lib/canis/core/include/defaultfilerenderer.rb', line 34

def match_line line
  @hash.each_pair {| k , p|
    if line =~ k
      return p
    end
  }
  return @default_colors
end

#render(pad, lineno, text) ⇒ Object

render given line in color configured using insert_mapping



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/canis/core/include/defaultfilerenderer.rb', line 43

def render pad, lineno, text
  if @hash
    dim = match_line text
    fg = dim.first
    bg = dim[1] || @default_colors[1]
    if dim.size == 3
      att = dim.last
    else
      att = @default_colors.last
    end
    cp = get_color($datacolor, fg, bg)
  else
    cp = @pair
    att = @default_colors[2]
  end

  FFI::NCurses.wattron(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
  FFI::NCurses.mvwaddstr(pad, lineno, 0, text)
  FFI::NCurses.wattroff(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
end