canis 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +45 -0
  3. data/CHANGES +52 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +24 -0
  7. data/Rakefile +2 -0
  8. data/canis.gemspec +25 -0
  9. data/examples/alpmenu.rb +46 -0
  10. data/examples/app.sample +19 -0
  11. data/examples/appemail.rb +191 -0
  12. data/examples/atree.rb +105 -0
  13. data/examples/bline.rb +181 -0
  14. data/examples/common/devel.rb +319 -0
  15. data/examples/common/file.rb +93 -0
  16. data/examples/data/README.markdown +9 -0
  17. data/examples/data/brew.txt +38 -0
  18. data/examples/data/color.2 +37 -0
  19. data/examples/data/gemlist.txt +59 -0
  20. data/examples/data/lotr.txt +12 -0
  21. data/examples/data/ports.txt +136 -0
  22. data/examples/data/table.txt +37 -0
  23. data/examples/data/tasks.csv +88 -0
  24. data/examples/data/tasks.txt +27 -0
  25. data/examples/data/todo.txt +16 -0
  26. data/examples/data/todocsv.csv +28 -0
  27. data/examples/data/unix1.txt +21 -0
  28. data/examples/data/unix2.txt +11 -0
  29. data/examples/dbdemo.rb +506 -0
  30. data/examples/dirtree.rb +177 -0
  31. data/examples/newtabbedwindow.rb +100 -0
  32. data/examples/newtesttabp.rb +92 -0
  33. data/examples/tabular.rb +212 -0
  34. data/examples/tasks.rb +179 -0
  35. data/examples/term2.rb +88 -0
  36. data/examples/testbuttons.rb +307 -0
  37. data/examples/testcombo.rb +102 -0
  38. data/examples/testdb.rb +182 -0
  39. data/examples/testfields.rb +208 -0
  40. data/examples/testflowlayout.rb +43 -0
  41. data/examples/testkeypress.rb +98 -0
  42. data/examples/testlistbox.rb +187 -0
  43. data/examples/testlistbox1.rb +199 -0
  44. data/examples/testmessagebox.rb +144 -0
  45. data/examples/testprogress.rb +116 -0
  46. data/examples/testree.rb +107 -0
  47. data/examples/testsplitlayout.rb +53 -0
  48. data/examples/testsplitlayout1.rb +49 -0
  49. data/examples/teststacklayout.rb +48 -0
  50. data/examples/testwsshortcuts.rb +68 -0
  51. data/examples/testwsshortcuts2.rb +129 -0
  52. data/lib/canis.rb +16 -0
  53. data/lib/canis/core/docs/index.txt +104 -0
  54. data/lib/canis/core/docs/list.txt +16 -0
  55. data/lib/canis/core/docs/style_help.yml +34 -0
  56. data/lib/canis/core/docs/tabbedpane.txt +15 -0
  57. data/lib/canis/core/docs/table.txt +31 -0
  58. data/lib/canis/core/docs/textpad.txt +48 -0
  59. data/lib/canis/core/docs/tree.txt +23 -0
  60. data/lib/canis/core/include/.DS_Store +0 -0
  61. data/lib/canis/core/include/action.rb +83 -0
  62. data/lib/canis/core/include/actionmanager.rb +49 -0
  63. data/lib/canis/core/include/appmethods.rb +179 -0
  64. data/lib/canis/core/include/bordertitle.rb +49 -0
  65. data/lib/canis/core/include/canisparser.rb +100 -0
  66. data/lib/canis/core/include/colorparser.rb +437 -0
  67. data/lib/canis/core/include/defaultfilerenderer.rb +64 -0
  68. data/lib/canis/core/include/io.rb +320 -0
  69. data/lib/canis/core/include/layouts/SplitLayout.rb +161 -0
  70. data/lib/canis/core/include/layouts/abstractlayout.rb +213 -0
  71. data/lib/canis/core/include/layouts/flowlayout.rb +104 -0
  72. data/lib/canis/core/include/layouts/stacklayout.rb +109 -0
  73. data/lib/canis/core/include/listbindings.rb +89 -0
  74. data/lib/canis/core/include/listeditable.rb +319 -0
  75. data/lib/canis/core/include/listoperations.rb +61 -0
  76. data/lib/canis/core/include/listselectionmodel.rb +388 -0
  77. data/lib/canis/core/include/multibuffer.rb +173 -0
  78. data/lib/canis/core/include/ractionevent.rb +73 -0
  79. data/lib/canis/core/include/rchangeevent.rb +27 -0
  80. data/lib/canis/core/include/rhistory.rb +95 -0
  81. data/lib/canis/core/include/rinputdataevent.rb +47 -0
  82. data/lib/canis/core/include/textdocument.rb +111 -0
  83. data/lib/canis/core/include/vieditable.rb +175 -0
  84. data/lib/canis/core/include/widgetmenu.rb +66 -0
  85. data/lib/canis/core/system/colormap.rb +165 -0
  86. data/lib/canis/core/system/keydefs.rb +32 -0
  87. data/lib/canis/core/system/ncurses.rb +237 -0
  88. data/lib/canis/core/system/panel.rb +129 -0
  89. data/lib/canis/core/system/window.rb +1081 -0
  90. data/lib/canis/core/util/ansiparser.rb +119 -0
  91. data/lib/canis/core/util/app.rb +696 -0
  92. data/lib/canis/core/util/basestack.rb +412 -0
  93. data/lib/canis/core/util/defaultcolorparser.rb +84 -0
  94. data/lib/canis/core/util/extras/README +5 -0
  95. data/lib/canis/core/util/extras/bottomline.rb +1815 -0
  96. data/lib/canis/core/util/extras/padreader.rb +192 -0
  97. data/lib/canis/core/util/focusmanager.rb +31 -0
  98. data/lib/canis/core/util/helpmanager.rb +160 -0
  99. data/lib/canis/core/util/oldwidgetshortcuts.rb +304 -0
  100. data/lib/canis/core/util/promptmenu.rb +235 -0
  101. data/lib/canis/core/util/rcommandwindow.rb +933 -0
  102. data/lib/canis/core/util/rdialogs.rb +520 -0
  103. data/lib/canis/core/util/textutils.rb +74 -0
  104. data/lib/canis/core/util/viewer.rb +238 -0
  105. data/lib/canis/core/util/widgetshortcuts.rb +508 -0
  106. data/lib/canis/core/widgets/applicationheader.rb +103 -0
  107. data/lib/canis/core/widgets/box.rb +58 -0
  108. data/lib/canis/core/widgets/divider.rb +310 -0
  109. data/lib/canis/core/widgets/extras/README.md +12 -0
  110. data/lib/canis/core/widgets/extras/rtextarea.rb +960 -0
  111. data/lib/canis/core/widgets/extras/stackflow.rb +474 -0
  112. data/lib/canis/core/widgets/keylabelprinter.rb +194 -0
  113. data/lib/canis/core/widgets/listbox.rb +326 -0
  114. data/lib/canis/core/widgets/listfooter.rb +86 -0
  115. data/lib/canis/core/widgets/rcombo.rb +210 -0
  116. data/lib/canis/core/widgets/rcontainer.rb +415 -0
  117. data/lib/canis/core/widgets/rlink.rb +30 -0
  118. data/lib/canis/core/widgets/rmenu.rb +970 -0
  119. data/lib/canis/core/widgets/rmenulink.rb +30 -0
  120. data/lib/canis/core/widgets/rmessagebox.rb +400 -0
  121. data/lib/canis/core/widgets/rprogress.rb +118 -0
  122. data/lib/canis/core/widgets/rtabbedpane.rb +631 -0
  123. data/lib/canis/core/widgets/rtabbedwindow.rb +70 -0
  124. data/lib/canis/core/widgets/rwidget.rb +3634 -0
  125. data/lib/canis/core/widgets/scrollbar.rb +147 -0
  126. data/lib/canis/core/widgets/statusline.rb +113 -0
  127. data/lib/canis/core/widgets/table.rb +1072 -0
  128. data/lib/canis/core/widgets/tabular.rb +264 -0
  129. data/lib/canis/core/widgets/textpad.rb +1674 -0
  130. data/lib/canis/core/widgets/tree.rb +690 -0
  131. data/lib/canis/core/widgets/tree/treecellrenderer.rb +150 -0
  132. data/lib/canis/core/widgets/tree/treemodel.rb +432 -0
  133. data/lib/canis/version.rb +3 -0
  134. metadata +229 -0
