Class: Canis::Chunks::ChunkLine

Inherits:
AbstractChunkLine show all
Defined in:
lib/canis/core/include/colorparser.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arr = nil) ⇒ ChunkLine

Returns a new instance of ChunkLine.



96
97
98
# File 'lib/canis/core/include/colorparser.rb', line 96

def initialize arr=nil
  @chunks = arr.nil? ? Array.new : arr
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

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



174
175
176
# File 'lib/canis/core/include/colorparser.rb', line 174

def method_missing(sym, *args, &block)
  self.to_s.send sym, *args, &block
end

Instance Attribute Details

#chunksObject (readonly)

an array of chunks



94
95
96
# File 'lib/canis/core/include/colorparser.rb', line 94

def chunks
  @chunks
end

Instance Method Details

#<<(chunk) ⇒ Object Also known as: add

Raises:

  • (ArgumentError)


99
100
101
102
# 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

#_print(pad, string, _width) ⇒ Object

called only by show_colored_chunks



209
210
211
212
# 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

#each(&block) ⇒ Object



104
105
106
# File 'lib/canis/core/include/colorparser.rb', line 104

def each &block
  @chunks.each &block
end

#each_with_color { ... } ⇒ Object

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.

Yields:

  • text, color and attr to the block



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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) ⇒ Object

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.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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

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 [Fixnum] lineno to print (zero-based)
@param [Fixnum] column to start printing on, usually 0
@param [Fixnum] width of pad, usually @content_cols
@param [color_pair] colorpair of textpad or widget
@param [attr] attr of textpad or widget


186
187
188
189
190
191
# File 'lib/canis/core/include/colorparser.rb', line 186

def print pad, lineno, col, width, color_pair, attr
  FFI::NCurses.wmove pad, lineno, col
  a = get_attrib attr

  show_colored_chunks pad, width, color_pair, a
end

#row_lengthObject Also known as: length, size

returns length of text in chunks



130
131
132
133
134
# File 'lib/canis/core/include/colorparser.rb', line 130

def row_length
  result = 0
  @chunks.each { |e| result += e.text.length }
  return result
end

#show_colored_chunks(pad, width, defcolor = nil, defattr = nil) ⇒ Object

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


194
195
196
197
198
199
200
201
202
203
204
205
206
207
# 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

#substring(start, size) ⇒ Object

return a Chunkline containing only the text for the range requested



162
163
164
# File 'lib/canis/core/include/colorparser.rb', line 162

def substring start, size
  raise "substring not implemented yet"
end

#to_sObject



165
166
167
168
169
# File 'lib/canis/core/include/colorparser.rb', line 165

def to_s
  result = ""
  @chunks.each { |e| result << e.text }
  result
end