Class: RubyCurses::DefaultTableRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcurse/experimental/widgets/tablewidget.rb

Overview

TODO see how jtable does the renderers and columns stuff.

perhaps we can combine the two but have different methods or some flag that way oter methods can be shared

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ DefaultTableRenderer

source is the textpad or extending widget needed so we can call show_colored_chunks if the user specifies column wise colors



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 189

def initialize source
  @source = source
  @y = '|'
  @x = '+'
  @coffsets = []
  @header_color = :red
  @header_bgcolor = :white
  @header_attrib = NORMAL
  @color = :white
  @bgcolor = :black
  @color_pair = $datacolor
  @attrib = NORMAL
  @_check_coloring = nil
end

Instance Method Details

#check_colorsObject

check if we need to individually color columns or we can do the entire row in one shot



316
317
318
319
320
321
322
323
324
325
326
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 316

def check_colors
  each_column {|c,i|
  #@chash.each_with_index { |c, i| 
    #next if c.hidden
    if c.color || c.bgcolor || c.attrib
      @_check_coloring = true
      return
    end
    @_check_coloring = false
  }
end

#colorize(pad, lineno, r) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 333

def colorize pad, lineno, r
  # the incoming data is already in the order of display based on chash,
  # so we cannot run chash on it again, so how do we get the color info
  _offset = 0
  # we need to get coffsets here FIXME
  #@chash.each_with_index { |c, i| 
    #next if c.hidden
  each_column {|c,i|
    text = r[i]
    color = c.color
    bg = c.bgcolor
    if color || bg
      cp = get_color(@color_pair, color || @color, bg || @bgcolor)
    else
      cp = @color_pair
    end
    att = c.attrib || @attrib
    FFI::NCurses.wattron(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
    FFI::NCurses.mvwaddstr(pad, lineno, _offset, text)
    FFI::NCurses.wattroff(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
    _offset += text.length
  }
end

#column_model(c) ⇒ Object



219
220
221
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 219

def column_model c
  @chash = c
end

#content_attrib(att) ⇒ Object



216
217
218
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 216

def content_attrib att
  @attrib = att
end

#content_colors(fg, bg) ⇒ Object

set fg and bg color of content rows, default is $datacolor (white on black).



211
212
213
214
215
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 211

def content_colors fg, bg
  @color = fg
  @bgcolor = bg
  @color_pair = get_color($datacolor, fg, bg)
end

#convert_value_to_text(r) ⇒ Object

Takes the array of row data and formats it using column widths and returns a string which is used for printing

TODO return an array so caller can color columns if need be



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 227

def convert_value_to_text r  
  str = []
  fmt = nil
  field = nil
  # we need to loop through chash and get index from it and get that row from r
  #r.each_with_index { |e, i| 
    #c = @chash[i]
  #@chash.each_with_index { |c, i| 
    #next if c.hidden
  each_column {|c,i|
    e = r[c.index]
    w = c.width
    l = e.to_s.length
    # if value is longer than width, then truncate it
    if l > w
      fmt = "%.#{w}s "
    else
      case c.align
      when :right
        fmt = "%#{w}s "
      else
        fmt = "%-#{w}s "
      end
    end
    field = fmt % e
    # if we really want to print a single column with color, we need to print here itself
    # each cell. If we want the user to use tmux formatting in the column itself ...
    # FIXME - this must not be done for headers.
    #if c.color
      #field = "#[fg=#{c.color}]#{field}#[/end]"
    #end
    str << field
  }
  return str
end

#each_columnObject



327
328
329
330
331
332
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 327

def each_column
  @chash.each_with_index { |c, i| 
    next if c.hidden
    yield c,i if block_given?
  }
end

#header_attrib(att) ⇒ Object



207
208
209
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 207

def header_attrib att
  @header_attrib = att
end

#header_colors(fg, bg) ⇒ Object



203
204
205
206
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 203

def header_colors fg, bg
  @header_color = fg
  @header_bgcolor = bg
end

#render(pad, lineno, str) ⇒ Object

Parameters:

  • pad

    for calling print methods on

  • lineno

    the line number on the pad to print on

  • text

    data to print



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 266

def render pad, lineno, str
  #lineno += 1 # header_adjustment
  return render_header pad, lineno, 0, str if lineno == 0
  #text = str.join " | "
  #text = @fmstr % str
  text = convert_value_to_text str
  if @_check_coloring
    $log.debug "XXX:  INSIDE COLORIIN"
    text = colorize pad, lineno, text
    return
  end
  # check if any specific colors , if so then print colors in a loop with no dependence on colored chunks
  # then we don't need source pointer
  text = text.join
  $log.debug "XXX:  NOTINSIDE COLORIIN"
  #if text.index "#["
    #require 'rbcurse/core/include/chunk'
    #@parser ||= Chunks::ColorParser.new :tmux
    #text = @parser.convert_to_chunk text
    #FFI::NCurses.wmove pad, lineno, 0
    #@source.show_colored_chunks text, nil, nil
    #return
  #end
  # FIXME why repeatedly getting this colorpair
  cp = @color_pair
  att = @attrib
  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

#render_header(pad, lineno, col, columns) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 297

def render_header pad, lineno, col, columns
  # I could do it once only but if user sets colors midway we can check once whenvever
  # repainting
  check_colors #if @_check_coloring.nil?
  #text = columns.join " | "
  #text = @fmstr % columns
  text = convert_value_to_text columns
  text = text.join
  bg = @header_bgcolor
  fg = @header_color
  att = @header_attrib
  #cp = $datacolor
  cp = get_color($datacolor, fg, bg)
  FFI::NCurses.wattron(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
  FFI::NCurses.mvwaddstr(pad, lineno, col, text)
  FFI::NCurses.wattroff(pad,FFI::NCurses.COLOR_PAIR(cp) | att)
end