@@ -0,0 +1,437 @@
1
+ # ------------------------------------------------------------ #
2
+ # File: chunk.rb
3
+ # Description: Contains classes that implement the default native format
4
+ # we use for colored lines display.
5
+ # Author: jkepler http://github.com/mare-imbrium/canis/
6
+ # Date: 07.11.11 - 12:31
7
+ # Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
8
+ # Last update: 2014-06-23 01:43
9
+ # ------------------------------------------------------------ #
10
+ #
11
+
12
+ module Canis
13
+ module Chunks
14
+ extend self
15
+
16
+ # A chunk is a piece of text with associated color and attr.
17
+ # Several such chunks make a ChunkLine.
18
+ # 2014-05-24 - 11:52 adding parent, and trying to resolve at time of render
19
+ # so changes in form;s color can take effect without parsing the tree again.
20
+ #
21
+ # +color+ is a color pair which is already resolved with parent's color at time of
22
+ # parsing. We need to store bgcolor and color so we can resolve at render time if nil.
23
+ #
24
+ class Chunk
25
+
26
+ # color_pair of associated text
27
+ # text to print
28
+ # attribute of associated text
29
+ #attr_accessor :color, :text, :attr
30
+ # hope no one is accessing chunk since format can change to a hash
31
+ attr_reader :chunk
32
+ attr_accessor :parent
33
+ attr_writer :color, :bgcolor
34
+
35
+ # earlier color was being resolved at parse time. Now with chunk change 2014-05-24 - 12:41
36
+ # color should be nil if not specified. Do not use parent's color.
37
+ # Please set fgcolor and bgcolor if present, so we can resolve later.
38
+ def initialize color_pair, text, attr
39
+ @chunk = [ color_pair, text, attr ]
40
+ #@color = color
41
+ #@text = text
42
+ #@attr = attr
43
+ end
44
+ #
45
+ # This is to be called at runtime by render_all or render to resolve
46
+ # the color.
47
+ # @return [color_pair, nil] color pair for chunk, if nil then substitute with
48
+ # the default which will be form's color. If the form has set fg and bg, then nil
49
+ # should not be returned ever.
50
+ def color_pair
51
+ #@chunk[0]
52
+ # if the color was set, use return it.
53
+ return @chunk[0] if @chunk[0]
54
+ if @color && @bgcolor
55
+ # color was not set, but fg and bg both were
56
+ @chunk[0] = get_color(nil, @color, @bgcolor)
57
+ return @chunk[0] if @chunk[0]
58
+ end
59
+ # return a resolved color_pair if we can, but do not store it in tree.
60
+ # This will be resolved each time render is called from parent.
61
+ return get_color(nil, self.color(), self.bgcolor())
62
+ end
63
+ def text
64
+ @chunk[1]
65
+ end
66
+ def attr
67
+ @chunk[2] || @parent.attr || NORMAL
68
+ end
69
+
70
+ # this returns the color of this chunk, else goes up the parents, and finally
71
+ # if none, then returns the default fg color
72
+ # NOTE: this is used at the time of rendering, not parsing
73
+ # This is to ensure that any changes in widgets colors are reflected in renderings
74
+ # without requiring the parse to be done again.
75
+ # Idiealy, the widget would return the form's color if its own was not set, however,
76
+ # i see that color has been removed from form. It should be there, so it reflects
77
+ # in all widgets.
78
+ def color
79
+ @color || @parent.color || $def_fg_color
80
+ end
81
+ # this returns the bgcolor of this chunk, else goes up the parents, and finally
82
+ # if none, then returns the default bg color (global) set in colormap.rb
83
+ # NOTE: this is used at the time of rendering, not parsing
84
+ def bgcolor
85
+ @bgcolor || @parent.bgcolor || $def_bg_color
86
+ end
87
+ end
88
+
89
+ # consists of an array of chunks and corresponds to a line
90
+ # to be printed.
91
+ class ChunkLine < AbstractChunkLine
92
+
93
+ # an array of chunks
94
+ attr_reader :chunks
95
+
96
+ def initialize arr=nil
97
+ @chunks = arr.nil? ? Array.new : arr
98
+ end
99
+ def <<(chunk)
100
+ raise ArgumentError, "Chunk object expected. Received #{chunk.class} " unless chunk.is_a? Chunk
101
+ @chunks << chunk
102
+ end
103
+ alias :add :<<
104
+ def each &block
105
+ @chunks.each &block
106
+ end
107
+ #
108
+ # Splits a chunk line giving text, color and attr
109
+ # The purpose of this is to free callers such as window or pad from having to know the internals
110
+ # of this implementation. Any substituing class should have a similar interface.
111
+ # @yield text, color and attr to the block
112
+ def each_with_color &block
113
+ @chunks.each do |chunk|
114
+ case chunk
115
+ when Chunks::Chunk
116
+ color = chunk.color_pair
117
+ attr = chunk.attr
118
+ text = chunk.text
119
+ when Array
120
+ # for earlier demos that used an array
121
+ color = chunk[0]
122
+ attr = chunk[2]
123
+ text = chunk[1]
124
+ end
125
+ yield text, color, attr
126
+ end
127
+ end
128
+
129
+ # returns length of text in chunks
130
+ def row_length
131
+ result = 0
132
+ @chunks.each { |e| result += e.text.length }
133
+ return result
134
+ end
135
+ # returns match for str in this chunk
136
+ # added 2013-03-07 - 23:59
137
+ # adding index on 2014-05-26 for multiple matches on one line.
138
+ # 2014-05-28 - 12:02 may no longer be used since i have added to_s in next_match in textpad.
139
+ def index str, offset = 0
140
+ result = 0
141
+ _end = 0
142
+ @chunks.each { |e| txt = e.text;
143
+ _end += txt.length
144
+ if _end < offset
145
+ result += e.text.length
146
+ next
147
+ end
148
+
149
+ ix = txt.index(str)
150
+ if ix
151
+ _off = result + ix
152
+ return _off if _off > offset
153
+ end
154
+ result += e.text.length
155
+ }
156
+ return nil
157
+ end
158
+ alias :length :row_length
159
+ alias :size :row_length
160
+
161
+ # return a Chunkline containing only the text for the range requested
162
+ def substring start, size
163
+ raise "substring not implemented yet"
164
+ end
165
+ def to_s
166
+ result = ""
167
+ @chunks.each { |e| result << e.text }
168
+ result
169
+ end
170
+
171
+ # added to take care of many string methods that are called.
172
+ # Callers really don't know this is a chunkline, they assume its a string
173
+ # 2013-03-21 - 19:01
174
+ def method_missing(sym, *args, &block)
175
+ self.to_s.send sym, *args, &block
176
+ end
177
+ # print a chunkline
178
+ # NOTE: tested with pad only. Not with window.
179
+ # Moved from textpad, this is the one method textpad is to call.
180
+ # @param [Pad] pad on which to print
181
+ # @param [Fixnum] lineno to print (zero-based)
182
+ # @param [Fixnum] column to start printing on, usually 0
183
+ # @param [Fixnum] width of pad, usually @content_cols
184
+ # @param [color_pair] colorpair of textpad or widget
185
+ # @param [attr] attr of textpad or widget
186
+ def print pad, lineno, col, width, color_pair, attr
187
+ FFI::NCurses.wmove pad, lineno, col
188
+ a = get_attrib attr
189
+
190
+ show_colored_chunks pad, width, color_pair, a
191
+ end
192
+ # moved from textpad.rb. Note that this does printing for a pad not window
193
+ # so not yet tested if window is passed. Called by +print+.
194
+ def show_colored_chunks(pad, width, defcolor = nil, defattr = nil)
195
+
196
+ self.each_with_color do |text, color, attrib|
197
+
198
+ color ||= defcolor
199
+ attrib ||= defattr || NORMAL
200
+
201
+ #$log.debug "XXX: CHUNK textpad #{text}, cp #{color} , attrib #{attrib}. "
202
+ FFI::NCurses.wcolor_set(pad, color,nil) if color
203
+ FFI::NCurses.wattron(pad, attrib) if attrib
204
+ _print(pad, text, width)
205
+ FFI::NCurses.wattroff(pad, attrib) if attrib
206
+ end
207
+ end
208
+ # called only by show_colored_chunks
209
+ def _print(pad, string, _width )
210
+ w = _width == 0? Ncurses.COLS : _width
211
+ FFI::NCurses.waddnstr(pad,string.to_s, w) # changed 2011 dts
212
+ end
213
+ end
214
+
215
+ # This class does not do the actual parsing which must be done by a class
216
+ # based on the actual format of the marked up document.
217
+ # However, this class converts the parsed fragments to a Chunkline
218
+ # which is an array of Chunks,
219
+ class ColorParser
220
+ attr_reader :stylesheet
221
+ # hash containing color, bgcolor and attr for a given style
222
+ attr_writer :style_map
223
+ def initialize cp
224
+ # in some cases like statusline where it requests window to do some parsing, we will never know who
225
+ # the parent is. We could somehow get the window, and from there the form ???
226
+ @parents = nil
227
+
228
+ chunk_parser cp
229
+
230
+ if cp.is_a? Hash
231
+ @color = cp[:color]
232
+ @bgcolor = cp[:bgcolor]
233
+ @attr = cp[:attr]
234
+ end
235
+ if cp.is_a? TextPad
236
+ self.form = cp
237
+ end
238
+ @attr ||= FFI::NCurses::A_NORMAL
239
+ @color ||= :white
240
+ @bgcolor ||= :black
241
+ @color_pair = get_color($datacolor, @color, @bgcolor)
242
+ @color_array = [@color]
243
+ @bgcolor_array = [@bgcolor]
244
+ @attrib_array = [@attr]
245
+ @color_pair_array = [@color_pair]
246
+ end
247
+ # this is the widget actually that created the parser
248
+ def form=(f)
249
+ @parents = [f]
250
+ end
251
+ public
252
+ # set a stylesheet -- this is a file path containing yaml
253
+ # a style_map is loaded from the stylesheet
254
+ # Sending a symbol such as :help will load style_help.yml
255
+ # @param [String, Symbol] s is a pathname for stylesheet or symbol pointing to a stylesheet
256
+ def stylesheet=(s)
257
+ return unless s
258
+ if s.is_a? Symbol
259
+ s = CANIS_DOCPATH + "style_#{s}.yml"
260
+ end
261
+ @stylesheet = s
262
+ if File.exist? s
263
+ require 'yaml'
264
+ @style_map = YAML::load( File.open( File.expand_path(s) ))
265
+ else
266
+ raise "Could not find stylesheet file #{s}"
267
+ end
268
+ end
269
+ # what if a text pad changes it's content type, should be not allow it to just
270
+ # use the same parser with a changed internal parser
271
+ # UNUSED as yet
272
+ def content_type ct
273
+ return if ct == @content_type
274
+ @content_type = ct
275
+ self.chunk_parser ct
276
+ end
277
+ # supply with a color parser, if you supplied formatted text
278
+ def chunk_parser f
279
+ if f.is_a? Hash
280
+ self.stylesheet = f[:stylesheet]
281
+ content_type = f[:content_type]
282
+ elsif f.is_a? TextPad
283
+ ff = f.document
284
+ self.stylesheet = ff.stylesheet
285
+ content_type = ff.content_type
286
+ elsif f.is_a? Symbol
287
+ content_type = f
288
+ else
289
+ @chunk_parser = f
290
+ return
291
+ end
292
+ @content_type = content_type
293
+ $log.debug "XXX: chunk_parser setting in CP to #{f}, content+type is #{content_type} "
294
+ require 'canis/core/include/canisparser'
295
+ @chunk_parser ||= CanisParser[content_type]
296
+ raise "colorparser could not find a parser for #{content_type} " unless @chunk_parser
297
+ =begin
298
+ if content_type == :tmux
299
+ @chunk_parser = get_default_color_parser()
300
+ elsif content_type == :ansi
301
+ require 'canis/core/util/ansiparser'
302
+ @chunk_parser = AnsiParser.new
303
+ else
304
+ @chunk_parser = f
305
+ end
306
+ =end
307
+ end
308
+
309
+ # since 2014-05-19 - 13:14
310
+ # converts a style name given in a document to color, bg, and attr from a stylesheet
311
+ def resolve_style style
312
+ if @style_map
313
+ # style_map contains a map for each style
314
+ retval = @style_map[style]
315
+ raise "Invalid style #{style} in document" unless retval
316
+ return retval
317
+ end
318
+ raise "Style given in document, but no stylesheet provided"
319
+ end
320
+ # parse array of Strings into array of Chunks (Chunklines)
321
+ # @param [Array<String>] formatted text to be parsed into chunks
322
+ # @return [Array<Abstractchunkline>] array of text in our native format (chunklines)
323
+ def parse_text formatted_text
324
+ l = []
325
+ formatted_text.each { |e|
326
+ l << convert_to_chunk(e)
327
+ }
328
+ return l
329
+ end
330
+ #
331
+ # Takes a formatted string and converts the parsed parts to chunks.
332
+ #
333
+ # @param [String] takes the entire line or string and breaks into an array of chunks
334
+ # @yield chunk if block
335
+ # @return [ChunkLine] # [Array] array of chunks
336
+ # @since 1.4.1 2011-11-3 experimental, can change
337
+ # 2014-05-24 - 12:54 NEW As of now since this is called at parse time
338
+ # colors must not be hardcoded, unless both fg and bg are given for a chunk.
339
+ # The color_pair should be resolved at render time using parent.
340
+ public
341
+ def convert_to_chunk s, colorp=$datacolor, att=FFI::NCurses::A_NORMAL
342
+
343
+ raise "You have not set parent of this using form(). Try setting window.form " unless @parents
344
+ @chunk_parser ||= get_default_color_parser()
345
+ res = ChunkLine.new
346
+ # stack the values, so when user issues "/end" we can pop earlier ones
347
+
348
+ newblockflag = false
349
+ @chunk_parser.parse_format(s) do |p|
350
+ case p
351
+ when Array
352
+ newblockflag = true
353
+ ## got color / attr info, this starts a new span
354
+
355
+ # added style 2014-05-19 - 12:57 maybe should be a hash
356
+ #color, bgcolor, attr , style = *p
357
+ lc, lb, la, ls = *p
358
+ if ls
359
+ #sc, sb, sa = resolve_style ls
360
+ map = resolve_style ls
361
+ $log.debug " STYLLE #{ls} : #{map} "
362
+ lc ||= map[:color]
363
+ lb ||= map[:bgcolor]
364
+ la ||= map[:attr]
365
+ end
366
+ @_bgcolor = lb
367
+ @_color = lc
368
+ if la
369
+ @attr = get_attrib la
370
+ end
371
+ @_color_pair = nil
372
+ if lc && lb
373
+ # we know only store color_pair if both are mentioned in style or tag
374
+ @_color_pair = get_color(nil, lc, lb)
375
+ end
376
+
377
+ #@color_pair_array << @color_pair
378
+ #@attrib_array << @attr
379
+ #$log.debug "XXX: CHUNK start cp=#{@color_pair} , a=#{@attr} :: c:#{lc} b:#{lb} : @c:#{@color} @bg: #{@bgcolor} "
380
+ #$log.debug "XXX: CHUNK start arr #{@color_pair_array} :: #{@attrib_array} ::#{@color_array} ::: #{@bgcolor_array} "
381
+
382
+ when :endcolor
383
+
384
+ # end the current (last) span
385
+ @parents.pop unless @parents.count == 1
386
+ @_bgcolor = @_color = nil
387
+ @_color_pair = nil
388
+ @attr = nil
389
+
390
+ #$log.debug "XXX: CHUNK end parents:#{@parents.count}, last: #{@parents.last} "
391
+ when :reset # ansi has this
392
+ # end all previous colors
393
+ # end the current (last) span
394
+ # maybe we need to remove all parents except for first
395
+ @parents.pop unless @parents.count == 1
396
+ @_bgcolor = @_color = nil
397
+ @_color_pair = nil
398
+ @attr = nil
399
+
400
+
401
+ when String
402
+
403
+ ## create the chunk
404
+ #$log.debug "XXX: CHUNK using on #{p} : #{@_color_pair} , #{@attr}, fg: #{@_color}, #{@_bgcolor}, parent: #{@parents.last} " # 2011-12-10 12:38:51
405
+
406
+ #chunk = [color_pair, p, attr]
407
+ chunk = Chunk.new @_color_pair, p, @attr
408
+ chunk.color = @_color
409
+ chunk.bgcolor = @_bgcolor
410
+ chunk.parent = @parents.last
411
+ if newblockflag
412
+ @parents << chunk
413
+ #$log.debug "XXX: CHUNK start parents:#{@parents.count}, #{@parents.last} "
414
+ newblockflag = true
415
+ end
416
+ if block_given?
417
+ yield chunk
418
+ else
419
+ res << chunk
420
+ end
421
+ end
422
+ end # parse
423
+ return res unless block_given?
424
+ end
425
+ alias :parse_line :convert_to_chunk
426
+
427
+ # returns an instance of the default color parser (tmux parser)
428
+ def get_default_color_parser
429
+ #require 'canis/core/util/defaultcolorparser'
430
+ #@chunk_parser || DefaultColorParser.new
431
+
432
+ require 'canis/core/include/canisparser'
433
+ @chunk_parser ||= CanisParser[:tmux]
434
+ end
435
+ end # class
436
+ end
437
+ end
@@ -0,0 +1,64 @@
1
+ # ----------------------------------------------------------------------------- #
2
+ # File: defaultfilerenderer.rb
3
+ # Description: Simple file renderer, colors an entire line based on some keyword.
4
+ # Author: j kepler http://github.com/mare-imbrium/canis/
5
+ # Date: 2014-06-25 - 12:57
6
+ # License: MIT
7
+ # Last update: 2014-06-25 12:58
8
+ # ----------------------------------------------------------------------------- #
9
+ # defaultfilerenderer.rb Copyright (C) 2012-2014 j kepler
10
+
11
+
12
+ # a simple file renderer that allows setting of colors per line based on
13
+ # regexps passed to +insert_mapping+. See +tasks.rb+ for example usage.
14
+ #
15
+ class DefaultFileRenderer < AbstractTextPadRenderer
16
+ attr_accessor :default_colors
17
+ attr_reader :hash
18
+
19
+ def initialize source=nil
20
+ @default_colors = [:white, :black, NORMAL]
21
+ @pair = get_color($datacolor, @default_colors.first, @default_colors[1])
22
+ end
23
+
24
+ def color_mappings hash
25
+ @hash = hash
26
+ end
27
+ # takes a regexp, and an array of color, bgcolor and attr
28
+ def insert_mapping regex, dim
29
+ @hash ||= {}
30
+ @hash[regex] = dim
31
+ end
32
+ # matches given line with each regexp to determine color use
33
+ # Internally used by render.
34
+ def match_line line
35
+ @hash.each_pair {| k , p|
36
+ if line =~ k
37
+ return p
38
+ end
39
+ }
40
+ return @default_colors
41
+ end
42
+ # render given line in color configured using +insert_mapping+
43
+ def render pad, lineno, text
44
+ if @hash
45
+ dim = match_line text
46
+ fg = dim.first
47
+ bg = dim[1] || @default_colors[1]
48
+ if dim.size == 3
49
+ att = dim.last
50
+ else
51
+ att = @default_colors.last
52
+ end
53
+ cp = get_color($datacolor, fg, bg)
54
+ else
55
+ cp = @pair
56
+ att = @default_colors[2]
57
+ end
58
+
59
+ FFI::NCurses.wattron(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
60
+ FFI::NCurses.mvwaddstr(pad, lineno, 0, text)
61
+ FFI::NCurses.wattroff(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
62
+ end
63
+ #
64
+ end