class Canis::Chunks::ChunkLine

consists of an array of chunks and corresponds to a line to be printed.

Attributes

chunks[R]

an array of chunks

Public Class Methods

new(arr=nil) click to toggle source
# File lib/canis/core/include/colorparser.rb, line 96
def initialize arr=nil
  @chunks = arr.nil? ? Array.new : arr
end

Public Instance Methods

<<(chunk) click to toggle source
# File lib/canis/core/include/colorparser.rb, line 99
def <<(chunk)
  raise ArgumentError, "Chunk object expected. Received #{chunk.class} " unless chunk.is_a? Chunk
  @chunks << chunk
end
Also aliased as: add
_print(pad, string, _width ) click to toggle source

called only by show_colored_chunks

# File lib/canis/core/include/colorparser.rb, line 209
def _print(pad, string, _width )
  w = _width == 0? Ncurses.COLS : _width
  FFI::NCurses.waddnstr(pad,string.to_s, w) # changed 2011 dts
end
add(chunk)
Alias for: <<
each(&block) click to toggle source
# File lib/canis/core/include/colorparser.rb, line 104
def each &block
  @chunks.each &block
end
each_with_color() { |text, color, attr| ... } click to toggle source

Splits a chunk line giving text, color and attr The purpose of this is to free callers such as window or pad from having to know the internals of this implementation. Any substituing class should have a similar interface. @yield text, color and attr to the block

# File lib/canis/core/include/colorparser.rb, line 112
def each_with_color &block
  @chunks.each do |chunk| 
    case chunk
    when Chunks::Chunk
      color = chunk.color_pair
      attr = chunk.attr
      text = chunk.text
    when Array
      # for earlier demos that used an array
      color = chunk[0]
      attr = chunk[2]
      text = chunk[1]
    end
    yield text, color, attr
  end
end
index(str, offset = 0) click to toggle source

returns match for str in this chunk added 2013-03-07 - 23:59 adding index on 2014-05-26 for multiple matches on one line. 2014-05-28 - 12:02 may no longer be used since i have added to_s in next_match in textpad.

# File lib/canis/core/include/colorparser.rb, line 139
def index str, offset = 0
  result = 0
  _end = 0
  @chunks.each { |e| txt = e.text; 
    _end += txt.length 
    if _end < offset
      result += e.text.length 
      next
    end

    ix =  txt.index(str) 
    if ix
      _off = result + ix
      return _off if _off > offset
    end
    result += e.text.length 
  }
  return nil
end
length()
Alias for: row_length
method_missing(sym, *args, &block) click to toggle source

added to take care of many string methods that are called. Callers really don't know this is a chunkline, they assume its a string 2013-03-21 - 19:01

# File lib/canis/core/include/colorparser.rb, line 174
def method_missing(sym, *args, &block)
  self.to_s.send sym, *args, &block
end
print(pad, lineno, col, width, color_pair, attr) click to toggle source

print a chunkline NOTE: tested with pad only. Not with window.

Moved from textpad, this is the one method textpad is to call.
@param [Pad] pad on which to print
@param [Integer] lineno to print (zero-based)
@param [Integer] column to start printing on, usually 0
@param [Integer] width of pad, usually @content_cols
@param [color_pair] colorpair of textpad or widget
@param [attr] attr of textpad or widget
row_length() click to toggle source

returns length of text in chunks

# File lib/canis/core/include/colorparser.rb, line 130
def row_length
  result = 0
  @chunks.each { |e| result += e.text.length }
  return result
end
Also aliased as: length, size
show_colored_chunks(pad, width, defcolor = nil, defattr = nil) click to toggle source

moved from textpad.rb. Note that this does printing for a pad not window

so not yet tested if window is passed. Called by +print+.
# File lib/canis/core/include/colorparser.rb, line 194
def show_colored_chunks(pad, width, defcolor = nil, defattr = nil)

  self.each_with_color do |text, color, attrib|

    color ||= defcolor
    attrib ||= defattr || NORMAL

    #$log.debug "XXX: CHUNK textpad #{text}, cp #{color} ,  attrib #{attrib}. "
    FFI::NCurses.wcolor_set(pad, color,nil) if color
    FFI::NCurses.wattron(pad, attrib) if attrib
    _print(pad, text, width)
    FFI::NCurses.wattroff(pad, attrib) if attrib
  end
end
size()
Alias for: row_length
substring(start, size) click to toggle source

return a Chunkline containing only the text for the range requested

# File lib/canis/core/include/colorparser.rb, line 162
def substring start, size
  raise "substring not implemented yet"
end
to_s() click to toggle source
# File lib/canis/core/include/colorparser.rb, line 165
def to_s
  result = ""
  @chunks.each { |e| result << e.text }
  result
end